esbuild enables any website to send any requests to the development server and read the response
Affected releases · esbuild
- 0.24.x and older0.0.0 – 0.24.2No fix, no longer updated
0.24.x has no fix yet; the newest fixed line:
npm install esbuild@0.25.0Affected
0.0.0 – 0.24.2
458 releases
Fixed
0.25.0 – 0.28.2
24 releases, latest
What happens
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.
github.com/evanw/esbuild/blob/df815ac27b84f8b34… github.com/evanw/esbuild/blob/df815ac27b84f8b34…
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 hereFetching
/assets: it's common to have aassetsdirectory when you have JS files and CSS files in a different directory and the directory listing feature tells the attacker the list of filesConnecting
/esbuildSSE 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 iRun
npm run watchRun
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
Impact
Users using the serve feature may get the source code stolen by malicious websites.