bug: an admin key injected with ${{VAR}} that contains only digits becomes a number, so the correct key gets 401

Aperta Adatta ai principianti
#14,178 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 2 giorni

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
2/5
Tempo stimato
1-3 ore
Idoneità per principianti
78/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
docker, kubernetes, lua

Direzione di ricerca

Inizia da apisix/cli/file.lua, in corrispondenza di resolve_conf_var, e confronta la sua conversione di tipo con la dichiarazione di stringa per la chiave di amministrazione in apisix/cli/schema.lua; l’issue indica anche check_token in apisix/admin/init.lua. Riproduci il problema con un APISIX_ADMIN_KEY composto solo da cifre, quindi cerca un test mirato per i campi stringa sostituiti oppure aggiungine uno. Il risultato è raggiunto quando la chiave numerica viene accettata come stringa e una richiesta all’Admin API che la usa ha esito positivo, oppure quando l’avvio fallisce con un errore chiaro.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

Current Behavior

When deployment.admin.admin_key[].key comes from an environment variable:

deployment:
  admin:
    admin_key:
      - name: admin
        key: ${{APISIX_ADMIN_KEY}}
        role: admin

and the variable contains only digits (for example APISIX_ADMIN_KEY=123456), APISIX starts normally, but every Admin API request is rejected with 401, including requests that send exactly that key.

The cause is the type conversion in resolve_conf_var. After a ${{VAR}} substitution, a value that tonumber() accepts is converted to a number, and true / false are converted to booleans (apisix/cli/file.lua#L104-L127). check_token then compares the request header, which is a string, with the configured key, which is now a number (apisix/admin/init.lua#L103-L113), so they never match. The CLI schema declares key as a string (apisix/cli/schema.lua#L385-L392), but apisix init accepted the value without an error in my test.

This affects any deployment that injects the key from a secret store as an environment variable. One example is the Helm chart's apisix.admin.credentials.secretName option, which renders key: ${{APISIX_ADMIN_KEY}}.

Expected Behavior

A substituted value for a string field stays a string, so a numeric key works like any other key. If that is not possible, APISIX should refuse to start with a clear error instead of rejecting every request.

Error Logs

The Admin API returns 401 even with the correct key.

Steps to Reproduce
cat > config.yaml <<'EOF'
deployment:
  role: traditional
  role_traditional:
    config_provider: yaml
  admin:
    allow_admin:
      - 0.0.0.0/0
    admin_key:
      - name: admin
        key: ${{APISIX_ADMIN_KEY}}
        role: admin
EOF
printf 'routes: []\n#END\n' > apisix.yaml

docker run -d --name numeric-key -p 9180:9180 -e APISIX_ADMIN_KEY=123456 \
  -v "$PWD/config.yaml:/usr/local/apisix/conf/config.yaml:ro" \
  -v "$PWD/apisix.yaml:/usr/local/apisix/conf/apisix.yaml:ro" \
  apache/apisix:3.19.0-debian
sleep 5
curl -s -o /dev/null -w '%{http_code}\n' -H 'X-API-KEY: 123456' \
  http://127.0.0.1:9180/apisix/admin/configs
# prints 401

With APISIX_ADMIN_KEY=AbCdEf123 or APISIX_ADMIN_KEY='Ab#c: d', the same request returns 200.

Environment
  • APISIX version: 3.19.0 (apache/apisix:3.19.0-debian). The same code is on master at 2b6d519.
  • Operating system: Linux container on Docker Desktop
  • OpenResty / Nginx version: the one bundled in the image
Lingua principale
Lua
Stelle
17.3k
Fork
3k
Merge medio
4g 16h
PR unite (30g)
69

Preparare l'ambiente

Apri in Codespaces

Avvia il container di sviluppo del progetto nel browser, con il tuo account GitHub.

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di apache/apisix

Tutte le issue di apache/apisix

Issue simili

Altre issue su Lua

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.