Summary
esbuild allows any websites to send any request to the development server and read the response due to default CORS settings.
Details
esbuild sets Access-Control-Allow-Origin: *
header to all requests, including the SSE connection, which allows any websites to send any request to the development server and read the response.
|
res.Header().Set("Access-Control-Allow-Origin", "*") |
|
res.Header().Set("Access-Control-Allow-Origin", "*") |
Attack scenario:
- The attacker serves a malicious web page (
http://malicious.example.com
).
- The user accesses the malicious web page.
- The attacker sends a
fetch('http://127.0.0.1:8000/main.js')
request by JS in that malicious web page. This request is normally blocked by same-origin policy, but that's not the case for the reasons above.
- The attacker gets the content of
http://127.0.0.1:8000/main.js
.
In this scenario, I assumed that the attacker knows the URL of the bundle output file name. But the attacker can also get that information by
- Fetching
/index.html
: normally you have a script tag here
- Fetching
/assets
: it's common to have a assets
directory when you have JS files and CSS files in a different directory and the directory listing feature tells the attacker the list of files
- Connecting
/esbuild
SSE endpoint: the SSE endpoint sends the URL path of the changed files when the file is changed (new EventSource('/esbuild').addEventListener('change', e => console.log(e.type, e.data))
)
- Fetching URLs in the known file: once the attacker knows one file, the attacker can know the URLs imported from that file
The scenario above fetches the compiled content, but if the victim has the source map option enabled, the attacker can also get the non-compiled content by fetching the source map file.
PoC
- Download reproduction.zip
- Extract it and move to that directory
- Run
npm i
- Run
npm run watch
- Run
fetch('http://127.0.0.1:8000/app.js').then(r => r.text()).then(content => console.log(content))
in a different website's dev tools.
![image](https://private-user-images.githubusercontent.com/49056869/407005094-08fc2e4d-e1ec-44ca-b0ea-78a73c3c40e9.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MzkzMzc5MzYsIm5iZiI6MTczOTMzNzYzNiwicGF0aCI6Ii80OTA1Njg2OS80MDcwMDUwOTQtMDhmYzJlNGQtZTFlYy00NGNhLWIwZWEtNzhhNzNjM2M0MGU5LnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNTAyMTIlMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjUwMjEyVDA1MjAzNlomWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPTIwMzE2ODYyYzQxZjk3ZmIzNzFhMTRmYjU2YjlmMTMwZTI1N2I5MDQyNzRjYmEzMTZhZmUwNTk1Nzc4NzY5MTkmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0In0.kD50ifWryeQ0MDB2VRWgCR_5UfB-VrTiYm0ojNiVuW4)
Impact
Users using the serve feature may get the source code stolen by malicious websites.
Summary
esbuild allows any websites to send any request to the development server and read the response due to default CORS settings.
Details
esbuild sets
Access-Control-Allow-Origin: *
header to all requests, including the SSE connection, which allows any websites to send any request to the development server and read the response.esbuild/pkg/api/serve_other.go
Line 121 in df815ac
esbuild/pkg/api/serve_other.go
Line 363 in df815ac
Attack scenario:
http://malicious.example.com
).fetch('http://127.0.0.1:8000/main.js')
request by JS in that malicious web page. This request is normally blocked by same-origin policy, but that's not the case for the reasons above.http://127.0.0.1:8000/main.js
.In this scenario, I assumed that the attacker knows the URL of the bundle output file name. But the attacker can also get that information by
/index.html
: normally you have a script tag here/assets
: it's common to have aassets
directory when you have JS files and CSS files in a different directory and the directory listing feature tells the attacker the list of files/esbuild
SSE endpoint: the SSE endpoint sends the URL path of the changed files when the file is changed (new EventSource('/esbuild').addEventListener('change', e => console.log(e.type, e.data))
)The scenario above fetches the compiled content, but if the victim has the source map option enabled, the attacker can also get the non-compiled content by fetching the source map file.
PoC
npm i
npm run watch
fetch('http://127.0.0.1:8000/app.js').then(r => r.text()).then(content => console.log(content))
in a different website's dev tools.Impact
Users using the serve feature may get the source code stolen by malicious websites.