fast-uri 3.1.2 → 3.1.4

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/index.js CHANGED
@@ -202,6 +202,10 @@ function serialize (cmpts, opts) {
202
202
 
203
203
  const URI_PARSE = /^(?:([^#/:?]+):)?(?:\/\/((?:([^#/?@]*)@)?(\[[^#/?\]]+\]|[^#/:?]*)(?::(\d*))?))?([^#?]*)(?:\?([^#]*))?(?:#((?:.|[\n\r])*))?/u
204
204
 
205
+ // Captures the authority component (between "//" and the next "/", "?" or "#"),
206
+ // with or without a scheme prefix, for the literal-backslash rejection below.
207
+ const AUTHORITY_PREFIX = /^(?:[^#/:?]+:)?\/\/([^/?#]*)/
208
+
205
209
  /**
206
210
  * @param {import('./types/index').URIComponent} parsed
207
211
  * @param {RegExpMatchArray} matches
@@ -248,6 +252,19 @@ function parseWithStatus (uri, opts) {
248
252
  }
249
253
  }
250
254
 
255
+ // A literal backslash (U+005C) is not a valid RFC 3986 URI character and is
256
+ // not an authority delimiter. Reject it in the authority rather than
257
+ // rewriting it: normalizing "\" -> "/" (WHATWG error recovery) could silently
258
+ // change the resource identified by an otherwise-invalid input, and lets "\"
259
+ // act as a host delimiter here while Node's native URL parses a different
260
+ // host (SSRF / redirect / origin-allowlist bypass). Percent-encoded %5C is
261
+ // untouched and remains valid encoded data.
262
+ const authorityMatch = uri.match(AUTHORITY_PREFIX)
263
+ if (authorityMatch !== null && authorityMatch[1].indexOf('\\') !== -1) {
264
+ parsed.error = 'URI authority must not contain a literal backslash.'
265
+ malformedAuthorityOrPort = true
266
+ }
267
+
251
268
  const matches = uri.match(URI_PARSE)
252
269
 
253
270
  if (matches) {
@@ -305,7 +322,7 @@ function parseWithStatus (uri, opts) {
305
322
  if (parsed.host && (options.domainHost || (schemeHandler && schemeHandler.domainHost)) && isIP === false && nonSimpleDomain(parsed.host)) {
306
323
  // convert Unicode IDN -> ASCII IDN
307
324
  try {
308
- parsed.host = URL.domainToASCII(parsed.host.toLowerCase())
325
+ parsed.host = new URL('http://' + parsed.host).hostname
309
326
  } catch (e) {
310
327
  parsed.error = parsed.error || "Host's domain name can not be converted to ASCII: " + e
311
328
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "fast-uri",
3
3
  "description": "Dependency-free RFC 3986 URI toolbox",
4
- "version": "3.1.2",
4
+ "version": "3.1.4",
5
5
  "main": "index.js",
6
6
  "type": "commonjs",
7
7
  "types": "types/index.d.ts",
@@ -131,3 +131,93 @@ test('normalize does not double-decode %2540 into a live @', (t) => {
131
131
  t.plan(1)
132
132
  t.notEqual(parsed.host, 'trusted.com@evil.com', 'http://trusted.com%2540evil.com/')
133
133
  })
134
+
135
+ test('parse canonicalises IDN / Unicode hosts to their ASCII form', (t) => {
136
+ const cases = [
137
+ {
138
+ input: 'http://127。0。0。1/',
139
+ expectedHost: '127.0.0.1',
140
+ description: 'full-width ideographic stops as octet separators'
141
+ },
142
+ {
143
+ input: 'http://example.com/',
144
+ expectedHost: 'example.com',
145
+ description: 'fullwidth e as first letter'
146
+ },
147
+ {
148
+ input: 'http://納豆.example.org/',
149
+ expectedHost: 'xn--99zt52a.example.org',
150
+ description: 'CJK label requiring punycode'
151
+ }
152
+ ]
153
+
154
+ t.plan(cases.length * 2)
155
+
156
+ cases.forEach(({ input, expectedHost, description }) => {
157
+ const parsed = fastURI.parse(input)
158
+ t.notOk(parsed.error, `parse should not set error: ${description}`)
159
+ t.equal(parsed.host, expectedHost, `host canonicalised to ASCII: ${description}`)
160
+ })
161
+ })
162
+
163
+ test('parse rejects a literal backslash in the authority as malformed (RFC 3986)', (t) => {
164
+ // Regression for the host-confusion bypass: a literal "\" is invalid RFC 3986
165
+ // syntax and must be flagged malformed, not silently rewritten. Otherwise "\"
166
+ // acts as a host delimiter here while Node's native URL parses a different
167
+ // host, defeating a host-based SSRF/redirect/origin allowlist.
168
+ const cases = [
169
+ 'http://evil.com\\@allowed.com',
170
+ 'https://169.254.169.254\\@trusted.example.com',
171
+ 'http://127.0.0.1\\@public.example.com',
172
+ 'https://attacker.com\\@api.internal',
173
+ 'http://a\\@b',
174
+ 'ws://evil.com\\@allowed.com/chat',
175
+ 'wss://evil.com\\@allowed.com/chat',
176
+ 'http://evil.com\\%40allowed.com',
177
+ '//evil.com\\@allowed.com'
178
+ ]
179
+
180
+ t.plan(cases.length)
181
+
182
+ cases.forEach((input) => {
183
+ t.equal(
184
+ fastURI.parse(input).error,
185
+ 'URI authority must not contain a literal backslash.',
186
+ input
187
+ )
188
+ })
189
+ })
190
+
191
+ test('normalize does not canonicalize a literal-backslash URI into a different valid URL', (t) => {
192
+ const cases = [
193
+ 'http://evil.com\\@allowed.com',
194
+ 'https://attacker.com\\@api.internal'
195
+ ]
196
+
197
+ t.plan(cases.length)
198
+
199
+ cases.forEach((input) => {
200
+ t.equal(fastURI.normalize(input), input, input)
201
+ })
202
+ })
203
+
204
+ test('parse leaves percent-encoded %5C untouched as encoded data (not rejected)', (t) => {
205
+ // Only the literal "\" byte is rejected; %5C stays valid encoded data and
206
+ // does not diverge from the native URL parser, so it must not be flagged.
207
+ const input = 'http://evil.com%5C@allowed.com'
208
+ const parsed = fastURI.parse(input)
209
+
210
+ t.plan(2)
211
+ t.notOk(parsed.error, '%5C is valid encoded data, not malformed')
212
+ t.equal(parsed.host, new URL(input).hostname, '%5C host matches native URL (no divergence)')
213
+ })
214
+
215
+ test('parse does not reject a literal backslash in the query or fragment', (t) => {
216
+ // The rejection is scoped to the authority/path (the host-confusion surface);
217
+ // a backslash after "?"/"#" is normalized as encoded data as before.
218
+ const parsed = fastURI.parse('http://host.example.com/?x=\\y#z\\w')
219
+
220
+ t.plan(2)
221
+ t.notOk(parsed.error, 'backslash in query/fragment does not mark the URI malformed')
222
+ t.equal(parsed.host, 'host.example.com', 'host parsed normally')
223
+ })