fast-uri 2.4.2 → 2.4.3

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.
@@ -17,6 +17,9 @@ on:
17
17
 
18
18
  jobs:
19
19
  test:
20
+ permissions:
21
+ contents: write
22
+ pull-requests: write
20
23
  uses: fastify/workflows/.github/workflows/plugins-ci.yml@v3
21
24
  with:
22
25
  license-check: true
package/index.js CHANGED
@@ -181,6 +181,10 @@ function nonSimpleDomain (value) {
181
181
 
182
182
  const URI_PARSE = /^(?:([^#/:?]+):)?(?:\/\/((?:([^#/?@]*)@)?(\[[^#/?\]]+\]|[^#/:?]*)(?::(\d*))?))?([^#?]*)(?:\?([^#]*))?(?:#((?:.|[\n\r])*))?/u
183
183
 
184
+ // Captures the authority component (between "//" and the next "/", "?" or "#"),
185
+ // with or without a scheme prefix, for the literal-backslash rejection below.
186
+ const AUTHORITY_PREFIX = /^(?:[^#/:?]+:)?\/\/([^/?#]*)/
187
+
184
188
  function getParseError (parsed, matches) {
185
189
  if (matches[2] !== undefined && parsed.path && parsed.path[0] !== '/') {
186
190
  return 'URI path must start with "/" when authority is present.'
@@ -209,6 +213,19 @@ function parseWithStatus (uri, opts) {
209
213
  let isIP = false
210
214
  if (options.reference === 'suffix') uri = (options.scheme ? options.scheme + ':' : '') + '//' + uri
211
215
 
216
+ // A literal backslash (U+005C) is not a valid RFC 3986 URI character and is
217
+ // not an authority delimiter. Reject it in the authority rather than
218
+ // rewriting it: normalizing "\" -> "/" (WHATWG error recovery) could silently
219
+ // change the resource identified by an otherwise-invalid input, and lets "\"
220
+ // act as a host delimiter here while Node's native URL parses a different
221
+ // host (SSRF / redirect / origin-allowlist bypass). Percent-encoded %5C is
222
+ // untouched and remains valid encoded data.
223
+ const authorityMatch = uri.match(AUTHORITY_PREFIX)
224
+ if (authorityMatch !== null && authorityMatch[1].indexOf('\\') !== -1) {
225
+ parsed.error = 'URI authority must not contain a literal backslash.'
226
+ malformedAuthorityOrPort = true
227
+ }
228
+
212
229
  const matches = uri.match(URI_PARSE)
213
230
 
214
231
  if (matches) {
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": "2.4.2",
4
+ "version": "2.4.3",
5
5
  "main": "index.js",
6
6
  "type": "commonjs",
7
7
  "types": "types/index.d.ts",
@@ -160,3 +160,65 @@ test('parse canonicalises IDN / Unicode hosts to their ASCII form', (t) => {
160
160
  t.equal(parsed.host, expectedHost, `host canonicalised to ASCII: ${description}`)
161
161
  })
162
162
  })
163
+
164
+ test('parse rejects a literal backslash in the authority as malformed (RFC 3986)', (t) => {
165
+ // Regression for the host-confusion bypass: a literal "\" is invalid RFC 3986
166
+ // syntax and must be flagged malformed, not silently rewritten. Otherwise "\"
167
+ // acts as a host delimiter here while Node's native URL parses a different
168
+ // host, defeating a host-based SSRF/redirect/origin allowlist.
169
+ const cases = [
170
+ 'http://evil.com\\@allowed.com',
171
+ 'https://169.254.169.254\\@trusted.example.com',
172
+ 'http://127.0.0.1\\@public.example.com',
173
+ 'https://attacker.com\\@api.internal',
174
+ 'http://a\\@b',
175
+ 'ws://evil.com\\@allowed.com/chat',
176
+ 'wss://evil.com\\@allowed.com/chat',
177
+ 'http://evil.com\\%40allowed.com',
178
+ '//evil.com\\@allowed.com'
179
+ ]
180
+
181
+ t.plan(cases.length)
182
+
183
+ cases.forEach((input) => {
184
+ t.equal(
185
+ fastURI.parse(input).error,
186
+ 'URI authority must not contain a literal backslash.',
187
+ input
188
+ )
189
+ })
190
+ })
191
+
192
+ test('normalize does not canonicalize a literal-backslash URI into a different valid URL', (t) => {
193
+ const cases = [
194
+ 'http://evil.com\\@allowed.com',
195
+ 'https://attacker.com\\@api.internal'
196
+ ]
197
+
198
+ t.plan(cases.length)
199
+
200
+ cases.forEach((input) => {
201
+ t.equal(fastURI.normalize(input), input, input)
202
+ })
203
+ })
204
+
205
+ test('parse leaves percent-encoded %5C untouched as encoded data (not rejected)', (t) => {
206
+ // Only the literal "\" byte is rejected; %5C stays valid encoded data and
207
+ // does not diverge from the native URL parser, so it must not be flagged.
208
+ const input = 'http://evil.com%5C@allowed.com'
209
+ const parsed = fastURI.parse(input)
210
+
211
+ t.plan(2)
212
+ t.notOk(parsed.error, '%5C is valid encoded data, not malformed')
213
+ t.equal(parsed.host, new URL(input).hostname, '%5C host matches native URL (no divergence)')
214
+ })
215
+
216
+ test('parse does not reject a literal backslash in the query or fragment', (t) => {
217
+ // The rejection is scoped to the authority/path (the host-confusion surface);
218
+ // a backslash after "?"/"#" is normalized as encoded data as before.
219
+ const parsed = fastURI.parse('http://host.example.com/?x=\\y#z\\w')
220
+
221
+ t.plan(2)
222
+ t.notOk(parsed.error, 'backslash in query/fragment does not mark the URI malformed')
223
+ t.equal(parsed.host, 'host.example.com', 'host parsed normally')
224
+ })