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

Escape HTML by default #2747

Merged
merged 2 commits into from
Mar 9, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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/sour-socks-enjoy.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': minor
---

Escape HTML inside of expressions by default. Please see [our migration guide](https://docs.astro.build/en/migrate/#deprecated-unescaped-html) for more details.
4 changes: 2 additions & 2 deletions packages/astro/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
"test:match": "mocha --timeout 20000 -g"
},
"dependencies": {
"@astrojs/compiler": "^0.12.0-next.8",
"@astrojs/compiler": "^0.12.0-next.9",
"@astrojs/language-server": "^0.8.6",
"@astrojs/markdown-remark": "^0.6.4",
"@astrojs/prism": "0.4.0",
Expand Down Expand Up @@ -95,10 +95,10 @@
"resolve": "^1.20.0",
"rollup": "^2.64.0",
"semver": "^7.3.5",
"sirv": "^2.0.2",
"serialize-javascript": "^6.0.0",
"shiki": "^0.10.0",
"shorthash": "^0.0.2",
"sirv": "^2.0.2",
"slash": "^4.0.0",
"sourcemap-codec": "^1.4.8",
"srcset-parse": "^1.1.0",
Expand Down
19 changes: 1 addition & 18 deletions packages/astro/src/runtime/server/escape.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,6 @@
const entities = { '"': 'quot', '&': 'amp', "'": 'apos', '<': 'lt', '>': 'gt' } as const;

const warned = new Set<string>();
export const escapeHTML = (string: any, { deprecated = false }: { deprecated?: boolean } = {}) => {
const escaped = string.replace(/["'&<>]/g, (char: keyof typeof entities) => '&' + entities[char] + ';');
if (!deprecated) return escaped;
if (warned.has(string) || !string.match(/[&<>]/g)) return string;
// eslint-disable-next-line no-console
console.warn(`Unescaped HTML content found inside expression!

The next minor version of Astro will automatically escape all
expression content. Please use the \`set:html\` directive.

Expression content:
${string}`);
warned.add(string);

// Return unescaped content for now. To be removed.
return string;
};
export const escapeHTML = (string: any) => string.replace(/["'&<>]/g, (char: keyof typeof entities) => '&' + entities[char] + ';');

/**
* RawString is a "blessed" version of String
Expand Down
4 changes: 2 additions & 2 deletions packages/astro/src/runtime/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { escapeHTML, UnescapedString, unescapeHTML } from './escape.js';

export type { Metadata } from './metadata';
export { createMetadata } from './metadata.js';
export { escapeHTML, unescapeHTML } from './escape.js';
export { unescapeHTML } from './escape.js';

const voidElementNames = /^(area|base|br|col|command|embed|hr|img|input|keygen|link|meta|param|source|track|wbr)$/i;
const htmlBooleanAttributes =
Expand Down Expand Up @@ -36,7 +36,7 @@ async function _render(child: any): Promise<any> {
// of wrapping it in a function and calling it.
return _render(child());
} else if (typeof child === 'string') {
return escapeHTML(child, { deprecated: true });
return escapeHTML(child);
} else if (!child && child !== 0) {
// do nothing, safe to ignore falsey values.
}
Expand Down
11 changes: 7 additions & 4 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.