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
Provide optional hardened security for the FZF server. If the
FZF_API_KEY
environment variable is set, FZF would refuse any request that does not contain the specified API key. The API key can be any string, and should be the specified using thex-api-key
header.Background
fzf --listen
currently accepts all requests, without any kind of authentication. This exposes the user to several threats:See #3368 for more details.
Alternative Approaches
We can protect against the DNS rebinding vulnerability by blocking any request that doesn't specify
localhost
in theHost
header. But this approach would only protect fzf against DNS rebinding, and it could also interfering with legitimate use cases where you want to bind another domain to the loopback interface.The API key could be made mandatory, and randomly auto-generated if not explicitly specified. This approach would break all current usage of the
--listen
flag.The API key could be specified with a new command line argument (e.g.
--listen-key
or--listen-api-key
). I believe using an environment variable is cleaner, since it naturally propagates to child processes.A different header name could be used. There are no standard headers for API authorization, but
x-api-key
is the closest thing we have to a popular choice for API keys (i.e. single-credential, directly-supplied secret keys for calling an API).We could use the standard
Authorization
header, but it offers no standard scheme for API keys. Thebearer
scheme is meant for tokens (especially OAuth 2 tokens), while theBasic
scheme requires two values (username and password) which makes it more complicated to use.Points of note
subtle.ConstantTimeCompare()
to prevent timing attacks. This requires converting the API key to a byte array, but it's well worth the price.Content-Type
header for error messages. I think it should returnContent-Type: text/plain
, but this is probably not this pull request's job to do that.