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 @@ -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" , ( ) => {
You can’t perform that action at this time.
0 commit comments