Skip to content

Commit d23834c

Browse files
authoredNov 6, 2024··
fix: [#1585] Fixes a security vulnerability that allowed for server side code to be executed by a <script> tag (#1588)
1 parent 5ee0b16 commit d23834c

File tree

2 files changed

+6
-7
lines changed

2 files changed

+6
-7
lines changed
 

‎packages/happy-dom/src/fetch/utilities/SyncFetchScriptBuilder.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,9 @@ export default class SyncFetchScriptBuilder {
4444
null,
4545
4
4646
)};
47-
const request = sendRequest(\`${request.url.href}\`, options, (incomingMessage) => {
47+
const request = sendRequest(${JSON.stringify(
48+
request.url.href
49+
)}, options, (incomingMessage) => {
4850
let data = Buffer.alloc(0);
4951
incomingMessage.on('data', (chunk) => {
5052
data = Buffer.concat([data, Buffer.from(chunk)]);

‎packages/happy-dom/test/fetch/SyncFetch.test.ts

+3-6
Original file line numberDiff line numberDiff line change
@@ -252,8 +252,7 @@ describe('SyncFetch', () => {
252252
it('Should not allow to inject code into scripts executed using child_process.execFileSync().', () => {
253253
browserFrame.url = 'https://localhost:8080/';
254254

255-
const url =
256-
"https://localhost:8080/`+require('child_process').execSync('id')+`/'+require('child_process').execSync('id')+'";
255+
const url = `https://localhost:8080/\`+require('child_process').execSync('id')+\`/'+require('child_process').execSync('id')+'/?key="+require('child_process').execSync('id')+"`;
257256
const responseText = 'test';
258257

259258
mockModule('child_process', {
@@ -267,7 +266,7 @@ describe('SyncFetch', () => {
267266
expect(args[1]).toBe(
268267
SyncFetchScriptBuilder.getScript({
269268
url: new URL(
270-
"https://localhost:8080/%60+require('child_process').execSync('id')+%60/'+require('child_process').execSync('id')+'"
269+
`https://localhost:8080/\`+require('child_process').execSync('id')+\`/'+require('child_process').execSync('id')+'/?key="+require('child_process').execSync('id')+"`
271270
),
272271
method: 'GET',
273272
headers: {
@@ -280,11 +279,9 @@ describe('SyncFetch', () => {
280279
body: null
281280
})
282281
);
283-
// new URL() will convert ` into %60
284-
// By using ` for the URL string within the script, we can prevent the script from being injected
285282
expect(
286283
args[1].includes(
287-
`\`https://localhost:8080/%60+require('child_process').execSync('id')+%60/'+require('child_process').execSync('id')+'\``
284+
`"https://localhost:8080/%60+require('child_process').execSync('id')+%60/'+require('child_process').execSync('id')+'/?key=%22+require(%27child_process%27).execSync(%27id%27)+%22"`
288285
)
289286
).toBe(true);
290287
expect(options).toEqual({

0 commit comments

Comments
 (0)
Please sign in to comment.