Skip to content

Commit 9eb8759

Browse files
committed
fix: preserve relative paths in suffix and relative resolution
1 parent 54c30f7 commit 9eb8759

2 files changed

Lines changed: 21 additions & 4 deletions

File tree

‎index.js‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -147,14 +147,17 @@ function resolveComponent (base, relative, options, skipNormalization) {
147147
if (relative.path[0] === '/') {
148148
target.path = removeDotSegments(relative.path)
149149
} else {
150-
if ((base.userinfo !== undefined || base.host !== undefined || base.port !== undefined) && !base.path) {
150+
const baseHasAuthority = base.userinfo !== undefined || base.host !== undefined || base.port !== undefined
151+
if (baseHasAuthority && !base.path) {
151152
target.path = '/' + relative.path
152153
} else if (!base.path) {
153154
target.path = relative.path
154155
} else {
155156
target.path = base.path.slice(0, base.path.lastIndexOf('/') + 1) + relative.path
156157
}
157-
target.path = removeDotSegments(target.path)
158+
target.path = base.scheme === undefined && !baseHasAuthority && target.path[0] !== '/'
159+
? removeDotSegmentsRelative(target.path)
160+
: removeDotSegments(target.path)
158161
}
159162
target.query = relative.query
160163
}
@@ -259,7 +262,7 @@ function serialize (cmpts, opts) {
259262
// A relative-path reference keeps its unresolvable leading ".." segments
260263
// and must never become root-relative (RFC 3986 section 5.2.4 applies to
261264
// merged target paths, not to the reference itself).
262-
s = component.scheme === undefined && authority === undefined && s[0] !== '/'
265+
s = schemelessReference && s[0] !== '/'
263266
? removeDotSegmentsRelative(s)
264267
: removeDotSegments(s)
265268
}

‎test/relative-normalization-security.test.js‎

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ const ABSOLUTE_BASES = [
1010
'http://h/p/q/'
1111
]
1212

13+
const RELATIVE_BASES = ['', 'base', 'relative/base']
14+
1315
test('normalize keeps the meaning of relative-path references', (t) => {
1416
const cases = [
1517
['a/../b', 'b'],
@@ -57,7 +59,7 @@ test('equal does not alias relative references with different targets', (t) => {
5759
})
5860

5961
test('resolving a normalized relative reference matches resolving the original', (t) => {
60-
for (const base of ABSOLUTE_BASES) {
62+
for (const base of [...ABSOLUTE_BASES, ...RELATIVE_BASES]) {
6163
for (const ref of ['a/../b', 'x/..', '../x', './../x', 'a/b/../../..', '.', '..', './/x']) {
6264
t.equal(
6365
fastURI.resolve(base, fastURI.normalize(ref)),
@@ -116,6 +118,18 @@ test('property: normalization of relative references is resolution-preserving an
116118
t.end()
117119
})
118120

121+
test('suffix normalization preserves relative path meaning', (t) => {
122+
const options = { reference: 'suffix', scheme: 'x' }
123+
for (const [input, expected] of [
124+
['a/../b', 'b'],
125+
['x/..', './'],
126+
['a/../a:b', './a:b']
127+
]) {
128+
t.equal(fastURI.normalize(input, options), expected, `${input} -> ${expected}`)
129+
}
130+
t.end()
131+
})
132+
119133
test('normalize does not promote a colon-first relative reference to a scheme (skipEscape)', (t) => {
120134
// RFC 3986 section 4.2: a first path segment containing ":" must stay
121135
// protected by a leading "./", or it is mistaken for a scheme. Collapsing

0 commit comments

Comments
 (0)