Visitar URL original
USWDS - Footer: Prevent unsafe element creation from data-tag attribute by IHIutch · Pull Request #6674 · uswds/uswds · GitHub
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions packages/usa-footer/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,16 @@ function toggleHtmlTag(isMobile) {

const primaryLinks = bigFooter.querySelectorAll(BUTTON);

const ALLOWED_TAGS = ["h1", "h2", "h3", "h4", "h5", "h6"];

primaryLinks.forEach((currentElement) => {
const currentElementClasses = currentElement.getAttribute("class");
const preservedHtmlTag =
currentElement.getAttribute("data-tag") || currentElement.tagName;
const rawPreservedTag = (
currentElement.getAttribute("data-tag") || currentElement.tagName
).toLowerCase();
const preservedHtmlTag = ALLOWED_TAGS.includes(rawPreservedTag)
? rawPreservedTag
: "h4";

const newElementType = isMobile ? "button" : preservedHtmlTag;

Expand Down
14 changes: 14 additions & 0 deletions packages/usa-footer/src/test/footer.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,5 +132,19 @@ tests.forEach(({ name, selector: containerSelector }) => {
assert.strictEqual(buttons()[0].tagName, initialHtmlTag);
assert.strictEqual(buttons()[0].getAttribute("data-tag"), null);
});

it("falls back to h4 when data-tag is not in the allow-list", () => {
resizeTo(400);
assert.strictEqual(buttons()[0].tagName, "BUTTON");

buttons()[0].setAttribute("data-tag", "script");
buttons()[1].setAttribute("data-tag", "<img src=x onerror=alert(1)>");
buttons()[2].setAttribute("data-tag", "h7");

resizeTo(1024);
assert.strictEqual(buttons()[0].tagName, "H4");
assert.strictEqual(buttons()[1].tagName, "H4");
assert.strictEqual(buttons()[2].tagName, "H4");
});
});
});