Skip to content

Commit b110a35

Browse files
zekthmcollina
authored andcommitted
fix(SEC-02): preserve meaning of relative references during normalization
normalize()/serialize() applied RFC 3986 remove_dot_segments to relative-path references, which is only defined for merged target paths. This turned document-relative references into different targets: 'a/../b' became '/b' (root-relative), 'x/..' became '/', and '../x' became 'x', so equal('../x', 'x') was true. Add removeDotSegmentsRelative() in lib/utils.js and use it in serialize() when there is no scheme, no authority and the path does not start with '/'. It collapses '.' and 'seg/..' pairs, keeps leading '..' segments that cannot be collapsed, keeps a trailing-directory meaning ('a/b/..' -> 'a/'), and prefixes '.' when the result would be empty or start with an empty segment ('x/..' -> './', 'a/..//x' -> './/x'), so it never becomes root-relative, never introduces an authority and never collapses to the same-document reference ''. resolve() still accepts relative base URIs: Ajv, the main consumer, resolves schema ids against relative bases ('' / '#' / relative $id), and rejecting them breaks test/ajv.test.js. Adds test/relative-normalization-security.test.js with a seeded property test asserting resolve(b, normalize(r)) === resolve(b, r) for absolute bases and normalize() idempotence. (cherry picked from commit 0efc4dcf921a5a36ddefd8496089d3ed77ff8e47)
1 parent 597f254 commit b110a35

4 files changed

Lines changed: 175 additions & 2 deletions

File tree

‎README.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@ uri.normalize('http://example.com/a%2Fb')
8181

8282
Reserved path escapes such as `%2F` and `%2E` are preserved as path data during normalization and comparison.
8383

84+
Dot segments in relative references are removed without changing what the reference resolves to: leading `..` segments that cannot be collapsed are kept, and a relative path is never turned into a root-relative one (`a/../b` → `b`, `../x` → `../x`, `x/..` → `./`).
85+
8486
### Equal
8587

