Skip to content

Commit e577f95

Browse files
committed
Escape the site title before using it in a regex
A title containing regex metacharacters would change the pattern's meaning or throw at build time.
1 parent a979dc2 commit e577f95

1 file changed

Lines changed: 10 additions & 4 deletions

File tree

docs/plugins/swm-geo.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,20 @@ const decode = (value) =>
1313
.replace(/&#(?:39|x27);/g, "'")
1414
.trim();
1515

16+
const escapeRegExp = (value) => value.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
17+
1618
const metaOf = (html, name) =>
17-
new RegExp(`<meta[^>]+name="${name}"[^>]+content="([^"]*)"`, 'i').exec(
18-
html,
19-
)?.[1] ?? '';
19+
new RegExp(
20+
`<meta[^>]+name="${escapeRegExp(name)}"[^>]+content="([^"]*)"`,
21+
'i',
22+
).exec(html)?.[1] ?? '';
2023

2124
function describe(html, siteTitle) {
2225
const raw = /<title[^>]*>([\s\S]*?)<\/title>/i.exec(html)?.[1] ?? '';
23-
const title = decode(raw).replace(new RegExp(`\\s*\\|\\s*${siteTitle}$`), '');
26+
const title = decode(raw).replace(
27+
new RegExp(`\\s*\\|\\s*${escapeRegExp(siteTitle)}$`),
28+
'',
29+
);
2430
return { title, description: decode(metaOf(html, 'description')) };
2531
}
2632

0 commit comments

Comments
 (0)