Skip to content

Commit cf6ea6b

Browse files
authored
fix(assets): Ensure remotePatterns globs don't match unexpected paths (#15779)
1 parent 074901f commit cf6ea6b

File tree

3 files changed

+15
-1
lines changed

3 files changed

+15
-1
lines changed

.changeset/perky-dots-prove.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@astrojs/internal-helpers': patch
3+
---
4+
5+
Fixes glob matching of remote patterns matching more paths than intended in select situations

packages/astro/test/units/remote-pattern.test.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,12 @@ describe('remote-pattern', () => {
9292
assert.equal(matchPathname(url2, '/*', true), false);
9393
});
9494

95+
it('does not match pathname when prefix appears mid-path', async () => {
96+
// /en/* should NOT match /evil/en/getting-started
97+
const evilUrl = new URL('https://docs.astro.build/evil/en/getting-started');
98+
assert.equal(matchPathname(evilUrl, '/en/*', true), false);
99+
});
100+
95101
it('matches patterns', async () => {
96102
assert.equal(matchPattern(url1, {}), true);
97103

packages/internal-helpers/src/remote.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,11 @@ export function matchPathname(url: URL, pathname?: string, allowWildcard = false
9292
return slicedPathname !== url.pathname && url.pathname.startsWith(slicedPathname);
9393
} else if (pathname.endsWith('/*')) {
9494
const slicedPathname = pathname.slice(0, -1); // * length
95+
if (!url.pathname.startsWith(slicedPathname)) {
96+
return false;
97+
}
9598
const additionalPathChunks = url.pathname
96-
.replace(slicedPathname, '')
99+
.slice(slicedPathname.length)
97100
.split('/')
98101
.filter(Boolean);
99102
return additionalPathChunks.length === 1;

0 commit comments

Comments
 (0)