Skip to content

Commit 60dee8b

Browse files
authored
fix: sanitization of relative OpenAPI JSON paths (#10528)
1 parent 679e73b commit 60dee8b

File tree

2 files changed

+20
-5
lines changed

2 files changed

+20
-5
lines changed

src/core/utils/url.js

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,19 @@ export function sanitizeUrl(url) {
5858

5959
// return sanitized URI reference
6060
if (urlObject.origin === base) {
61-
return urlTrimmed.startsWith("/")
62-
? `${urlObject.pathname}${urlObject.search}${urlObject.hash}`
63-
: urlTrimmed.startsWith(".")
64-
? `.${urlObject.pathname}${urlObject.search}${urlObject.hash}`
65-
: `${urlObject.pathname.substring(1)}${urlObject.search}${urlObject.hash}`
61+
if (urlTrimmed.startsWith("/")) {
62+
return `${urlObject.pathname}${urlObject.search}${urlObject.hash}`
63+
}
64+
65+
if (urlTrimmed.startsWith("./")) {
66+
return `.${urlObject.pathname}${urlObject.search}${urlObject.hash}`
67+
}
68+
69+
if (urlTrimmed.startsWith("../")) {
70+
return `..${urlObject.pathname}${urlObject.search}${urlObject.hash}`
71+
}
72+
73+
return `${urlObject.pathname.substring(1)}${urlObject.search}${urlObject.hash}`
6674
}
6775

6876
return String(urlObject)

test/unit/core/utils.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1449,6 +1449,13 @@ describe("utils", () => {
14491449
expect(sanitizeUrl(url)).toEqual("https://swagger.io/")
14501450
})
14511451

1452+
it("should gracefully handle relative paths", () => {
1453+
expect(sanitizeUrl(".openapi.json")).toEqual(".openapi.json")
1454+
expect(sanitizeUrl("./openapi.json")).toEqual("./openapi.json")
1455+
expect(sanitizeUrl("..openapi.json")).toEqual("..openapi.json")
1456+
expect(sanitizeUrl("../openapi.json")).toEqual("../openapi.json")
1457+
})
1458+
14521459
it("should gracefully handle empty strings", () => {
14531460
expect(sanitizeUrl("")).toEqual("")
14541461
})

0 commit comments

Comments
 (0)