feat(decap-server): Allow the server to be run securely #7579
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
The documentation currently says:
This is fair but it is also unnecessary. The server can be restricted in such a way that it is safe to use.
I introduced two optional environment variables to configure
decap-server
. TheBIND_HOST
variable allows binding to127.0.0.1
rather than all IP addresses. AndORIGIN
allows restricting CORS responses to a specific origin rather that allowing the API to be accessed from any server.Note that I would have preferred making
BIND_HOST=localhost
the default. I didn’t want to introduce backwards incompatible changes however.Test plan
Running
npm run start
still allows connecting to bothhttp://localhost:8081/
andhttp://<public_ip>:8081>/
. The responses contain the HTTP headerAccess-Content-Allow-Origin: *
as before this change.Running
BIND_HOST=localhost npm run start
allows connecting tohttp://localhost:8081/
but connections tohttp://<public_ip>:8081>/
are no longer possible. It’s a purely local server now.Running
ORIGIN=https://example.com npm run start
changes the HTTP header in responses toAccess-Content-Allow-Origin: https://example.com
. The server can no longer be accessed by arbitrary websites.Tests complain about
port
beingnumber | string
whereasnumber
is expected. This is unrelated to my change but I’ve fixed it.Checklist
Please add a
x
inside each checkbox:I’ve also noticed that the script required fornpm run format
doesn’t actually exist. I guessnpm run lint
is meant here which for me errors out due to two warnings unrelated to my changes – but for some reason it succeeds in the CI runs (are these running an older lint version?).🐈