Skip to content

Commit dfd469a

Browse files
David VogelDavidVogel
authored andcommitted
fix(url): handle arbitrary number of levels for relative paths
1 parent 04b9ad7 commit dfd469a

File tree

2 files changed

+22
-5
lines changed

2 files changed

+22
-5
lines changed

src/core/utils/url.js

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff 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-

test/unit/core/utils.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1486,6 +1486,9 @@ describe("utils", () => {
14861486
expect(sanitizeUrl("./openapi.json")).toEqual("./openapi.json")
14871487
expect(sanitizeUrl("..openapi.json")).toEqual("..openapi.json")
14881488
expect(sanitizeUrl("../openapi.json")).toEqual("../openapi.json")
1489+
expect(sanitizeUrl("../../openapi.json")).toEqual("../../openapi.json")
1490+
expect(sanitizeUrl("../../../openapi.json")).toEqual("../../../openapi.json")
1491+
expect(sanitizeUrl("../../../../openapi.json")).toEqual("../../../../openapi.json")
14891492
})
14901493

14911494
it("should gracefully handle empty strings", () => {

0 commit comments

Comments
 (0)