Skip to content

Commit

Permalink
Revert "feat(server-islands): only encode ETAGO delimiter (#11513)" (#…
Browse files Browse the repository at this point in the history
…13013)

This reverts commit f64b73c.
  • Loading branch information
ascorbic authored Jan 20, 2025
1 parent 3357ff6 commit d065182
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 26 deletions.
5 changes: 0 additions & 5 deletions .changeset/fifty-socks-end.md

This file was deleted.

16 changes: 5 additions & 11 deletions packages/astro/src/runtime/server/render/server-islands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,13 @@ export function containsServerDirective(props: Record<string | number, any>) {
return 'server:component-directive' in props;
}

const SCRIPT_RE = /<\/script/giu;
const COMMENT_RE = /<!--/gu;
const SCRIPT_REPLACER = '<\\/script';
const COMMENT_REPLACER = '\\u003C!--';

/**
* Encodes the script end-tag open (ETAGO) delimiter and opening HTML comment syntax for JSON inside a `<script>` tag.
* @see https://mathiasbynens.be/notes/etago
*/
function safeJsonStringify(obj: any) {
return JSON.stringify(obj)
.replace(SCRIPT_RE, SCRIPT_REPLACER)
.replace(COMMENT_RE, COMMENT_REPLACER);
.replace(/\u2028/g, '\\u2028')
.replace(/\u2029/g, '\\u2029')
.replace(/</g, '\\u003c')
.replace(/>/g, '\\u003e')
.replace(/\//g, '\\u002f');
}

function createSearchParams(componentExport: string, encryptedProps: string, slots: string) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
---
import Island from '../components/Island.astro';
const xssMe ="</script><script>alert('xss')</script><!--"
---
<html>
<head>
<title>Testing</title>
</head>
<body>
<h1>Testing</h1>
<Island server:defer message={xssMe} />
<Island server:defer />
</body>
</html>
7 changes: 0 additions & 7 deletions packages/astro/test/server-islands.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,6 @@ describe('Server islands', () => {
assert.equal(serverIslandEl.length, 0);
});

it('HTML escapes scripts', async () => {
const res = await fixture.fetch('/');
assert.equal(res.status, 200);
const html = await res.text();
assert.equal(html.includes("</script><script>alert('xss')</script><!--"), false);
});

it('island is not indexed', async () => {
const res = await fixture.fetch('/_server-islands/Island', {
method: 'POST',
Expand Down

0 comments on commit d065182

Please sign in to comment.