From afe156e1747a234a6db307c04fdbe9c9bab4e582 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Philip=20J=C3=A4genstedt?= Date: Thu, 4 Jul 2024 18:15:15 +0200 Subject: [PATCH] Validate identifers when reading features/groups/snapshots 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 7573160358..ca9ea484e6 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);