8688
```js

‎index.js‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict'
22

3-
const { normalizeIPv6, removeDotSegments, recomposeAuthority, normalizePercentEncoding, normalizePathEncoding, serializePathEncoding, normalizeQueryFragmentEncoding, encodeQuery, encodeFragment, reescapeHostDelimiters, isIPv4, nonSimpleDomain } = require('./lib/utils')
3+
const { normalizeIPv6, removeDotSegments, removeDotSegmentsRelative, recomposeAuthority, normalizePercentEncoding, normalizePathEncoding, serializePathEncoding, normalizeQueryFragmentEncoding, encodeQuery, encodeFragment, reescapeHostDelimiters, isIPv4, nonSimpleDomain } = require('./lib/utils')
44
const { SCHEMES, getSchemeHandler } = require('./lib/schemes')
55

66
const VALID_SCHEME = /^[A-Za-z][A-Za-z0-9+.-]*$/u
@@ -242,7 +242,12 @@ function serialize (cmpts, opts) {
242242
let s = component.path
243243

244244
if (!options.absolutePath && (!schemeHandler || !schemeHandler.absolutePath)) {
245-
s = removeDotSegments(s)
245+
// A relative-path reference keeps its unresolvable leading ".." segments
246+
// and must never become root-relative (RFC 3986 section 5.2.4 applies to
247+
// merged target paths, not to the reference itself).
248+
s = component.scheme === undefined && authority === undefined && s[0] !== '/'
249+
? removeDotSegmentsRelative(s)
250+
: removeDotSegments(s)
246251
}
247252

248253
// Dot-segment removal can expose a colon that was not originally in the

‎lib/utils.js‎

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,54 @@ function removeDotSegments (path) {
345345
return output.join('')
346346
}
347347

348+
/**
349+
* Removes dot segments from the path of a relative reference (a path that does
350+
* not start with "/") without changing what it resolves to against any base.
351+
* Unlike `removeDotSegments`, leading ".." segments that cannot be collapsed
352+
* are kept, the result never becomes root-relative, and a result that would be
353+
* empty or start with an empty segment is prefixed with "." (an empty path
354+
* means "same document" and a leading "//" would introduce an authority).
355+
*
356+
* @param {string} path
357+
* @returns {string}
358+
*/
359+
function removeDotSegmentsRelative (path) {
360+
if (path === '' || path[0] === '/') {
361+
return removeDotSegments(path)
362+
}
363+
364+
const segments = path.split('/')
365+
const last = segments.length - 1
366+
/** @type {string[]} */
367+
const output = []
368+
let trailingDirectory = false
369+
370+
for (let i = 0; i <= last; i++) {
371+
const segment = segments[i]
372+
if (segment === '.' || segment === '..') {
373+
if (segment === '..') {
374+
if (output.length !== 0 && output[output.length - 1] !== '..') {
375+
output.pop()
376+
} else {
377+
output.push('..')
378+
}
379+
}
380+
trailingDirectory = i === last
381+
} else {
382+
output.push(segment)
383+
}
384+
}
385+
386+
if (trailingDirectory && output[output.length - 1] !== '..') {
387+
output.push('')
388+
}
389+
if (output.length === 0 || output[0] === '') {
390+
output.unshift('.')
391+
}
392+
393+
return output.join('/')
394+
}
395+
348396
/**
349397
* Re-escape RFC 3986 gen-delims that must not appear literally in the host.
350398
* After the URI regex parses, these characters cannot be literal in the host
@@ -740,6 +788,7 @@ module.exports = {
740788
encodeFragment,
741789
escapePreservingEscapes,
742790
removeDotSegments,
791+
removeDotSegmentsRelative,
743792
isIPv4,
744793
isUUID,
745794
normalizeIPv6,
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
'use strict'
2+
3+
const test = require('tape')
4+
const fastURI = require('..')
5+
6+
const ABSOLUTE_BASES = [
7+
'http://h/p/q/r',
8+
'http://h/',
9+
'foo://h/a/b/',
10+
'http://h/p/q/'
11+
]
12+
13+
test('normalize keeps the meaning of relative-path references', (t) => {
14+
const cases = [
15+
['a/../b', 'b'],
16+
['x/..', './'],
17+
['../x', '../x'],
18+
['./../x', '../x'],
19+
['a/b/../../..', '..'],
20+
['a/./b', 'a/b'],
21+
['a/b/..', 'a/'],
22+
['.', './'],
23+
['..', '..'],
24+
['../', '../'],
25+
['../../a/./b/../c', '../../a/c'],
26+
['a//..', 'a/'],
27+
['a/../b?q#f', 'b?q#f']
28+
]
29+
for (const [input, expected] of cases) {
30+
t.equal(fastURI.normalize(input), expected, `${input} -> ${expected}`)
31+
}
32+
t.end()
33+
})
34+
35+
test('normalize never turns a relative-path reference into a root or network path', (t) => {
36+
t.equal(fastURI.normalize('.//x'), './/x', 'leading empty segment is kept behind "."')
37+
t.equal(fastURI.normalize('a/..//x'), './/x', 'collapsed prefix does not expose "//"')
38+
t.equal(fastURI.parse(fastURI.normalize('a/..//x')).host, undefined, 'no authority is introduced')
39+
t.equal(fastURI.normalize('./a:b'), 'a%3Ab', 'exposed colon is still escaped')
40+
t.equal(fastURI.parse(fastURI.normalize('./a:b')).scheme, undefined, 'no scheme is introduced')
41+
t.end()
42+
})
43+
44+
test('normalize keeps the existing behavior for root-relative and absolute paths', (t) => {
45+
t.equal(fastURI.normalize('/a/../b'), '/b')
46+
t.equal(fastURI.normalize('/../x'), '/x')
47+
t.equal(fastURI.normalize('http://h/a/../../b'), 'http://h/b')
48+
t.end()
49+
})
50+
51+
test('equal does not alias relative references with different targets', (t) => {
52+
t.equal(fastURI.equal('../x', 'x'), false)
53+
t.equal(fastURI.equal('a/../b', '/b'), false)
54+
t.equal(fastURI.equal('x/..', '/'), false)
55+
t.equal(fastURI.equal('a/../b', 'b'), true)
56+
t.end()
57+
})
58+
59+
test('resolving a normalized relative reference matches resolving the original', (t) => {
60+
for (const base of ABSOLUTE_BASES) {
61+
for (const ref of ['a/../b', 'x/..', '../x', './../x', 'a/b/../../..', '.', '..', './/x']) {
62+
t.equal(
63+
fastURI.resolve(base, fastURI.normalize(ref)),
64+
fastURI.resolve(base, ref),
65+
`${base} + ${ref}`
66+
)
67+
}
68+
}
69+
t.equal(fastURI.resolve('http://h/p/q/r', fastURI.normalize('a/../b')), 'http://h/p/q/b')
70+
t.equal(fastURI.resolve('http://h/p/q/r', fastURI.normalize('../x')), 'http://h/p/x')
71+
t.end()
72+
})
73+
74+
test('property: normalization of relative references is resolution-preserving and idempotent', (t) => {
75+
const segments = ['a', 'b', '.', '..', '']
76+
// Deterministic linear congruential generator so failures are reproducible.
77+
let seed = 0x5ec02
78+
const next = (n) => {
79+
seed = (seed * 1103515245 + 12345) % 2147483648
80+
return Math.floor((seed / 2147483648) * n)
81+
}
82+
83+
let failures = 0
84+
let checked = 0
85+
for (let i = 0; i < 4000; i++) {
86+
const length = 1 + next(6)
87+
const parts = []
88+
for (let j = 0; j < length; j++) parts.push(segments[next(segments.length)])
89+
const ref = parts.join('/')
90+
// Only relative-path references: empty refs and refs starting with "/"
91+
// are not affected by this change ("//" would also introduce an authority).
92+
if (ref === '' || ref[0] === '/') continue
93+
94+
checked++
95+
const normalized = fastURI.normalize(ref)
96+
if (fastURI.normalize(normalized) !== normalized) {
97+
failures++
98+
t.fail(`not idempotent: ${ref} -> ${normalized} -> ${fastURI.normalize(normalized)}`)
99+
}
100+
// 3.x's removeDotSegments (used by resolve) collapses a trailing "//" to
101+
// "/" (fixed in v4 by #185), so resolve() itself is inconsistent for
102+
// these paths regardless of normalization.
103+
if (normalized.endsWith('//')) continue
104+
for (const base of ABSOLUTE_BASES) {
105+
const expected = fastURI.resolve(base, ref)
106+
const actual = fastURI.resolve(base, normalized)
107+
if (actual !== expected) {
108+
failures++
109+
t.fail(`${base} + ${ref}: normalized ${normalized} resolves to ${actual}, expected ${expected}`)
110+
}
111+
}
112+
}
113+
114+
t.ok(checked > 1000, `checked ${checked} relative references`)
115+
t.equal(failures, 0, 'no relative reference changed meaning')
116+
t.end()
117+
})

0 commit comments

Comments
 (0)