Skip to content
Closed
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
33 changes: 24 additions & 9 deletions src/core/utils/url.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,31 @@ export function sanitizeUrl(url) {
if (urlTrimmed.startsWith("/")) {
return `${urlObject.pathname}${urlObject.search}${urlObject.hash}`
}

if (urlTrimmed.startsWith("./")) {
return `.${urlObject.pathname}${urlObject.search}${urlObject.hash}`
}

if (urlTrimmed.startsWith("../")) {
return `..${urlObject.pathname}${urlObject.search}${urlObject.hash}`

// Handle relative paths (./path, ../path, ./../../path, etc.)
if (urlTrimmed.startsWith("./") || urlTrimmed.startsWith("../")) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (urlTrimmed.startsWith("./") || urlTrimmed.startsWith("../")) {
if (urlTrimmed.startsWith("./") || urlTrimmed.startsWith("../")) {
const relativePath = urlTrimmed.match(/^(\.\.?\/)+/)[0]
const remainingPath = urlObject.pathname.substring(1)
return `${relativePath}${remainingPath}${urlObject.search}${urlObject.hash}`
}

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@glowcloud Yes, that approach works for me.

const segments = urlTrimmed.split("/")
let relativePath = ""
let pathStartIndex = 0

// Process initial relative segments
for (let i = 0; i < segments.length; i++) {
if (segments[i] === ".") {
relativePath += "./"
pathStartIndex = i + 1
} else if (segments[i] === "..") {
relativePath += "../"
pathStartIndex = i + 1
} else {
break
}
}

// Get the remaining path from the URL object
const remainingPath = urlObject.pathname.substring(1)
return `${relativePath}${remainingPath}${urlObject.search}${urlObject.hash}`
}

return `${urlObject.pathname.substring(1)}${urlObject.search}${urlObject.hash}`
}

Expand All @@ -78,4 +94,3 @@ export function sanitizeUrl(url) {
return blankURL
}
}

4 changes: 4 additions & 0 deletions test/unit/core/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -1486,6 +1486,10 @@ describe("utils", () => {
expect(sanitizeUrl("./openapi.json")).toEqual("./openapi.json")
expect(sanitizeUrl("..openapi.json")).toEqual("..openapi.json")
expect(sanitizeUrl("../openapi.json")).toEqual("../openapi.json")
expect(sanitizeUrl("../../openapi.json")).toEqual("../../openapi.json")
expect(sanitizeUrl("../../../openapi.json")).toEqual("../../../openapi.json")
expect(sanitizeUrl("../../../../openapi.json")).toEqual("../../../../openapi.json")
expect(sanitizeUrl("./../../../openapi.json")).toEqual("./../../../openapi.json")
})

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