fix(astro): prevent crash on unexpected file in pages (#2034)

* Skip files in `src/pages` when extension is not '.astro' or '.md'
* Add test

Fix #2033
This commit is contained in:
Ludovico Fischer 2021-11-29 20:33:16 +01:00 committed by GitHub
parent e7feb425b8
commit 4436592d22
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 28 additions and 1 deletions

View file

@ -0,0 +1,5 @@
---
'astro': patch
---
Fix crash with unexpected file types in pages directory

View file

@ -86,6 +86,7 @@ interface Item {
export function createRouteManifest({ config, cwd }: { config: AstroConfig; cwd?: string }, logging: LogOptions): ManifestData {
const components: string[] = [];
const routes: RouteData[] = [];
const validExtensions: Set<string> = new Set(['.astro', '.md']);
function walk(dir: string, parentSegments: Part[][], parentParams: string[]) {
let items: Item[] = [];
@ -104,7 +105,7 @@ export function createRouteManifest({ config, cwd }: { config: AstroConfig; cwd?
return;
}
// filter out "foo.astro_tmp" files, etc
if (!isDir && !/^(\.[a-z0-9]+)+$/i.test(ext)) {
if (!isDir && !validExtensions.has(ext)) {
return;
}
const segment = isDir ? basename : name;

View file

@ -184,6 +184,27 @@ describe('route manifest', () => {
]);
});
it('ignores invalid route extensions', () => {
const { routes } = create('invalid-extension', 'always');
expect(cleanRoutes(routes)).to.deep.equal([
{
type: 'page',
pattern: /^\/$/,
params: [],
component: 'invalid-extension/index.astro',
pathname: '/',
},
{
type: 'page',
pattern: /^\/about\/$/,
params: [],
component: 'invalid-extension/about.astro',
pathname: '/about',
},
]);
});
it('allows multiple slugs', () => {
const { routes } = create('multiple-slugs', 'always');