From 677660c6da1a8ac3e6df305f2363c641f7440700 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Philip=20J=C3=A4genstedt?= Date: Mon, 8 Jul 2024 16:43:27 +0200 Subject: [PATCH] Validate identifers when reading features/groups/snapshots (#1317) This is in order to simplify the schema: https://github.com/web-platform-dx/web-features/pull/1060#discussion_r1664568229 --- index.ts | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/index.ts b/index.ts index 7573160358f..ca9ea484e69 100644 --- a/index.ts +++ b/index.ts @@ -38,6 +38,8 @@ function scrub(data: any) { return data as FeatureData; } +const identifierPattern = /^[a-z0-9-]*$/; + function* yamlEntries(root: string): Generator<[string, any]> { const filePaths = new fdir() .withBasePath() @@ -48,9 +50,13 @@ function* yamlEntries(root: string): Generator<[string, any]> { for (const fp of filePaths) { // The feature identifier/key is the filename without extension. const { name: key } = path.parse(fp); - const distPath = `${fp}.dist`; + + if (!identifierPattern.test(key)) { + throw new Error(`${key} is not a valid identifier (must be lowercase a-z, 0-9, and hyphens)`); + } const data = YAML.parse(fs.readFileSync(fp, { encoding: 'utf-8'})); + const distPath = `${fp}.dist`; if (fs.existsSync(distPath)) { const dist = YAML.parse(fs.readFileSync(distPath, { encoding: 'utf-8'})); Object.assign(data, dist);