Skip to content

Commit 475c4c2

Browse files
authored
Merge pull request #3 from nint8835/no-xss
Resolve XSS / incorrectly displayed tags
2 parents 322e177 + 0a11098 commit 475c4c2

File tree

2 files changed

+32
-12
lines changed

2 files changed

+32
-12
lines changed

packages/astro-meta-tags/package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "astro-meta-tags",
3-
"version": "0.1.3",
3+
"version": "0.2.0",
44
"author": "Patrick Arminio",
55
"license": "MIT",
66
"description": "A Dev Toolbar extension to debug meta tags in your Astro website",
@@ -36,4 +36,4 @@
3636
"peerDependencies": {
3737
"astro": "^4.0.0"
3838
}
39-
}
39+
}

packages/astro-meta-tags/src/toolbar.ts

+30-10
Original file line numberDiff line numberDiff line change
@@ -24,31 +24,51 @@ const getWindowContent = () => {
2424
document.querySelectorAll("meta[property^='twitter:']")
2525
).map(getTagTuple);
2626

27-
console.log(twitterMetaTags);
28-
2927
const getSingleTagHtml = ([property, content]: [string, string]) => {
28+
let contentTag: HTMLElement | Text;
3029
if (["og:image", "twitter:image"].includes(property)) {
31-
content = `<img src="${content}" />`;
30+
contentTag = document.createElement("img");
31+
contentTag.setAttribute("src", content);
32+
} else {
33+
contentTag = document.createTextNode(content);
3234
}
3335

34-
return /* html */ `<dt>${property}</dt><dd>${content}</dd>`;
36+
const tagHtml = document.createDocumentFragment();
37+
38+
const propertyName = document.createElement("dt");
39+
propertyName.textContent = property;
40+
41+
const propertyValue = document.createElement("dd");
42+
propertyValue.append(contentTag);
43+
44+
tagHtml.append(propertyName, propertyValue);
45+
46+
return tagHtml
3547
};
3648

3749
const getTagsHtml = (
3850
title: string,
3951
tags: [string, string][],
4052
wrapWithDetails = true
4153
) => {
42-
const dl = `<dl>${tags.map((tag) => getSingleTagHtml(tag)).join("")}</dl>`;
54+
const dl = document.createElement("dl");
55+
56+
for (const tag of tags) {
57+
dl.append(getSingleTagHtml(tag));
58+
}
4359

4460
if (!wrapWithDetails) {
45-
return dl;
61+
return dl.outerHTML;
4662
}
4763

48-
return `<details>
49-
<summary>${title}</summary>
50-
${dl}
51-
</details>`;
64+
const details = document.createElement("details");
65+
66+
const summary = document.createElement("summary");
67+
summary.textContent = title;
68+
69+
details.append(summary, dl);
70+
71+
return details.outerHTML;
5272
};
5373

5474
const standardTags = [

0 commit comments

Comments
 (0)