fix(http): keep every ? after the first in the query string (CARD-25, GitHub #48)
#136
No files matched your search
@@ -81,6 +81,12 @@ npm treats versions as semver and semver forbids leading zeros.
|
||||
param hit −88%, method miss −94%, path miss −96%; at 10 routes, every cell is within noise or
|
||||
faster (late param hit +3.4%, 95% CI −5.0…+12.6%).
|
||||
|
||||
- **A query string keeps every `?` after the first** (GitHub #48). `parseQuery` took the text
|
||||
between the first and second `?`, so `/search?q=what?now` gave `q: 'what'` and dropped the rest.
|
||||
RFC 3986 §3.4 allows `?` inside a query, so the query string is now everything after the first
|
||||
`?` and before `#`, if there is one: `q: 'what?now'`, and `/search?a=1&b=?` gives `b: '?'`. URLs
|
||||
with a single `?` parse as before. Path extraction is unchanged.
|
||||
|
||||
### Fixed
|
||||
|
||||
- **`static` no longer serves files outside its root through a symlink** (#106). The traversal guard
|
||||
|
||||
+15
-10
@@ -743,18 +743,23 @@ export function allowedMethods(routes: RouteDef[], path: string): string[] {
|
||||
});
|
||||
}
|
||||
|
||||
/** Parses a URL's query string into a flat string map (last value wins for repeated keys). */
|
||||
/**
|
||||
* Parses a URL's query string into a flat string map (last value wins for repeated keys).
|
||||
*
|
||||
* The query string is everything after the **first** `?` and before a `#`, if one is present.
|
||||
* A `?` inside the query is a legal character (RFC 3986 §3.4), so `/search?q=what?now` gives
|
||||
* `{ q: 'what?now' }`.
|
||||
*/
|
||||
export function parseQuery(url: string): Record<string, string> {
|
||||
// Most requests carry no query string, and the work below is not free for them: `split` builds
|
||||
// an array, `new URLSearchParams('')` builds an object, and the loop runs zero times — three
|
||||
// allocations to arrive at `{}`. Measured at 92.3 ns against 18.6 ns for this check.
|
||||
//
|
||||
// Only the guard is new; the parse below is untouched on purpose. Reaching for `indexOf` +
|
||||
// `slice` instead of `split` would also drop an allocation, but it changes what a second `?`
|
||||
// means, and that is a behaviour question rather than a performance one.
|
||||
if (!url.includes('?')) return {};
|
||||
// Most requests carry no query string, and the work below is not free for them:
|
||||
// `new URLSearchParams('')` builds an object and the loop runs zero times, to arrive at `{}`.
|
||||
// Measured at 92.3 ns against 18.6 ns for this check.
|
||||
const start = url.indexOf('?');
|
||||
if (start < 0) return {};
|
||||
|
||||
const queryString = url.split('?')[1] ?? '';
|
||||
// `split('?')[1]` used to stop at a second `?` and drop the rest of the value (GitHub #48).
|
||||
const hash = url.indexOf('#', start + 1);
|
||||
const queryString = hash < 0 ? url.slice(start + 1) : url.slice(start + 1, hash);
|
||||
const out: Record<string, string> = {};
|
||||
for (const [key, value] of new URLSearchParams(queryString)) out[key] = value;
|
||||
return out;
|
||||
|
||||
@@ -0,0 +1,61 @@
|
||||
import 'reflect-metadata';
|
||||
import { afterAll, beforeAll, describe, expect, test } from 'vitest';
|
||||
import type { AddressInfo } from 'node:net';
|
||||
import type { Server } from 'node:http';
|
||||
import { Get, Module, Route, Transformer } from '../src/metadata';
|
||||
import { JsonTransformer } from '../src/transformers';
|
||||
import { createApp } from '../src/app';
|
||||
import { parseQuery } from '../src/http';
|
||||
import { query } from '../src/params';
|
||||
|
||||
// CARD-25 / GitHub #48: the query string is everything after the FIRST `?` (and before `#`).
|
||||
const CASES: Array<[string, Record<string, string>]> = [
|
||||
['/search?q=what?now', { q: 'what?now' }], // T1
|
||||
['/search?a=1&b=?', { a: '1', b: '?' }], // T2
|
||||
['/search?', {}], // T3
|
||||
['/search', {}], // T4
|
||||
];
|
||||
|
||||
describe('parseQuery', () => {
|
||||
test.each(CASES)('%s', (url, expected) => {
|
||||
expect(parseQuery(url)).toEqual(expected);
|
||||
});
|
||||
|
||||
test('stops at a fragment', () => {
|
||||
expect(parseQuery('/search?q=a?b#frag?x=1')).toEqual({ q: 'a?b' });
|
||||
});
|
||||
});
|
||||
|
||||
@Route('/search')
|
||||
class SearchCtl {
|
||||
@Get('/')
|
||||
@Transformer(JsonTransformer)
|
||||
search(@query() q: Record<string, string>) {
|
||||
return q;
|
||||
}
|
||||
}
|
||||
|
||||
@Module({ mountpoint: '/', controllers: [SearchCtl] })
|
||||
class SearchModule {}
|
||||
|
||||
describe('query string with more than one `?`', () => {
|
||||
const app = createApp({ modules: [SearchModule] });
|
||||
let server: Server;
|
||||
let base: string;
|
||||
|
||||
beforeAll(async () => {
|
||||
server = await app.listen(0);
|
||||
base = `http://127.0.0.1:${(server.address() as AddressInfo).port}`;
|
||||
});
|
||||
afterAll(() => new Promise<void>((resolve) => server.close(() => resolve())));
|
||||
|
||||
test.each(CASES)('app.fetch %s', async (url, expected) => {
|
||||
const res = await app.fetch(new Request(`http://x${url}`));
|
||||
expect(await res.json()).toEqual(expected);
|
||||
});
|
||||
|
||||
test.each(CASES)('Node listener %s', async (url, expected) => {
|
||||
const res = await fetch(`${base}${url}`);
|
||||
expect(await res.json()).toEqual(expected);
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user