File tree Expand file tree Collapse file tree 2 files changed +22
-5
lines changed Expand file tree Collapse file tree 2 files changed +22
-5
lines changed Original file line number Diff line number Diff line change @@ -61,15 +61,30 @@ export function sanitizeUrl(url) {
6161 if (urlTrimmed.startsWith("/")) {
6262 return `${urlObject.pathname}${urlObject.search}${urlObject.hash}`
6363 }
64-
64+
6565 if (urlTrimmed.startsWith("./")) {
6666 return `.${urlObject.pathname}${urlObject.search}${urlObject.hash}`
6767 }
68-
68+
69+ // Handle multiple levels of relative paths (../, ../../, ../../../, etc.)
6970 if (urlTrimmed.startsWith("../")) {
70- return `..${urlObject.pathname}${urlObject.search}${urlObject.hash}`
71+ // Count the number of ../ segments
72+ const segments = urlTrimmed.split("/")
73+ let relativeLevels = 0
74+
75+ for (const segment of segments) {
76+ if (segment === "..") {
77+ relativeLevels++
78+ } else {
79+ break
80+ }
81+ }
82+
83+ // Reconstruct the relative path with correct number of ../
84+ const relativePath = "../".repeat(relativeLevels)
85+ return `${relativePath}${urlObject.pathname.substring(1)}${urlObject.search}${urlObject.hash}`
7186 }
72-
87+
7388 return `${urlObject.pathname.substring(1)}${urlObject.search}${urlObject.hash}`
7489 }
7590
@@ -78,4 +93,3 @@ export function sanitizeUrl(url) {
7893 return blankURL
7994 }
8095}
81-
Original file line number Diff line number Diff line change @@ -1454,6 +1454,9 @@ describe("utils", () => {
14541454 expect(sanitizeUrl("./openapi.json")).toEqual("./openapi.json")
14551455 expect(sanitizeUrl("..openapi.json")).toEqual("..openapi.json")
14561456 expect(sanitizeUrl("../openapi.json")).toEqual("../openapi.json")
1457+ expect(sanitizeUrl("../../openapi.json")).toEqual("../../openapi.json")
1458+ expect(sanitizeUrl("../../../openapi.json")).toEqual("../../../openapi.json")
1459+ expect(sanitizeUrl("../../../../openapi.json")).toEqual("../../../../openapi.json")
14571460 })
14581461
14591462 it("should gracefully handle empty strings", () => {
You can’t perform that action at this time.
0 commit comments