Skip to content

Commit

Permalink
fix: Allow URLs with multiple leading slashes.
Browse files Browse the repository at this point in the history
Fixes #1025
  • Loading branch information
RubenVerborgh committed Oct 25, 2021
1 parent 2e45899 commit 0a9429d
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
11 changes: 5 additions & 6 deletions src/http/input/identifier/OriginalUrlExtractor.ts
Expand Up @@ -45,12 +45,11 @@ export class OriginalUrlExtractor extends TargetExtractor {
}

// URL object applies punycode encoding to domain
const base = `${protocol}://${host}`;
const originalUrl = new URL(toCanonicalUriPath(url), base);

// Drop the query string if requested
if (!this.includeQueryString) {
originalUrl.search = '';
const originalUrl = new URL(`${protocol}://${host}`);
const [ , pathname, search ] = /^([^?]*)(.*)/u.exec(toCanonicalUriPath(url))!;
originalUrl.pathname = pathname;
if (this.includeQueryString) {
originalUrl.search = search;
}

return { path: originalUrl.href };
Expand Down
6 changes: 6 additions & 0 deletions test/unit/http/input/identifier/OriginalUrlExtractor.test.ts
Expand Up @@ -32,6 +32,12 @@ describe('A OriginalUrlExtractor', (): void => {
.resolves.toEqual({ path: 'http://test.com/url' });
});

it('returns an input URL with multiple leading slashes.', async(): Promise<void> => {
const noQuery = new OriginalUrlExtractor({ includeQueryString: true });
await expect(noQuery.handle({ request: { url: '///url?abc=def&xyz', headers: { host: 'test.com' }} as any }))
.resolves.toEqual({ path: 'http://test.com///url?abc=def&xyz' });
});

it('drops the query string when includeQueryString is set to false.', async(): Promise<void> => {
await expect(extractor.handle({ request: { url: '/url?abc=def&xyz', headers: { host: 'test.com' }} as any }))
.resolves.toEqual({ path: 'http://test.com/url?abc=def&xyz' });
Expand Down

0 comments on commit 0a9429d

Please sign in to comment.