diff options
author | 2021-11-29 20:33:16 +0100 | |
---|---|---|
committer | 2021-11-29 12:33:16 -0700 | |
commit | 4436592d22f61622d1119dd4f4136bc7c33eb0bf (patch) | |
tree | 1d0c1b3445a375acca24f716926fea2a4e87500d | |
parent | e7feb425b838c749b5c4bf3d28e2ecab37ba246b (diff) | |
download | astro-4436592d22f61622d1119dd4f4136bc7c33eb0bf.tar.gz astro-4436592d22f61622d1119dd4f4136bc7c33eb0bf.tar.zst astro-4436592d22f61622d1119dd4f4136bc7c33eb0bf.zip |
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
7 files changed, 28 insertions, 1 deletions
diff --git a/.changeset/tame-hats-give.md b/.changeset/tame-hats-give.md new file mode 100644 index 000000000..c4158a46b --- /dev/null +++ b/.changeset/tame-hats-give.md @@ -0,0 +1,5 @@ +--- +'astro': patch +--- + +Fix crash with unexpected file types in pages directory diff --git a/packages/astro/src/core/ssr/routing.ts b/packages/astro/src/core/ssr/routing.ts index 1eedfb67f..d3d20c2d1 100644 --- a/packages/astro/src/core/ssr/routing.ts +++ b/packages/astro/src/core/ssr/routing.ts @@ -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; diff --git a/packages/astro/test/fixtures/route-manifest/invalid-extension/about.astro b/packages/astro/test/fixtures/route-manifest/invalid-extension/about.astro new file mode 100644 index 000000000..e69de29bb --- /dev/null +++ b/packages/astro/test/fixtures/route-manifest/invalid-extension/about.astro diff --git a/packages/astro/test/fixtures/route-manifest/invalid-extension/image.svg b/packages/astro/test/fixtures/route-manifest/invalid-extension/image.svg new file mode 100644 index 000000000..e69de29bb --- /dev/null +++ b/packages/astro/test/fixtures/route-manifest/invalid-extension/image.svg diff --git a/packages/astro/test/fixtures/route-manifest/invalid-extension/index.astro b/packages/astro/test/fixtures/route-manifest/invalid-extension/index.astro new file mode 100644 index 000000000..e69de29bb --- /dev/null +++ b/packages/astro/test/fixtures/route-manifest/invalid-extension/index.astro diff --git a/packages/astro/test/fixtures/route-manifest/invalid-extension/styles.css b/packages/astro/test/fixtures/route-manifest/invalid-extension/styles.css new file mode 100644 index 000000000..e69de29bb --- /dev/null +++ b/packages/astro/test/fixtures/route-manifest/invalid-extension/styles.css diff --git a/packages/astro/test/route-manifest.test.js b/packages/astro/test/route-manifest.test.js index 1f32ee6af..8ae58c1d6 100644 --- a/packages/astro/test/route-manifest.test.js +++ b/packages/astro/test/route-manifest.test.js @@ -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'); |