Skip to content
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

VulnerableCode URL escape fixes #8222

Merged
merged 2 commits into from
Feb 2, 2024
Merged
Show file tree
Hide file tree
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 @@ -173,9 +173,9 @@ class VulnerableCode(name: String, config: VulnerableCodeConfiguration) : Advice
}
}

private val BACKSLASH_ESCAPE_REGEX = Regex("\\\\\\\\(.)")
private val BACKSLASH_ESCAPE_REGEX = """\\\\\\?(.)""".toRegex()

internal fun String.fixupUrlEscaping(): String =
replace("\\/", "/").replace(BACKSLASH_ESCAPE_REGEX) {
replace("""\/""", "/").replace(BACKSLASH_ESCAPE_REGEX) {
it.groupValues[1].percentEncode()
}
Original file line number Diff line number Diff line change
Expand Up @@ -277,26 +277,29 @@ class VulnerableCodeTest : WordSpec({
}
}

@Suppress("MaxLineLength")
"fixupUrlEscaping()" should {
"fixup a wrongly escaped ampersand" {
val brokenUrl = "https://nvd.nist.gov/vuln/search/results?adv_search=true&isCpeNameSearch=true" +
"&query=cpe:2.3:a:oracle:retail_category_management_planning_" +
"\\\\&_optimization:16.0.3:*:*:*:*:*:*:*"

URI.create(brokenUrl.fixupUrlEscaping()) shouldBe URI(
"https://nvd.nist.gov/vuln/search/results?adv_search=true&isCpeNameSearch=true" +
"&query=cpe:2.3:a:oracle:retail_category_management_planning_" +
"%26_optimization:16.0.3:*:*:*:*:*:*:*"
val u = """https://nvd.nist.gov/vuln/search/results?adv_search=true&isCpeNameSearch=true&query=cpe:2.3:a:oracle:retail_category_management_planning_\\&_optimization:16.0.3:*:*:*:*:*:*:*"""

URI.create(u.fixupUrlEscaping()) shouldBe URI(
"""https://nvd.nist.gov/vuln/search/results?adv_search=true&isCpeNameSearch=true&query=cpe:2.3:a:oracle:retail_category_management_planning_%26_optimization:16.0.3:*:*:*:*:*:*:*"""
)
}

"fixup a wrongly escaped slash" {
val brokenUrl = "https://nvd.nist.gov/vuln/search/results?adv_search=true&isCpeNameSearch=true" +
"&query=cpe:2.3:a:apple:swiftnio_http\\/2:*:*:*:*:*:swift:*:*"
val u = """https://nvd.nist.gov/vuln/search/results?adv_search=true&isCpeNameSearch=true&query=cpe:2.3:a:apple:swiftnio_http\/2:*:*:*:*:*:swift:*:*"""

URI.create(u.fixupUrlEscaping()) shouldBe URI(
"""https://nvd.nist.gov/vuln/search/results?adv_search=true&isCpeNameSearch=true&query=cpe:2.3:a:apple:swiftnio_http/2:*:*:*:*:*:swift:*:*"""
)
}

"fixup a wrongly escaped plus" {
val u = """https://nvd.nist.gov/vuln/search/results?adv_search=true&isCpeNameSearch=true&query=cpe:2.3:a:oracle:hyperion_bi\\\+:*:*:*:*:*:*:*:*"""

URI.create(brokenUrl.fixupUrlEscaping()) shouldBe URI(
"https://nvd.nist.gov/vuln/search/results?adv_search=true&isCpeNameSearch=true&" +
"query=cpe:2.3:a:apple:swiftnio_http/2:*:*:*:*:*:swift:*:*"
URI.create(u.fixupUrlEscaping()) shouldBe URI(
"""https://nvd.nist.gov/vuln/search/results?adv_search=true&isCpeNameSearch=true&query=cpe:2.3:a:oracle:hyperion_bi%2B:*:*:*:*:*:*:*:*"""
)
}
}
Expand Down
Loading