Skip to content

Try to ignore dtd references in xml configs parsing #2163

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 19, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,15 @@ import org.utbot.intellij.plugin.ui.utils.getResourcesPaths
import org.utbot.intellij.plugin.ui.utils.getSortedTestRoots
import org.utbot.intellij.plugin.ui.utils.isBuildWithGradle
import org.utbot.intellij.plugin.ui.utils.suitableTestSourceRoots
import java.io.IOException
import java.io.StringReader
import java.nio.file.Files
import javax.xml.parsers.DocumentBuilder
import javax.xml.parsers.DocumentBuilderFactory
import javax.xml.parsers.ParserConfigurationException
import kotlin.streams.asSequence


val PsiClass.packageName: String get() = this.containingFile.containingDirectory.getPackage()?.qualifiedName ?: ""
const val HISTORY_LIMIT = 10

Expand Down Expand Up @@ -125,7 +130,7 @@ open class BaseTestsModel(
.filter { it.fileExtension == ".xml" }
}

val builder = DocumentBuilderFactory.newInstance().newDocumentBuilder()
val builder = customizeXmlBuilder()
return xmlFilePaths.mapNotNullTo(mutableSetOf()) { path ->
try {
val doc = builder.parse(path.toFile())
Expand All @@ -137,14 +142,32 @@ open class BaseTestsModel(
else -> null
}
} catch (e: Exception) {
// Sometimes xml parsing may fail, for example, when it references external DTD schemas.
// See https://stackoverflow.com/questions/343383/unable-to-parse-xml-file-using-documentbuilder.
// `DocumentBuilder.parse` is an unpredictable operation, may have some side effects, we suppress them.
null
}

}
}

/**
* Creates "safe" xml builder instance.
*
* Using standard `DocumentBuilderFactory.newInstance()` may lead to some problems like
* https://stackoverflow.com/questions/343383/unable-to-parse-xml-file-using-documentbuilder.
*
* We try to solve it in accordance with top-rated recommendation here
* https://stackoverflow.com/questions/155101/make-documentbuilder-parse-ignore-dtd-references.
*/
private fun customizeXmlBuilder(): DocumentBuilder {
val builderFactory = DocumentBuilderFactory.newInstance()
builderFactory.isNamespaceAware = true

// See documentation https://xerces.apache.org/xerces2-j/features.html
builderFactory.setFeature("http://apache.org/xml/features/nonvalidating/load-dtd-grammar", false)
builderFactory.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false)

return builderFactory.newDocumentBuilder()
}

fun updateSourceRootHistory(path: String) {
sourceRootHistory.apply {
remove(path)//Remove existing entry if any
Expand Down