Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

prevent URLs in attributes being escaped #9820

Merged
merged 8 commits into from
Mar 13, 2024
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
5 changes: 5 additions & 0 deletions .changeset/quick-islands-ring.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"astro": patch
---

Prevents fully formed URLs in attributes from being escaped
14 changes: 14 additions & 0 deletions packages/astro/src/runtime/server/render/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,11 @@ Make sure to use the static attribute syntax (\`${key}={value}\`) instead of the
return markHTMLString(` class="${toAttributeString(value, shouldEscape)}"`);
}

// Prevents URLs in attributes from being escaped in static builds
if (typeof value === 'string' && value.includes('&') && urlCanParse(value)) {
return markHTMLString(` ${key}="${toAttributeString(value, false)}"`);
}

// Boolean values only need the key
if (value === true && (key.startsWith('data-') || htmlBooleanAttributes.test(key))) {
return markHTMLString(` ${key}`);
Expand Down Expand Up @@ -224,3 +229,12 @@ export function promiseWithResolvers<T = any>(): PromiseWithResolvers<T> {
reject,
};
}

function urlCanParse(url: string) {
try {
new URL(url);
lilnasy marked this conversation as resolved.
Show resolved Hide resolved
return true;
} catch {
return false;
}
}
3 changes: 3 additions & 0 deletions packages/astro/test/astro-attrs.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ describe('Attributes', async () => {
'html-enum-false': { attribute: 'draggable', value: 'false' },
};

// cheerio will unescape the values, so checking that the url rendered unescaped to begin with has to be done manually
assert.equal(html.includes("https://example.com/api/og?title=hello&description=somedescription"), true);

for (const id of Object.keys(attrs)) {
const { attribute, value } = attrs[id];
const attr = $(`#${id}`).attr(attribute);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<span id="empty" attr="" />
<span id="null" attr={null} />
<span id="undefined" attr={undefined} />
<span id="url" attr={"https://example.com/api/og?title=hello&description=somedescription"}/>
<!--
Per HTML spec, some attributes should be treated as booleans
These should always render <span async /> or <span /> (without a string value)
Expand Down
Loading