Repository navigation
fix(@angular/build): match proxy glob patterns against URL pathname only #34377
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
clydin
wants to merge
1
commit into
angular:main
Choose a base branch
from
clydin:fix/proxy-glob-query-string
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+211
−2
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
129 changes: 129 additions & 0 deletions
129
packages/angular/build/src/utils/load-proxy-config_spec.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,129 @@ | ||
| /** | ||
| * @license | ||
| * Copyright Google LLC All Rights Reserved. | ||
| * | ||
| * Use of this source code is governed by an MIT-style license that can be | ||
| * found in the LICENSE file at https://angular.dev/license | ||
| */ | ||
|
|
||
| import assert from 'node:assert'; | ||
| import { mkdtemp, rm, writeFile } from 'node:fs/promises'; | ||
| import { join } from 'node:path'; | ||
| import { loadProxyConfiguration } from './load-proxy-config'; | ||
|
clydin marked this conversation as resolved.
|
||
|
|
||
| describe('loadProxyConfiguration', () => { | ||
| let tempDir: string; | ||
|
|
||
| beforeEach(async () => { | ||
| const baseTmpDir = process.env['TEST_TMPDIR']; | ||
| assert(baseTmpDir, 'TEST_TMPDIR is not set'); | ||
| tempDir = await mkdtemp(join(baseTmpDir, 'angular-proxy-config-spec-')); | ||
| }); | ||
|
clydin marked this conversation as resolved.
|
||
|
|
||
| afterEach(async () => { | ||
| await rm(tempDir, { recursive: true, force: true }); | ||
| }); | ||
|
|
||
| async function createGlobMatcher(glob: string): Promise<(url: string) => boolean> { | ||
| const configPath = join(tempDir, 'proxy.conf.json'); | ||
| await writeFile(configPath, JSON.stringify({ [glob]: { target: 'http://localhost:3000' } })); | ||
| const proxy = await loadProxyConfiguration(tempDir, 'proxy.conf.json'); | ||
| assert(proxy, 'Expected proxy configuration to be loaded'); | ||
| const keys = Object.keys(proxy); | ||
| expect(keys.length).toBe(1); | ||
| const regex = new RegExp(keys[0]); | ||
|
|
||
| return (url: string) => regex.test(url); | ||
| } | ||
|
|
||
| describe('glob to RegExp conversion with query strings', () => { | ||
| it('handles trailing globstar (/**) with and without query strings', async () => { | ||
| const matches = await createGlobMatcher('/search/**'); | ||
|
|
||
| expect(matches('/search')).toBeTrue(); | ||
| expect(matches('/search?q=angular')).toBeTrue(); | ||
| expect(matches('/search/')).toBeTrue(); | ||
| expect(matches('/search/?q=angular')).toBeTrue(); | ||
| expect(matches('/search/items')).toBeTrue(); | ||
| expect(matches('/search/items?q=angular')).toBeTrue(); | ||
| expect(matches('/search/items?callback=/.hidden')).toBeTrue(); | ||
|
|
||
| expect(matches('/searching')).toBeFalse(); | ||
| expect(matches('/searching?q=angular')).toBeFalse(); | ||
| expect(matches('/other?redirect=/search')).toBeFalse(); | ||
| }); | ||
|
|
||
| it('handles single-segment wildcard (*) without matching across slashes or query strings', async () => { | ||
| const matches = await createGlobMatcher('/api/*'); | ||
|
|
||
| expect(matches('/api/users')).toBeTrue(); | ||
| expect(matches('/api/users?id=1')).toBeTrue(); | ||
| expect(matches('/api/users?redirect=/home/page')).toBeTrue(); | ||
|
|
||
| expect(matches('/api')).toBeFalse(); | ||
| expect(matches('/api/')).toBeFalse(); | ||
| expect(matches('/api/?a=1')).toBeFalse(); | ||
| expect(matches('/api/users/profile')).toBeFalse(); | ||
| expect(matches('/api/users/profile?a=1')).toBeFalse(); | ||
| }); | ||
|
|
||
| it('handles suffix globs (*.json) without matching query parameters', async () => { | ||
| const matches = await createGlobMatcher('/api/*.json'); | ||
|
|
||
| expect(matches('/api/data.json')).toBeTrue(); | ||
| expect(matches('/api/data.json?v=1')).toBeTrue(); | ||
|
|
||
| expect(matches('/api/users')).toBeFalse(); | ||
| expect(matches('/api/users?file=data.json')).toBeFalse(); | ||
| expect(matches('/api/.json?v=1')).toBeFalse(); | ||
| }); | ||
|
|
||
| it('handles middle globstar (/**/) without matching paths in query parameters', async () => { | ||
| const matches = await createGlobMatcher('/api/**/users'); | ||
|
|
||
| expect(matches('/api/users')).toBeTrue(); | ||
| expect(matches('/api/users?page=1')).toBeTrue(); | ||
| expect(matches('/api/v1/users?page=1')).toBeTrue(); | ||
| expect(matches('/api/v1/v2/users?page=1')).toBeTrue(); | ||
|
|
||
| expect(matches('/api/other?redirect=/users')).toBeFalse(); | ||
| expect(matches('/api/users/profile?page=1')).toBeFalse(); | ||
| }); | ||
|
|
||
| it('handles single-character wildcard (?) without matching the query delimiter', async () => { | ||
| const matches = await createGlobMatcher('/api/v?'); | ||
|
|
||
| expect(matches('/api/v1')).toBeTrue(); | ||
| expect(matches('/api/v1?a=1')).toBeTrue(); | ||
|
|
||
| expect(matches('/api/v')).toBeFalse(); | ||
| expect(matches('/api/v?')).toBeFalse(); | ||
| expect(matches('/api/v?a=1')).toBeFalse(); | ||
| expect(matches('/api/v12?a=1')).toBeFalse(); | ||
| }); | ||
|
|
||
| it('handles negated globs (!) with and without query strings', async () => { | ||
| const matches = await createGlobMatcher('!/api/public/**'); | ||
|
|
||
| expect(matches('/api/public')).toBeFalse(); | ||
| expect(matches('/api/public?foo=1')).toBeFalse(); | ||
| expect(matches('/api/public/assets?foo=1')).toBeFalse(); | ||
|
|
||
| expect(matches('/api/private')).toBeTrue(); | ||
| expect(matches('/api/private?foo=1')).toBeTrue(); | ||
| }); | ||
|
|
||
| it('handles brace expansion and extglobs with query strings', async () => { | ||
| const matchesBraces = await createGlobMatcher('/api/{auth,users}/**'); | ||
| expect(matchesBraces('/api/auth?q=1')).toBeTrue(); | ||
| expect(matchesBraces('/api/users/list?q=1')).toBeTrue(); | ||
| expect(matchesBraces('/api/other?q=1')).toBeFalse(); | ||
|
|
||
| const matchesExtglob = await createGlobMatcher('/**/!(*.d).ts'); | ||
| expect(matchesExtglob('/app/main.ts')).toBeTrue(); | ||
| expect(matchesExtglob('/app/main.ts?extra=.d.ts')).toBeTrue(); | ||
| expect(matchesExtglob('/app/main.d.ts')).toBeFalse(); | ||
| expect(matchesExtglob('/app/main.d.ts?extra=.ts')).toBeFalse(); | ||
| }); | ||
| }); | ||
| }); | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.