Skip to content

Commit f576519

Browse files
authoredJan 21, 2025··
Revert "Revert "feat(server-islands): only encode ETAGO delimiter (#11513)"" (#13031)
1 parent 8911bda commit f576519

File tree

5 files changed

+40
-8
lines changed

5 files changed

+40
-8
lines changed
 

‎.changeset/fifty-socks-end.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'astro': patch
3+
---
4+
5+
Updates the server islands encoding logic to only escape the script end tag open delimiter and opening HTML comment syntax

‎packages/astro/src/runtime/server/render/server-islands.ts

+11-5
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,19 @@ export function containsServerDirective(props: Record<string | number, any>) {
1515
return 'server:component-directive' in props;
1616
}
1717

18+
const SCRIPT_RE = /<\/script/giu;
19+
const COMMENT_RE = /<!--/gu;
20+
const SCRIPT_REPLACER = '<\\/script';
21+
const COMMENT_REPLACER = '\\u003C!--';
22+
23+
/**
24+
* Encodes the script end-tag open (ETAGO) delimiter and opening HTML comment syntax for JSON inside a `<script>` tag.
25+
* @see https://mathiasbynens.be/notes/etago
26+
*/
1827
function safeJsonStringify(obj: any) {
1928
return JSON.stringify(obj)
20-
.replace(/\u2028/g, '\\u2028')
21-
.replace(/\u2029/g, '\\u2029')
22-
.replace(/</g, '\\u003c')
23-
.replace(/>/g, '\\u003e')
24-
.replace(/\//g, '\\u002f');
29+
.replace(SCRIPT_RE, SCRIPT_REPLACER)
30+
.replace(COMMENT_RE, COMMENT_REPLACER);
2531
}
2632

2733
function createSearchParams(componentExport: string, encryptedProps: string, slots: string) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
---
2+
import Island from '../components/Island.astro';
3+
---
4+
<html>
5+
<head>
6+
<title>Testing</title>
7+
</head>
8+
<body>
9+
<h1>Testing</h1>
10+
<Island server:defer />
11+
</body>
12+
</html>
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
---
22
import Island from '../components/Island.astro';
3+
4+
const xssMe ="</script><script>alert('xss')</script><!--"
35
---
46
<html>
57
<head>
68
<title>Testing</title>
79
</head>
810
<body>
911
<h1>Testing</h1>
10-
<Island server:defer />
12+
<Island server:defer message={xssMe} />
1113
</body>
1214
</html>

‎packages/astro/test/server-islands.test.js

+9-2
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,13 @@ describe('Server islands', () => {
3737
assert.equal(serverIslandEl.length, 0);
3838
});
3939

40+
it('HTML escapes scripts', async () => {
41+
const res = await fixture.fetch('/');
42+
assert.equal(res.status, 200);
43+
const html = await res.text();
44+
assert.equal(html.includes("</script><script>alert('xss')</script><!--"), false);
45+
});
46+
4047
it('island is not indexed', async () => {
4148
const res = await fixture.fetch('/_server-islands/Island', {
4249
method: 'POST',
@@ -62,7 +69,7 @@ describe('Server islands', () => {
6269
assert.equal(works, 'true', 'able to set header from server island');
6370
});
6471
it('omits empty props from the query string', async () => {
65-
const res = await fixture.fetch('/');
72+
const res = await fixture.fetch('/empty-props');
6673
assert.equal(res.status, 200);
6774
const html = await res.text();
6875
const fetchMatch = html.match(/fetch\('\/_server-islands\/Island\?[^']*p=([^&']*)/);
@@ -135,7 +142,7 @@ describe('Server islands', () => {
135142
});
136143
it('omits empty props from the query string', async () => {
137144
const app = await fixture.loadTestAdapterApp();
138-
const request = new Request('http://example.com/');
145+
const request = new Request('http://example.com/empty-props');
139146
const response = await app.render(request);
140147
assert.equal(response.status, 200);
141148
const html = await response.text();

0 commit comments

Comments
 (0)
Please sign in to comment.