Home >Web Front-end >JS Tutorial >Preventing supply-chain attacks to the JavaScript ecosystem
Supply-chain attacks are big problem for the JavaScript ecosystem. In this short post I will outline a straightforward security measure that can be implemented by all JavaScript runtimes, both in-browser and off-browser. This will prevent most of the supply-chain attacks that are plaguing the JavaScript ecosystem today.
Here's a recent incident where websites were hacked:
The core of the problem is that JavaScript modules inherit the permissions of the application or module that has called them. This is a problem for both in-browser runtimes and off-browser runtimes.
This problem is further complicated by the fact that the module version that an application depends on can change unbeknownst to the application's author. So even if the codes of all dependencies have been thoroughly reviewed (which is a huge effort in itself), this effort will be wasted if dependency versions haven't been locked down.
Why not lock down versions and use semver-based dependencies? Well, this is mainly because if a module publisher publishes bug-fixes, it's better to use the fixed code. And this is one big reason why JavaScript CDNs such as esm.sh are supporting semvers.
Web browsers are sandboxed execution environments, so a third-party JavaScript module (3JM) that's imported by a website can't cause any damage to the end-user's device.
Nevertheless, a 3JM can use the device's compute resources and issue network requests for Bitcoin mining etc, without the website's consent.
Some off-browser runtimes such as Deno do implement measures to restrict the permissions given to a JavaScript/TypeScript application. But these measures fall short for the following reasons:
Currently, security teams have put automated processes in place for looking for vulnerabilities in modules that are published on well-known registries such as NPM. This security measure has several shortcomings:
To fix these issues, I propose a new per-module permissions system that is backwards-compatible with how JS/TS applications currently work.
This involves a new optional permissions configuration that each application and module can declare in their deno.json / deno.jsonc / package.json files. permissions has 2 parts:
Here's how permissions would be used by the JS/TS runtime:
The value of permissions would be something like this:
{ "self": { "read": {"allow": [], "deny": []}, "write": {"allow": [], "deny": []}, "net": {"allow": [], "deny": []}, "env": {"allow": [], "deny": []}, "run": {"allow": [], "deny": []} }, "imports": { "jsr:@org/module@1.0.0": { "read": {"allow": [], "deny": []}, "write": {"allow": [], "deny": []}, "net": {"allow": [], "deny": []}, "env": {"allow": [], "deny": []}, "run": {"allow": [], "deny": []} }, "https://cdn.example/org/module@1.0.0": { "read": {"allow": [], "deny": []}, "write": {"allow": [], "deny": []}, "net": {"allow": [], "deny": []}, "env": {"allow": [], "deny": []}, "run": {"allow": [], "deny": []} }, "[default]": { "read": {"allow": [], "deny": []}, "write": {"allow": [], "deny": []}, "net": {"allow": [], "deny": []}, "env": {"allow": [], "deny": []}, "run": {"allow": [], "deny": []} } } }
Compared to the current solution, the solution I propose has several advantages:
This seems very straightforward. No modification is required to the JS/TS languages. And if the permissions configuration is absent, then we fall back to the current situation where the application and its dependency graph all have the permissions that were provided by the command-line arguments ∎
The above is the detailed content of Preventing supply-chain attacks to the JavaScript ecosystem. For more information, please follow other related articles on the PHP Chinese website!