gateway icon indicating copy to clipboard operation
gateway copied to clipboard

API Key Authentication (apiKeyAuth) strips Bearer from beginning of the string

Open a-nych opened this issue 11 months ago • 6 comments

Description: When utilizing apiKeyAuth method in SecurityPolicy, when a secret key is prefixed with Bearer, eg. Bearer my-api-key and then user auths with the header Authorization: Bearer my-api-key the auth fails with 401. The solution to this issue is to specify the api-key in the Secret without the Bearer prefix.

Repro steps: Non-working configuration:

apiVersion: v1
kind: Secret
metadata:
  name: api-keys
type: Opaque
stringData:
  client1: "Bearer my-api-key"
---
apiVersion: gateway.envoyproxy.io/v1alpha1
kind: SecurityPolicy
metadata:
  name: authorization-api-keys
spec:
  targetRefs:
    - group: gateway.networking.k8s.io
      kind: HTTPRoute
      name: app-routes
  apiKeyAuth:
    credentialRefs:
      - group: ""
        kind: Secret
        namespace: qa01
        name: api-keys
    extractFrom:
      - headers:
          - authorization

Curl output

curl -XGET -I -H "authorization: Bearer my-api-key" https://envoy
HTTP/2 401

Working configuration:

apiVersion: v1
kind: Secret
metadata:
  name: api-keys
type: Opaque
stringData:
  client1: "my-api-key"
---
apiVersion: gateway.envoyproxy.io/v1alpha1
kind: SecurityPolicy
metadata:
  name: authorization-api-keys
spec:
  targetRefs:
    - group: gateway.networking.k8s.io
      kind: HTTPRoute
      name: app-routes
  apiKeyAuth:
    credentialRefs:
      - group: ""
        kind: Secret
        namespace: qa01
        name: api-keys
    extractFrom:
      - headers:
          - authorization

Curl output

curl -XGET -I -H "authorization: Bearer my-api-key" https://envoy
HTTP/2 200

Environment: Envoy Helm Chart v1.6.0

a-nych avatar Dec 09 '25 14:12 a-nych

Hi @a-nych the API key auth filter doesn't treat the bearer prefix as part of the key, so it strips it out during processing.

https://github.com/envoyproxy/envoy/blob/4072078808fea56068edbc108f97d1bcdee43e48/source/extensions/filters/http/api_key_auth/api_key_auth.cc#L49-L53

zhaohuabing avatar Dec 10 '25 02:12 zhaohuabing

@zhaohuabing if this is wanted behavior then it's fine with me, but for the next person who stumbles upon this, a notice in documentation would be helpful and save time from debugging. If I have some more time in the evening I will try to make a PR for the docs.

a-nych avatar Dec 10 '25 08:12 a-nych

Sure. Feel free to open a PR to add this to the docs.

zhaohuabing avatar Dec 10 '25 08:12 zhaohuabing

Hi maintainers, I’d like to work on this issue. Please assign it to me if it’s available. Thanks!

Aditya7880900936 avatar Dec 15 '25 22:12 Aditya7880900936

Hi @Aditya7880900936 Thanks for volunteering. This file needs to be updated: https://github.com/envoyproxy/gateway/blob/main/site/content/en/latest/tasks/security/apikey-auth.md

zhaohuabing avatar Dec 17 '25 02:12 zhaohuabing

Hi @zhaohuabing , thanks! I’ve started working on updating this file and will open a PR shortly.

Aditya7880900936 avatar Dec 17 '25 17:12 Aditya7880900936