fast-uri 4.1.4 → 4.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -0
- package/SECURITY.md +62 -13
- package/index.js +40 -9
- package/lib/schemes.js +46 -16
- package/lib/utils.js +54 -10
- package/package.json +1 -1
- package/test/fixtures/uri-js-parse.json +1 -0
- package/test/ipv6-validation.test.js +130 -0
- package/test/mailto-field-name-security.test.js +151 -0
- package/test/security-normalization.test.js +38 -0
- package/types/index.d.ts +16 -0
package/README.md
CHANGED
|
@@ -99,6 +99,7 @@ fast-uri supports inserting custom [scheme](https://en.wikipedia.org/wiki/URI_sc
|
|
|
99
99
|
* wss \[[RFC 6455](https://www.ietf.org/rfc/rfc6455.txt)\]
|
|
100
100
|
* urn \[[RFC 2141](https://www.ietf.org/rfc/rfc2141.txt)\]
|
|
101
101
|
* urn:uuid \[[RFC 4122](https://www.ietf.org/rfc/rfc4122.txt)\]
|
|
102
|
+
* mailto \[[RFC 6068](https://www.rfc-editor.org/rfc/rfc6068)\]
|
|
102
103
|
|
|
103
104
|
|
|
104
105
|
## Benchmarks
|
package/SECURITY.md
CHANGED
|
@@ -15,38 +15,87 @@ Reports based solely on comparing or mixing parsers that follow these different
|
|
|
15
15
|
standards are out of scope. Applications must use the same parsing and
|
|
16
16
|
normalization rules for both security decisions and subsequent URI use.
|
|
17
17
|
|
|
18
|
+
A WHATWG comparison may be useful supporting evidence, but a parser difference
|
|
19
|
+
is not by itself a vulnerability. A report remains in scope when it independently
|
|
20
|
+
demonstrates a defect in `fast-uri`'s RFC or documented behavior for a supported
|
|
21
|
+
scheme and a concrete security impact when the application uses `fast-uri`
|
|
22
|
+
consistently. An exploit that requires validating with `fast-uri` and then
|
|
23
|
+
passing the original, unmodified input to a parser with different rules is an
|
|
24
|
+
application parser-confusion issue.
|
|
25
|
+
|
|
18
26
|
Reports are assessed based on reproducibility, documented supported usage, and
|
|
19
27
|
concrete security impact—not merely on whether they mention WHATWG URL
|
|
20
|
-
behavior.
|
|
21
|
-
scheme remain eligible for evaluation.
|
|
28
|
+
behavior.
|
|
22
29
|
|
|
23
30
|
## Threat model
|
|
24
31
|
|
|
25
32
|
`fast-uri`'s threat model extends the
|
|
26
33
|
[Node.js threat model](https://github.com/nodejs/node/blob/main/SECURITY.md#the-nodejs-threat-model).
|
|
27
34
|
|
|
28
|
-
**Trusted:** Application code,
|
|
29
|
-
|
|
35
|
+
**Trusted:** Application code, structured component objects constructed or
|
|
36
|
+
modified by application code and passed directly to `serialize()`, parser
|
|
37
|
+
options, configuration, and the runtime environment.
|
|
30
38
|
|
|
31
39
|
**Untrusted:** URI and IRI strings passed to the package's public APIs.
|
|
32
40
|
|
|
41
|
+
## Vulnerability classification
|
|
42
|
+
|
|
43
|
+
A report is treated as a vulnerability when it demonstrates all of the
|
|
44
|
+
following:
|
|
45
|
+
|
|
46
|
+
1. Attacker-controlled input reaches a public API through documented,
|
|
47
|
+
supported usage.
|
|
48
|
+
2. `fast-uri` violates an applicable RFC or its documented API contract.
|
|
49
|
+
3. The defect creates a concrete confidentiality, integrity, or availability
|
|
50
|
+
impact in a realistic supported use case.
|
|
51
|
+
4. The impact does not depend solely on application misuse, an unsupported
|
|
52
|
+
scheme, a different parser interpreting the original input, or another
|
|
53
|
+
package's behavior.
|
|
54
|
+
|
|
55
|
+
A reproducible bug can still merit a normal issue and fix without meeting the
|
|
56
|
+
security-vulnerability threshold.
|
|
57
|
+
|
|
33
58
|
### Examples of vulnerabilities
|
|
34
59
|
|
|
35
|
-
-
|
|
36
|
-
-
|
|
37
|
-
|
|
60
|
+
- `parse()`, `normalize()`, or `resolve()` changes an attacker-controlled
|
|
61
|
+
authority within `fast-uri`, and the report demonstrates a host security
|
|
62
|
+
control being bypassed using that output
|
|
63
|
+
- `equal()` aliases distinct URIs under the rules of a supported scheme, and
|
|
64
|
+
the report demonstrates an authorization, signature, or cache boundary being
|
|
65
|
+
bypassed
|
|
66
|
+
- One public API call on a reasonably sized malformed URI causes
|
|
67
|
+
disproportionate CPU or memory consumption, with reproducible availability
|
|
68
|
+
impact
|
|
69
|
+
- Parsing or normalization turns component data into a structural delimiter
|
|
70
|
+
and the report demonstrates a security-control bypass in documented usage
|
|
71
|
+
|
|
72
|
+
### Denial-of-service reports
|
|
73
|
+
|
|
74
|
+
A synchronous exception, parse error, or non-idempotent result is not by itself
|
|
75
|
+
a denial-of-service vulnerability. A report must demonstrate disproportionate
|
|
76
|
+
resource consumption in a public API call, or loss of availability in a
|
|
77
|
+
supported deployment despite normal exception handling. An application that
|
|
78
|
+
terminates on an uncaught exception, or repeatedly normalizes until reaching a
|
|
79
|
+
fixpoint, does not establish a package vulnerability by itself.
|
|
38
80
|
|
|
39
81
|
### Examples of non-vulnerabilities
|
|
40
82
|
|
|
41
83
|
The following are **not** considered vulnerabilities in `fast-uri`:
|
|
42
84
|
|
|
43
|
-
- **Different URL standards:**
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
- **Unsupported scheme semantics:**
|
|
47
|
-
`fast-uri` does not document as supported
|
|
85
|
+
- **Different URL standards:** `fast-uri` returns one host, but an application
|
|
86
|
+
passes the original input to WHATWG `URL`, `fetch`, or another parser that
|
|
87
|
+
applies different recovery, whitespace, or legacy-IP rules
|
|
88
|
+
- **Unsupported scheme semantics:** Behavior specific to schemes such as
|
|
89
|
+
`ftp` or `file`, which `fast-uri` does not document as supported
|
|
90
|
+
- **Correctness without security impact:** Non-idempotent normalization,
|
|
91
|
+
surprising equality, truncation, re-encoding, or malformed-input handling
|
|
92
|
+
without a demonstrated security boundary or availability impact
|
|
93
|
+
- **Caller-created resource use:** An application repeatedly parses or
|
|
94
|
+
normalizes a value, or fails to catch a synchronous exception
|
|
95
|
+
- **Trusted structured input:** Boundary characters supplied in a component
|
|
96
|
+
object that application code constructs and passes to `serialize()`
|
|
48
97
|
- **Application code vulnerabilities:** Security flaws in code that consumes
|
|
49
|
-
`fast-uri` output
|
|
98
|
+
`fast-uri` output contrary to its documented contract
|
|
50
99
|
- **Configuration mistakes:** Security issues caused by incorrect parser
|
|
51
100
|
options or application configuration
|
|
52
101
|
- **Missing security features:** Application-level protections that are not
|
package/index.js
CHANGED
|
@@ -89,6 +89,22 @@ function resolve (baseURI, relativeURI, options) {
|
|
|
89
89
|
return serialize(resolved, schemelessOptions)
|
|
90
90
|
}
|
|
91
91
|
|
|
92
|
+
/**
|
|
93
|
+
* Copies a host component together with its parse-time zone identifier (if
|
|
94
|
+
* any), so downstream recomposition does not re-derive an IPv6 zone from the
|
|
95
|
+
* ambiguous single-"%" component form. The zone is validated on use by
|
|
96
|
+
* `recomposeAuthority` rather than blindly trusted.
|
|
97
|
+
*
|
|
98
|
+
* @param {import('./types/index').URIComponent} target
|
|
99
|
+
* @param {import('./types/index').URIComponent} source
|
|
100
|
+
*/
|
|
101
|
+
function copyHost (target, source) {
|
|
102
|
+
target.host = source.host
|
|
103
|
+
if (source.ipv6Zone !== undefined) {
|
|
104
|
+
target.ipv6Zone = source.ipv6Zone
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
|
|
92
108
|
/**
|
|
93
109
|
* @param {import ('./types/index').URIComponent} base
|
|
94
110
|
* @param {import ('./types/index').URIComponent} relative
|
|
@@ -109,7 +125,7 @@ function resolveComponent (base, relative, options, skipNormalization) {
|
|
|
109
125
|
target.scheme = relative.scheme
|
|
110
126
|
// target.authority = relative.authority;
|
|
111
127
|
target.userinfo = relative.userinfo
|
|
112
|
-
target
|
|
128
|
+
copyHost(target, relative)
|
|
113
129
|
target.port = relative.port
|
|
114
130
|
target.path = removeDotSegments(relative.path || '')
|
|
115
131
|
target.query = relative.query
|
|
@@ -117,7 +133,7 @@ function resolveComponent (base, relative, options, skipNormalization) {
|
|
|
117
133
|
if (relative.userinfo !== undefined || relative.host !== undefined || relative.port !== undefined) {
|
|
118
134
|
// target.authority = relative.authority;
|
|
119
135
|
target.userinfo = relative.userinfo
|
|
120
|
-
target
|
|
136
|
+
copyHost(target, relative)
|
|
121
137
|
target.port = relative.port
|
|
122
138
|
target.path = removeDotSegments(relative.path || '')
|
|
123
139
|
target.query = relative.query
|
|
@@ -146,7 +162,7 @@ function resolveComponent (base, relative, options, skipNormalization) {
|
|
|
146
162
|
}
|
|
147
163
|
// target.authority = base.authority;
|
|
148
164
|
target.userinfo = base.userinfo
|
|
149
|
-
target
|
|
165
|
+
copyHost(target, base)
|
|
150
166
|
target.port = base.port
|
|
151
167
|
}
|
|
152
168
|
target.scheme = base.scheme
|
|
@@ -178,6 +194,7 @@ function equal (uriA, uriB, options) {
|
|
|
178
194
|
function serialize (cmpts, opts) {
|
|
179
195
|
const component = {
|
|
180
196
|
host: cmpts.host,
|
|
197
|
+
ipv6Zone: cmpts.ipv6Zone,
|
|
181
198
|
scheme: cmpts.scheme,
|
|
182
199
|
userinfo: cmpts.userinfo,
|
|
183
200
|
port: cmpts.port,
|
|
@@ -506,6 +523,16 @@ function parseWithStatus (uri, opts) {
|
|
|
506
523
|
malformedIPLiteral = hasIPLiteralBracket && (!bracketedIPLiteral || ipv6result.error === true)
|
|
507
524
|
parsed.host = isIP ? ipv6result.host : ipv6result.host.toLowerCase()
|
|
508
525
|
|
|
526
|
+
if (isIP && ipv6result.isIPV6 === true && parsed.host.indexOf('%') !== -1) {
|
|
527
|
+
// Persist the zone identifier so recomposition never has to re-derive
|
|
528
|
+
// it from the ambiguous single-"%" component form, which misreads a
|
|
529
|
+
// zone that begins with "25" as a %25 separator. It is validated on
|
|
530
|
+
// use during recomposition, so a host reassigned after parse or an
|
|
531
|
+
// input zone that does not describe the host is ignored. Only zoned
|
|
532
|
+
// literals are ambiguous; plain IPv6 addresses round-trip without it.
|
|
533
|
+
parsed.ipv6Zone = parsed.host.slice(parsed.host.indexOf('%') + 1)
|
|
534
|
+
}
|
|
535
|
+
|
|
509
536
|
if (malformedIPLiteral) {
|
|
510
537
|
parsed.error = parsed.error || 'URI host is malformed.'
|
|
511
538
|
malformedAuthorityOrPort = true
|
|
@@ -537,13 +564,17 @@ function parseWithStatus (uri, opts) {
|
|
|
537
564
|
malformedHost = canonicalizeHost(parsed, options, schemeHandler, isIP)
|
|
538
565
|
}
|
|
539
566
|
|
|
540
|
-
if (
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
567
|
+
if (uri.indexOf('%') !== -1 && parsed.host !== undefined && !malformedIPLiteral) {
|
|
568
|
+
let host = isIP ? parsed.host : normalizePercentEncoding(parsed.host, true)
|
|
569
|
+
if (!isIP) {
|
|
570
|
+
// Fold reg-name case after decoding unreserved octets. The second
|
|
571
|
+
// pass only restores uppercase hex in escapes that remain encoded.
|
|
572
|
+
host = normalizePercentEncoding(host.toLowerCase())
|
|
546
573
|
}
|
|
574
|
+
parsed.host = reescapeHostDelimiters(host, isIP)
|
|
575
|
+
}
|
|
576
|
+
|
|
577
|
+
if (!schemeHandler || (schemeHandler && !schemeHandler.skipNormalize)) {
|
|
547
578
|
if (parsed.path) {
|
|
548
579
|
parsed.path = normalizePathEncoding(parsed.path)
|
|
549
580
|
}
|
package/lib/schemes.js
CHANGED
|
@@ -495,23 +495,24 @@ function mailtoParse (component, options) {
|
|
|
495
495
|
mailtoComponent.error = mailtoComponent.error || 'URI mailto has malformed header fields.'
|
|
496
496
|
continue
|
|
497
497
|
}
|
|
498
|
-
const name = eqIdx === -1 ? token : token.slice(0, eqIdx)
|
|
498
|
+
const name = decodeHex(eqIdx === -1 ? token : token.slice(0, eqIdx))
|
|
499
|
+
const normalizedName = name.toLowerCase()
|
|
499
500
|
const value = eqIdx === -1 ? '' : token.slice(eqIdx + 1)
|
|
500
|
-
if (
|
|
501
|
+
if (normalizedName === 'to') {
|
|
501
502
|
const addrs = value.split(',')
|
|
502
503
|
for (let j = 0; j < addrs.length; j++) to.push(addrs[j])
|
|
503
504
|
continue
|
|
504
505
|
}
|
|
505
|
-
if (
|
|
506
|
+
if (normalizedName === 'subject') {
|
|
506
507
|
mailtoComponent.subject = decodeHex(value)
|
|
507
508
|
continue
|
|
508
509
|
}
|
|
509
|
-
if (
|
|
510
|
+
if (normalizedName === 'body') {
|
|
510
511
|
mailtoComponent.body = decodeHex(value)
|
|
511
512
|
continue
|
|
512
513
|
}
|
|
513
514
|
if (headers === null) headers = /** @type {Record<string,string>} */ (Object.create(null))
|
|
514
|
-
headers[
|
|
515
|
+
headers[name] = decodeHex(value)
|
|
515
516
|
}
|
|
516
517
|
if (headers !== null) mailtoComponent.headers = headers
|
|
517
518
|
}
|
|
@@ -536,6 +537,18 @@ function mailtoParse (component, options) {
|
|
|
536
537
|
return mailtoComponent
|
|
537
538
|
}
|
|
538
539
|
|
|
540
|
+
/**
|
|
541
|
+
* Encodes a decoded mailto header name. Unlike `encodeWithAllow`, a percent
|
|
542
|
+
* sign is always data here, even when followed by two hex digits. Preserving
|
|
543
|
+
* such a sequence as an existing escape would decode the name again on the
|
|
544
|
+
* next parse and could turn a generic header into `to`, `subject`, or `body`.
|
|
545
|
+
* @param {string} name
|
|
546
|
+
* @returns {string}
|
|
547
|
+
*/
|
|
548
|
+
function mailtoEncodeHeaderName (name) {
|
|
549
|
+
return encodeWithAllow(name.replace(/%/gu, '%25'), HFNAME)
|
|
550
|
+
}
|
|
551
|
+
|
|
539
552
|
/**
|
|
540
553
|
* @param {import('../types/index').URIComponent} component
|
|
541
554
|
* @param {import('../types/index').Options} options
|
|
@@ -544,6 +557,31 @@ function mailtoParse (component, options) {
|
|
|
544
557
|
function mailtoSerialize (component, options) {
|
|
545
558
|
const mailtoComponent = component
|
|
546
559
|
const to = Array.isArray(mailtoComponent.to) ? mailtoComponent.to.slice() : []
|
|
560
|
+
const sourceHeaders = mailtoComponent.headers && typeof mailtoComponent.headers === 'object'
|
|
561
|
+
? Object.assign(Object.create(null), mailtoComponent.headers)
|
|
562
|
+
: Object.create(null)
|
|
563
|
+
const headers = Object.create(null)
|
|
564
|
+
|
|
565
|
+
// Header field names are case-insensitive. Canonicalize the fields with
|
|
566
|
+
// dedicated component properties so callers cannot smuggle a second spelling
|
|
567
|
+
// through `headers`. As during parsing, all `to` fields are accumulated;
|
|
568
|
+
// repeated subject/body fields use the last value.
|
|
569
|
+
for (const name in sourceHeaders) {
|
|
570
|
+
const normalizedName = name.toLowerCase()
|
|
571
|
+
if (normalizedName === 'to') {
|
|
572
|
+
const addrs = String(sourceHeaders[name]).split(',')
|
|
573
|
+
for (let i = 0; i < addrs.length; i++) to.push(addrs[i])
|
|
574
|
+
} else if (normalizedName === 'subject' || normalizedName === 'body') {
|
|
575
|
+
headers[normalizedName] = sourceHeaders[name]
|
|
576
|
+
} else {
|
|
577
|
+
headers[name] = sourceHeaders[name]
|
|
578
|
+
}
|
|
579
|
+
}
|
|
580
|
+
|
|
581
|
+
if (mailtoComponent.subject !== undefined) headers.subject = mailtoComponent.subject
|
|
582
|
+
if (mailtoComponent.body !== undefined) headers.body = mailtoComponent.body
|
|
583
|
+
mailtoComponent.headers = headers
|
|
584
|
+
|
|
547
585
|
if (to.length) {
|
|
548
586
|
for (let i = 0; i < to.length; i++) {
|
|
549
587
|
const addr = String(to[i])
|
|
@@ -570,21 +608,13 @@ function mailtoSerialize (component, options) {
|
|
|
570
608
|
mailtoComponent.path = undefined
|
|
571
609
|
}
|
|
572
610
|
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
: Object.create(null)
|
|
576
|
-
|
|
577
|
-
if (mailtoComponent.subject) headers.subject = mailtoComponent.subject
|
|
578
|
-
if (mailtoComponent.body) headers.body = mailtoComponent.body
|
|
579
|
-
mailtoComponent.headers = headers
|
|
580
|
-
|
|
581
|
-
// `headers` has a null prototype and was filled via Object.assign, so every
|
|
582
|
-
// enumerable key is an own property and no hasOwnProperty guard is needed.
|
|
611
|
+
// `headers` has a null prototype, so every enumerable key is an own property
|
|
612
|
+
// and no hasOwnProperty guard is needed.
|
|
583
613
|
let query = ''
|
|
584
614
|
let count = 0
|
|
585
615
|
for (const name in headers) {
|
|
586
616
|
if (count++ !== 0) query += '&'
|
|
587
|
-
query +=
|
|
617
|
+
query += mailtoEncodeHeaderName(name) + '=' + encodeWithAllow(String(headers[name]), HFNAME)
|
|
588
618
|
}
|
|
589
619
|
|
|
590
620
|
if (count !== 0) {
|
package/lib/utils.js
CHANGED
|
@@ -740,6 +740,37 @@ function escapePreservingEscapes (input) {
|
|
|
740
740
|
return output
|
|
741
741
|
}
|
|
742
742
|
|
|
743
|
+
/**
|
|
744
|
+
* Rebuilds the bracketed form of a zoned IPv6 authority from the current
|
|
745
|
+
* `component.host` and its zone identifier (`component.ipv6Zone`), but only
|
|
746
|
+
* when they are consistent and describe a real zoned IPv6 literal that
|
|
747
|
+
* normalizes back to `component.host`. This validates the zone on use, so a
|
|
748
|
+
* caller-supplied `ipv6Zone` that does not match the host (or a host
|
|
749
|
+
* reassigned after `parse`) is never trusted and recomposition falls back to
|
|
750
|
+
* the normal path.
|
|
751
|
+
*
|
|
752
|
+
* @param {import('../types/index').URIComponent} component
|
|
753
|
+
* @returns {string|undefined}
|
|
754
|
+
*/
|
|
755
|
+
function recomposeZonedIPv6Host (component) {
|
|
756
|
+
const zone = component.ipv6Zone
|
|
757
|
+
// Without an ipv6Zone the single-"%" host form is genuinely ambiguous when
|
|
758
|
+
// the zone begins with "25", so we fall through to the best-effort heuristic
|
|
759
|
+
// (which matches the pre-existing behavior for hand-built components that do
|
|
760
|
+
// not carry a zone).
|
|
761
|
+
if (zone === undefined) return undefined
|
|
762
|
+
const host = component.host
|
|
763
|
+
const separator = host.indexOf('%')
|
|
764
|
+
if (separator === -1) return undefined
|
|
765
|
+
const address = host.slice(0, separator)
|
|
766
|
+
const escaped = address + '%25' + zone
|
|
767
|
+
const derived = normalizeIPv6('[' + escaped + ']')
|
|
768
|
+
if (derived.isIPV6 === true && derived.host === host) {
|
|
769
|
+
return '[' + escaped + ']'
|
|
770
|
+
}
|
|
771
|
+
return undefined
|
|
772
|
+
}
|
|
773
|
+
|
|
743
774
|
/**
|
|
744
775
|
* @param {import('../types/index').URIComponent} component
|
|
745
776
|
* @returns {string|undefined}
|
|
@@ -755,17 +786,30 @@ function recomposeAuthority (component) {
|
|
|
755
786
|
if (component.host !== undefined) {
|
|
756
787
|
let host = component.host
|
|
757
788
|
if (!isIPv4(host)) {
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
789
|
+
// A parse-time zone identifier is authoritative only while it still
|
|
790
|
+
// describes the current host. Re-deriving an IPv6 zone from the
|
|
791
|
+
// single-"%" component form is ambiguous when the zone begins with
|
|
792
|
+
// "25": it is misread as a %25 separator, rewriting e.g. zone
|
|
793
|
+
// "25eth0" into "eth0" (or destroying a zone of just "25"). A
|
|
794
|
+
// validated zone keeps parse, normalize, serialize and equal in
|
|
795
|
+
// agreement.
|
|
796
|
+
const zonedHost = recomposeZonedIPv6Host(component)
|
|
797
|
+
if (zonedHost !== undefined) {
|
|
798
|
+
host = zonedHost
|
|
767
799
|
} else {
|
|
768
|
-
|
|
800
|
+
let ipV6res = normalizeIPv6(host)
|
|
801
|
+
if (ipV6res.isIPV6 !== true && ipV6res.isIPVFuture !== true) {
|
|
802
|
+
// Decode only unreserved bytes, once. In particular, keep %25
|
|
803
|
+
// encoded so it cannot introduce a second escape during
|
|
804
|
+
// recomposition.
|
|
805
|
+
host = normalizePercentEncoding(host, true)
|
|
806
|
+
ipV6res = normalizeIPv6(host)
|
|
807
|
+
}
|
|
808
|
+
if (ipV6res.isIPV6 === true || ipV6res.isIPVFuture === true) {
|
|
809
|
+
host = `[${ipV6res.escapedHost}]`
|
|
810
|
+
} else {
|
|
811
|
+
host = reescapeHostDelimiters(host, false)
|
|
812
|
+
}
|
|
769
813
|
}
|
|
770
814
|
}
|
|
771
815
|
uriTokens.push(host)
|
package/package.json
CHANGED
|
@@ -119,6 +119,136 @@ test('IPv6 zone identifiers are validated correctly', (t) => {
|
|
|
119
119
|
t.end()
|
|
120
120
|
})
|
|
121
121
|
|
|
122
|
+
test('zoned IPv6 hosts beginning with "25" round-trip through parse/normalize/serialize/equal', (t) => {
|
|
123
|
+
const cases = [
|
|
124
|
+
// zones beginning with "25" (once ambiguous with a %25 separator)
|
|
125
|
+
['http://[fe80::1%2525]/', 'fe80::1%25', 'http://[fe80::1%2525]/'],
|
|
126
|
+
['http://[fe80::1%2525eth0]/', 'fe80::1%25eth0', 'http://[fe80::1%2525eth0]/'],
|
|
127
|
+
['http://[fe80::1%2525en1]/', 'fe80::1%25en1', 'http://[fe80::1%2525en1]/'],
|
|
128
|
+
// zones that do not begin with "25" must stay idempotent
|
|
129
|
+
['http://[fe80::1%25eth0]/', 'fe80::1%eth0', 'http://[fe80::1%25eth0]/'],
|
|
130
|
+
['http://[fe80::a%en1]/', 'fe80::a%en1', 'http://[fe80::a%25en1]/'],
|
|
131
|
+
['http://[fe80::a%25eth%2D0]/', 'fe80::a%eth%2D0', 'http://[fe80::a%25eth%2D0]/']
|
|
132
|
+
]
|
|
133
|
+
|
|
134
|
+
for (const [input, host, normalized] of cases) {
|
|
135
|
+
const parsed = fastURI.parse(input)
|
|
136
|
+
t.equal(parsed.error, undefined, `${input} parses without error`)
|
|
137
|
+
t.equal(parsed.host, host, `${input} exposes the expected host`)
|
|
138
|
+
t.equal(fastURI.normalize(input), normalized, `${input} normalizes to the canonical form`)
|
|
139
|
+
t.equal(fastURI.serialize(parsed), normalized, `${input} serializes to the canonical form`)
|
|
140
|
+
const reparsed = fastURI.parse(normalized)
|
|
141
|
+
t.equal(reparsed.error, undefined, `${input} canonical output reparses without error`)
|
|
142
|
+
t.equal(reparsed.host, host, `${input} parse-after-normalize is stable`)
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
// distinct zones must not be conflated by equal
|
|
146
|
+
t.equal(
|
|
147
|
+
fastURI.equal('http://[fe80::1%2525eth0]/', 'http://[fe80::1%25eth0]/'),
|
|
148
|
+
false,
|
|
149
|
+
'equal distinguishes zones "25eth0" and "eth0"'
|
|
150
|
+
)
|
|
151
|
+
t.equal(
|
|
152
|
+
fastURI.equal('http://[fe80::1%2525]/', 'http://[fe80::1%25eth0]/'),
|
|
153
|
+
false,
|
|
154
|
+
'equal distinguishes zones "25" and "eth0"'
|
|
155
|
+
)
|
|
156
|
+
// the same zone still compares equal
|
|
157
|
+
t.equal(
|
|
158
|
+
fastURI.equal('http://[fe80::1%2525eth0]/', 'http://[fe80::1%2525eth0]/'),
|
|
159
|
+
true,
|
|
160
|
+
'an identical zoned authority is equal'
|
|
161
|
+
)
|
|
162
|
+
t.equal(
|
|
163
|
+
fastURI.equal('http://[fe80::1%2525]/', 'http://[fe80::1%2525]/'),
|
|
164
|
+
true,
|
|
165
|
+
'zone "25" equals itself'
|
|
166
|
+
)
|
|
167
|
+
|
|
168
|
+
// resolve must preserve the zone too
|
|
169
|
+
t.equal(
|
|
170
|
+
fastURI.resolve('http://example.com/', 'http://[fe80::1%2525eth0]/path'),
|
|
171
|
+
'http://[fe80::1%2525eth0]/path',
|
|
172
|
+
'resolve preserves a zone beginning with "25"'
|
|
173
|
+
)
|
|
174
|
+
t.equal(
|
|
175
|
+
fastURI.resolve('http://[fe80::1%2525eth0]/', 'child'),
|
|
176
|
+
'http://[fe80::1%2525eth0]/child',
|
|
177
|
+
'resolve preserves a zoned base'
|
|
178
|
+
)
|
|
179
|
+
|
|
180
|
+
t.end()
|
|
181
|
+
})
|
|
182
|
+
|
|
183
|
+
test('serialize only trusts the parse-derived IPv6 zone while the host is unchanged', (t) => {
|
|
184
|
+
// Reassigning `host` after parse must take effect for serialize.
|
|
185
|
+
const parsed = fastURI.parse('http://[::1]/admin')
|
|
186
|
+
parsed.host = 'example.com'
|
|
187
|
+
t.equal(
|
|
188
|
+
fastURI.serialize(parsed),
|
|
189
|
+
'http://example.com/admin',
|
|
190
|
+
'a host reassigned after parse wins over the stale zone'
|
|
191
|
+
)
|
|
192
|
+
|
|
193
|
+
// A caller-supplied `ipv6Zone` that does not match `host` must be ignored.
|
|
194
|
+
const forged = fastURI.serialize({
|
|
195
|
+
scheme: 'http',
|
|
196
|
+
host: 'fe80::1%25en1',
|
|
197
|
+
ipv6Zone: 'eth0',
|
|
198
|
+
path: '/'
|
|
199
|
+
})
|
|
200
|
+
t.equal(forged, 'http://[fe80::1%25en1]/', 'a mismatched ipv6Zone is ignored')
|
|
201
|
+
t.equal(fastURI.parse(forged).host, 'fe80::1%en1', 'mismatched ipv6Zone cannot redirect the parsed host')
|
|
202
|
+
|
|
203
|
+
// A caller-supplied `ipv6Zone` on a non-zoned host must be ignored too.
|
|
204
|
+
const noZone = fastURI.serialize({
|
|
205
|
+
scheme: 'http',
|
|
206
|
+
host: 'good.example',
|
|
207
|
+
ipv6Zone: 'eth0',
|
|
208
|
+
path: '/'
|
|
209
|
+
})
|
|
210
|
+
t.equal(noZone, 'http://good.example/', 'ipv6Zone on a non-zoned host is ignored')
|
|
211
|
+
|
|
212
|
+
// Reassigning a zoned host to a different zone must recompute, not reuse.
|
|
213
|
+
const zoned = fastURI.parse('http://[fe80::1%2525eth0]/')
|
|
214
|
+
zoned.host = 'fe80::1%25en1'
|
|
215
|
+
t.equal(
|
|
216
|
+
fastURI.serialize(zoned),
|
|
217
|
+
'http://[fe80::1%25en1]/',
|
|
218
|
+
'rewriting the zone recomposes from the new host'
|
|
219
|
+
)
|
|
220
|
+
|
|
221
|
+
// A consistent caller-supplied zone is honored (explicit disambiguation).
|
|
222
|
+
const disambiguated = fastURI.serialize({
|
|
223
|
+
scheme: 'http',
|
|
224
|
+
host: 'fe80::1%25eth0',
|
|
225
|
+
ipv6Zone: '25eth0',
|
|
226
|
+
path: '/'
|
|
227
|
+
})
|
|
228
|
+
t.equal(
|
|
229
|
+
disambiguated,
|
|
230
|
+
'http://[fe80::1%2525eth0]/',
|
|
231
|
+
'a consistent ipv6Zone disambiguates the component host'
|
|
232
|
+
)
|
|
233
|
+
|
|
234
|
+
// Without ipv6Zone, an ambiguous single-"%" host falls back to the
|
|
235
|
+
// best-effort heuristic (documented behavior for hand-built components).
|
|
236
|
+
t.equal(
|
|
237
|
+
fastURI.serialize({ host: 'fe80::1%25eth0' }),
|
|
238
|
+
'//[fe80::1%25eth0]',
|
|
239
|
+
'an ambiguous host without ipv6Zone uses the best-effort heuristic'
|
|
240
|
+
)
|
|
241
|
+
|
|
242
|
+
// Mutating the host must not poison resolve either.
|
|
243
|
+
t.equal(
|
|
244
|
+
fastURI.resolve('http://example.com/', 'http://[fe80::1%2525eth0]/path'),
|
|
245
|
+
'http://[fe80::1%2525eth0]/path',
|
|
246
|
+
'resolve preserves the parse-derived zone'
|
|
247
|
+
)
|
|
248
|
+
|
|
249
|
+
t.end()
|
|
250
|
+
})
|
|
251
|
+
|
|
122
252
|
test('hosts with unbalanced or misplaced IP-literal brackets are rejected', (t) => {
|
|
123
253
|
const malformed = [
|
|
124
254
|
'http://[fe80',
|
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
const test = require('tape')
|
|
4
|
+
const fastURI = require('..')
|
|
5
|
+
|
|
6
|
+
function encodeCharacter (value, index, lowercaseHex) {
|
|
7
|
+
let hex = value.charCodeAt(index).toString(16)
|
|
8
|
+
if (!lowercaseHex) hex = hex.toUpperCase()
|
|
9
|
+
return value.slice(0, index) + '%' + hex + value.slice(index + 1)
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
function mailtoState (component) {
|
|
13
|
+
return {
|
|
14
|
+
to: component.to,
|
|
15
|
+
subject: component.subject,
|
|
16
|
+
body: component.body,
|
|
17
|
+
headers: component.headers && { ...component.headers }
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
test('mailto parsing classifies decoded, case-insensitive reserved field names', (t) => {
|
|
22
|
+
const cases = [
|
|
23
|
+
{ name: 'to', value: 'attacker@evil.test', property: 'to' },
|
|
24
|
+
{ name: 'subject', value: 'injected subject', property: 'subject' },
|
|
25
|
+
{ name: 'body', value: 'injected body', property: 'body' }
|
|
26
|
+
]
|
|
27
|
+
|
|
28
|
+
for (const entry of cases) {
|
|
29
|
+
const variants = new Set([entry.name.toUpperCase()])
|
|
30
|
+
for (let i = 0; i < entry.name.length; i++) {
|
|
31
|
+
variants.add(encodeCharacter(entry.name, i, false))
|
|
32
|
+
variants.add(encodeCharacter(entry.name, i, true))
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
for (const name of variants) {
|
|
36
|
+
const parsed = fastURI.parse(`mailto:alice@example.test?${name}=${entry.value}`)
|
|
37
|
+
if (entry.property === 'to') {
|
|
38
|
+
t.deepEqual(parsed.to, ['alice@example.test', entry.value], name + ' is classified as a recipient')
|
|
39
|
+
} else {
|
|
40
|
+
t.equal(parsed[entry.property], entry.value, name + ' is classified as ' + entry.property)
|
|
41
|
+
}
|
|
42
|
+
t.equal(parsed.headers, undefined, name + ' is not exposed as a generic header')
|
|
43
|
+
|
|
44
|
+
const reparsed = fastURI.parse(fastURI.serialize(parsed))
|
|
45
|
+
t.deepEqual(mailtoState(reparsed), mailtoState(parsed), name + ' keeps its meaning after a roundtrip')
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
t.end()
|
|
50
|
+
})
|
|
51
|
+
|
|
52
|
+
test('mailto generic field names are decoded exactly once across roundtrips', (t) => {
|
|
53
|
+
const cases = [
|
|
54
|
+
['%2574o', '%74o'],
|
|
55
|
+
['%2573ubject', '%73ubject'],
|
|
56
|
+
['%2562ody', '%62ody'],
|
|
57
|
+
['x%2520y', 'x%20y'],
|
|
58
|
+
['%26to', '&to'],
|
|
59
|
+
['%25GGto', '%GGto']
|
|
60
|
+
]
|
|
61
|
+
|
|
62
|
+
for (const [encodedName, decodedName] of cases) {
|
|
63
|
+
let serialized = `mailto:alice@example.test?${encodedName}=value`
|
|
64
|
+
for (let round = 0; round < 3; round++) {
|
|
65
|
+
const parsed = fastURI.parse(serialized)
|
|
66
|
+
t.deepEqual({ ...parsed.headers }, { [decodedName]: 'value' }, encodedName + ' stays generic in round ' + round)
|
|
67
|
+
t.deepEqual(parsed.to, ['alice@example.test'], encodedName + ' does not add a recipient in round ' + round)
|
|
68
|
+
t.equal(parsed.subject, undefined, encodedName + ' does not set a subject in round ' + round)
|
|
69
|
+
t.equal(parsed.body, undefined, encodedName + ' does not set a body in round ' + round)
|
|
70
|
+
serialized = fastURI.serialize(parsed)
|
|
71
|
+
}
|
|
72
|
+
t.equal(serialized, `mailto:alice@example.test?${encodedName}=value`, encodedName + ' serializes idempotently')
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
t.equal(
|
|
76
|
+
fastURI.serialize({ scheme: 'mailto', to: ['alice@example.test'], headers: { '%74o': 'value' } }),
|
|
77
|
+
'mailto:alice@example.test?%2574o=value',
|
|
78
|
+
'a percent sequence in a decoded component key is encoded as data'
|
|
79
|
+
)
|
|
80
|
+
t.end()
|
|
81
|
+
})
|
|
82
|
+
|
|
83
|
+
test('mailto serialization canonicalizes reserved generic header names', (t) => {
|
|
84
|
+
const serialized = fastURI.serialize({
|
|
85
|
+
scheme: 'mailto',
|
|
86
|
+
to: ['alice@example.test'],
|
|
87
|
+
subject: 'safe subject',
|
|
88
|
+
body: 'safe body',
|
|
89
|
+
headers: {
|
|
90
|
+
TO: 'attacker@evil.test',
|
|
91
|
+
Subject: 'hidden subject',
|
|
92
|
+
BODY: 'hidden body',
|
|
93
|
+
cc: 'copy@example.test'
|
|
94
|
+
}
|
|
95
|
+
})
|
|
96
|
+
|
|
97
|
+
t.equal(
|
|
98
|
+
serialized,
|
|
99
|
+
'mailto:alice@example.test,attacker@evil.test?subject=safe%20subject&body=safe%20body&cc=copy@example.test',
|
|
100
|
+
'dedicated properties override header variants and all recipients are visible in the path'
|
|
101
|
+
)
|
|
102
|
+
|
|
103
|
+
const parsed = fastURI.parse(serialized)
|
|
104
|
+
t.deepEqual(parsed.to, ['alice@example.test', 'attacker@evil.test'], 'canonical recipients are classified consistently')
|
|
105
|
+
t.equal(parsed.subject, 'safe subject', 'the dedicated subject wins')
|
|
106
|
+
t.equal(parsed.body, 'safe body', 'the dedicated body wins')
|
|
107
|
+
t.deepEqual({ ...parsed.headers }, { cc: 'copy@example.test' }, 'only generic headers remain generic')
|
|
108
|
+
|
|
109
|
+
const empty = fastURI.serialize({
|
|
110
|
+
scheme: 'mailto',
|
|
111
|
+
to: ['alice@example.test'],
|
|
112
|
+
subject: '',
|
|
113
|
+
body: '',
|
|
114
|
+
headers: { Subject: 'hidden subject', BODY: 'hidden body' }
|
|
115
|
+
})
|
|
116
|
+
t.equal(empty, 'mailto:alice@example.test?subject=&body=', 'explicit empty properties override header variants')
|
|
117
|
+
const emptyParsed = fastURI.parse(empty)
|
|
118
|
+
t.equal(emptyParsed.subject, '', 'an empty subject round-trips')
|
|
119
|
+
t.equal(emptyParsed.body, '', 'an empty body round-trips')
|
|
120
|
+
t.end()
|
|
121
|
+
})
|
|
122
|
+
|
|
123
|
+
test('mailto duplicate reserved fields have deterministic parsing semantics', (t) => {
|
|
124
|
+
const parsed = fastURI.parse(
|
|
125
|
+
'mailto:alice@example.test?subject=first&%53UBJECT=last&body=first&%42ODY=last&to=one@example.test&%54O=two@example.test'
|
|
126
|
+
)
|
|
127
|
+
|
|
128
|
+
t.equal(parsed.subject, 'last', 'the last subject wins')
|
|
129
|
+
t.equal(parsed.body, 'last', 'the last body wins')
|
|
130
|
+
t.deepEqual(
|
|
131
|
+
parsed.to,
|
|
132
|
+
['alice@example.test', 'one@example.test', 'two@example.test'],
|
|
133
|
+
'all recipient fields are accumulated'
|
|
134
|
+
)
|
|
135
|
+
t.equal(parsed.headers, undefined, 'no reserved spelling remains generic')
|
|
136
|
+
t.end()
|
|
137
|
+
})
|
|
138
|
+
|
|
139
|
+
test('encoded reserved field names expose decoded control characters immediately', (t) => {
|
|
140
|
+
const input = 'mailto:alice@example.test?%73ubject=hello%0D%0Aworld'
|
|
141
|
+
const parsed = fastURI.parse(input)
|
|
142
|
+
|
|
143
|
+
t.equal(parsed.subject, 'hello\r\nworld', 'the first parse exposes CRLF in the dedicated property')
|
|
144
|
+
t.equal(parsed.headers, undefined, 'the subject is not hidden as a generic header')
|
|
145
|
+
t.equal(
|
|
146
|
+
fastURI.parse(fastURI.serialize(parsed)).subject,
|
|
147
|
+
parsed.subject,
|
|
148
|
+
'the decoded subject remains stable after a roundtrip'
|
|
149
|
+
)
|
|
150
|
+
t.end()
|
|
151
|
+
})
|
|
@@ -126,6 +126,44 @@ test('hostname normalization decodes only current unreserved escapes', (t) => {
|
|
|
126
126
|
t.end()
|
|
127
127
|
})
|
|
128
128
|
|
|
129
|
+
test('scheme-relative hostname normalization folds decoded ASCII case', (t) => {
|
|
130
|
+
const encodedHost = '//%41.com'
|
|
131
|
+
const normalizedHost = fastURI.normalize(encodedHost)
|
|
132
|
+
const encodedMetadata = '//%4Detadata.internal/private'
|
|
133
|
+
const literalMetadata = '//metadata.internal/private'
|
|
134
|
+
|
|
135
|
+
t.equal(fastURI.parse(encodedHost).host, 'a.com', 'parse folds an encoded uppercase host letter')
|
|
136
|
+
t.equal(normalizedHost, '//a.com', 'normalize emits a lowercase host')
|
|
137
|
+
t.equal(fastURI.normalize(normalizedHost), normalizedHost, 'host normalization is idempotent')
|
|
138
|
+
t.equal(fastURI.equal(encodedHost, '//a.com'), true, 'encoded and literal host spellings compare equal')
|
|
139
|
+
t.equal(fastURI.equal(encodedMetadata, literalMetadata), true, 'encoded metadata host compares equal')
|
|
140
|
+
t.equal(
|
|
141
|
+
fastURI.resolve('x://trusted.example/', encodedMetadata),
|
|
142
|
+
fastURI.resolve('x://trusted.example/', literalMetadata),
|
|
143
|
+
'encoded and literal metadata hosts resolve identically'
|
|
144
|
+
)
|
|
145
|
+
t.equal(
|
|
146
|
+
fastURI.normalize('//example.com%2fpath'),
|
|
147
|
+
'//example.com%2Fpath',
|
|
148
|
+
'reserved host escapes remain encoded with uppercase hex'
|
|
149
|
+
)
|
|
150
|
+
t.equal(fastURI.normalize('//%2541.com'), '//%2541.com', 'nested host escapes are not decoded twice')
|
|
151
|
+
t.equal(fastURI.equal('//User@%41.com/path', '//user@a.com/path'), false, 'userinfo remains case-sensitive')
|
|
152
|
+
t.equal(fastURI.equal('//%41.com/Path', '//a.com/path'), false, 'path remains case-sensitive')
|
|
153
|
+
t.equal(fastURI.equal('//%41.com/?Token=Value', '//a.com/?token=value'), false, 'query remains case-sensitive')
|
|
154
|
+
t.end()
|
|
155
|
+
})
|
|
156
|
+
|
|
157
|
+
test('host normalization applies to schemes that skip path normalization', (t) => {
|
|
158
|
+
const encodedHost = 'mailto://%41.com'
|
|
159
|
+
const literalHost = 'mailto://a.com'
|
|
160
|
+
|
|
161
|
+
t.equal(fastURI.parse(encodedHost).host, fastURI.parse(literalHost).host, 'parse results have consistent hosts')
|
|
162
|
+
t.equal(fastURI.normalize(encodedHost), fastURI.normalize(literalHost), 'normalize results are consistent')
|
|
163
|
+
t.equal(fastURI.equal(encodedHost, literalHost), true, 'encoded and literal hosts compare equal')
|
|
164
|
+
t.end()
|
|
165
|
+
})
|
|
166
|
+
|
|
129
167
|
test('host conversion failures are not treated as comparable URLs', (t) => {
|
|
130
168
|
const malformedHost = 'http://trusted.test%2540evil.test/'
|
|
131
169
|
|
package/types/index.d.ts
CHANGED
|
@@ -5,6 +5,22 @@ declare namespace fastUri {
|
|
|
5
5
|
scheme?: string;
|
|
6
6
|
userinfo?: string;
|
|
7
7
|
host?: string;
|
|
8
|
+
/**
|
|
9
|
+
* Zone identifier of a zoned IPv6 host, e.g. `"eth0"` or `"25eth0"`. Set by
|
|
10
|
+
* `parse` for a bracketed IPv6 literal so `serialize`/`normalize` can
|
|
11
|
+
* reproduce the exact zone without re-deriving it from the ambiguous
|
|
12
|
+
* single-`%` form. It is validated on use, so a value that does not match
|
|
13
|
+
* `host` (for example after `host` is reassigned) is ignored.
|
|
14
|
+
*
|
|
15
|
+
* When building a component object by hand, set `ipv6Zone` to disambiguate
|
|
16
|
+
* a single-`%` host whose zone is ambiguous (a zone beginning with `25`
|
|
17
|
+
* is re-read as a `%25` separator). Without it, `serialize` falls back to
|
|
18
|
+
* a best-effort heuristic and may reinterpret such a zone, e.g. `{ host:
|
|
19
|
+
* "fe80::1%25eth0" }` serializes to `[fe80::1%25eth0]` (zone `eth0`), while
|
|
20
|
+
* `{ host: "fe80::1%25eth0", ipv6Zone: "25eth0" }` serializes to
|
|
21
|
+
* `[fe80::1%2525eth0]` (zone `25eth0`).
|
|
22
|
+
*/
|
|
23
|
+
ipv6Zone?: string;
|
|
8
24
|
port?: number | string;
|
|
9
25
|
path?: string;
|
|
10
26
|
query?: string;
|