iamops.dev Cerca
Argo su X Feed RSS
ITItaliano

Lab Pratica · 12 minuti

Sviluppatori e ingegneri dell'identità che vogliono capire perché un bearer token ha bisogno di un secondo elemento a ogni chiamata. Salta la preparazione se ti interessa solo il risultato.

Lab di proof of possession per agenti: perché un token copiato non basta

Emittente e server di risorse in locale: un bearer token da solo viene rifiutato, una prova firmata passa una volta, replay e chiavi sbagliate falliscono.

Diagramma di sequenza: l'agente riceve un token legato alla sua chiave, il server di risorse rifiuta il token inviato da solo e accetta la prova firmata con quella chiave.
In breveIl bearer token da solo viene rifiutato con proof_required, quindi una…
Ripeti questo lab 11 passi

Ho scritto questo lab perché tu possa rifarlo per conto tuo. Ogni passo ha i comandi da copiare e l'output che ho ottenuto io, e puoi segnarlo come fatto. Le spunte restano solo nel tuo browser.

Vai ai passi

Come funziona

Copia un prompt già pronto per Claude Code, Cursor, Codex o un altro agente di codice. Solo per ambienti di test.

Mostra il prompt
I want to run a hands-on lab from iamops.dev, and I would like your help to do it safely.

Lab: Lab di proof of possession per agenti: perché un token copiato non basta
Read the whole lab here (Markdown): https://iamops.dev/it/agent-proof-of-possession-lab.md

What the lab needs before the first step:
- Python 3, curl, jq, openssl and tr

How I want you to work:
1. Read the lab from start to finish first (each step lists its own prerequisites). Then check that the prerequisites are in place and that the tool versions match what the lab expects. Tell me what is missing before you run anything.
2. Run one step at a time. Before each command, show it to me and say in a sentence what it does.
3. Ask me before anything that costs money, creates cloud resources, touches a real tenant or account, or deletes anything. Wait for my answer.
4. Use the placeholders from the lab. Never paste real secrets, tokens or production identities into a command, a file or this chat. If a value is needed, tell me where to set it myself.
5. After each step, compare the output with the expected output in the lab. If it does not match, stop and tell me what differs. Do not carry on or invent a fix without asking me.
6. At the end, report what passed, what failed and which cleanup you did. Then offer to tick the steps off for me. The lab page keeps ticks only in my browser, so if you cannot do that, list the steps I should tick.

This lab is for test environments only. Do not run it against production, or against a tenant or account that holds real data or real users.

You may answer in my language.

Un bearer token funziona per chiunque lo possieda. Se il token di un agente viene copiato, anche la copia funziona, a meno che il server di risorse non controlli qualcosa che la copia non ha. Questo lab lega il token a una chiave e fa controllare al server di risorse una prova firmata a ogni chiamata.

Il lab usa un solo file Python con la libreria standard, più openssl, curl e jq. L’emittente lega il token alla chiave pubblica dell’agente. Il server di risorse controlla il token, poi la prova. La prova è firmata con la chiave privata dell’agente e copre il metodo, il percorso e un hash del token.

Questa è una demo generica. Segue l’idea di proof of possession della RFC 9449 e non è un’implementazione della specifica per agenti. Quella specifica non è ancora pubblicata, quindi i dettagli specifici per agenti sono segnati come da tenere d’occhio e non li presento come acquisiti.

Preparazione#

Servono Python 3, curl, jq, openssl e tr. L’emittente ascolta su 127.0.0.1 alla porta 4901 e il server di risorse alla porta 4902. Non serve nessun account e, dopo il download, nulla lascia il computer.

Il servizio è un solo file, pop_lab.py, e le funzioni di supporto sono in agent_proof.sh.

Passo 1: Scarica il servizio e crea due chiavi#

bash
mkdir -p pop-lab/run && cd pop-lab
curl -s -O https://iamops-dev.github.io/lab-files/agent-proof-of-possession-lab/pop_lab.py
curl -s -O https://iamops-dev.github.io/lab-files/agent-proof-of-possession-lab/agent_proof.sh
openssl ecparam -name prime256v1 -genkey -noout -out issuer.key.pem
openssl ec -in issuer.key.pem -pubout -out issuer.pub.pem 2>/dev/null
openssl ecparam -name prime256v1 -genkey -noout -out agent.key.pem
openssl ec -in agent.key.pem -pubout -out agent.pub.pem 2>/dev/null
chmod 600 issuer.key.pem agent.key.pem

Le chiavi private restano in file che solo tu puoi leggere. Il passo di pulizia le elimina.

Passo 2: Avvia i due servizi#

bash
export ISSUER=http://127.0.0.1:4901 RESOURCE=http://127.0.0.1:4902
python3 -I pop_lab.py --role issuer --port 4901 --key issuer.key.pem --log run/issuer.log > run/issuer.out 2>&1 &
echo $! >> run/pids
python3 -I pop_lab.py --role resource --port 4902 --issuer-pub issuer.pub.pem --log run/resource.log > run/resource.out 2>&1 &
echo $! >> run/pids
sleep 1
cat run/issuer.out run/resource.out
Output del terminale: i due comandi di avvio in background, poi il cat dei file di output che mostra l'emittente in ascolto sulla 4901 e il server di risorse sulla 4902
Passo 2. Entrambi i servizi ascoltano su 127.0.0.1.

Passo 3: Controlla gli endpoint di stato#

bash
curl -s "$ISSUER/healthz" | jq -c .
curl -s "$RESOURCE/healthz" | jq -c .
Output del terminale: due righe JSON di stato, una per l'emittente e una per il server di risorse, entrambe con ok true
Passo 3. Entrambi i servizi rispondono.

Passo 4: Ottieni un token legato alla chiave dell’agente#

bash
source ./agent_proof.sh
TOKEN=$(curl -s -X POST "$ISSUER/token" -d agent=demo-agent -d scope=calendar.read --data-urlencode "public_key=$(cat agent.pub.pem)" | jq -r .access_token)
decode_part "$(printf '%s' "$TOKEN" | cut -d. -f2)" | jq -r '"sub: \(.sub)", "scope: \(.scope)", "cnf.jkt: \(.cnf.jkt)"'
Output del terminale: i claim decodificati del token, sub demo-agent, scope calendar.read e un'impronta cnf.jkt della chiave pubblica dell'agente
Passo 4. Il token contiene l'impronta della chiave in cnf.jkt.

Il claim cnf.jkt è l’impronta della chiave pubblica dell’agente. È quello che lega il token alla chiave.

Passo 5: Un bearer token da solo viene rifiutato#

bash
curl -s -o body.json -w "HTTP %{http_code}\n" -H "Authorization: Bearer $TOKEN" "$RESOURCE/v1/calendar"
jq -r 'del(.events) | to_entries[] | "\(.key): \(.value)"' body.json
Output del terminale: HTTP 401, errore proof_required, con il dettaglio che l'endpoint richiede un header Agent-Proof firmato con la chiave legata
Passo 5. Il token da solo viene rifiutato.

Passo 6: L’agente firma la richiesta e la chiamata riesce#

bash
PROOF=$(prove agent.key.pem GET /v1/calendar "$TOKEN")
curl -s -o body.json -w "HTTP %{http_code}\n" -H "Authorization: Bearer $TOKEN" -H "Agent-Proof: $PROOF" "$RESOURCE/v1/calendar"
jq -r 'del(.events) | to_entries[] | "\(.key): \(.value)"' body.json
Output del terminale: HTTP 200 con ok true, l'endpoint, l'agente demo-agent, lo scope calendar.read e proof checked
Passo 6. La prova firmata viene accettata.

La prova copre il metodo, il percorso, un timestamp, un identificativo usa-e-getta e un hash del token. Una prova per una richiesta non funziona per un altro percorso o un altro token.

Passo 7: La stessa prova non può essere usata due volte#

bash
curl -s -o body.json -w "HTTP %{http_code}\n" -H "Authorization: Bearer $TOKEN" -H "Agent-Proof: $PROOF" "$RESOURCE/v1/calendar"
jq -r 'del(.events) | to_entries[] | "\(.key): \(.value)"' body.json
Output del terminale: HTTP 401, errore replay, la prova è già stata usata una volta
Passo 7. La ripetizione viene rifiutata come replay.

Passo 8: Una prova con un’altra chiave viene rifiutata#

bash
openssl ecparam -name prime256v1 -genkey -noout -out attacker.key.pem
openssl ec -in attacker.key.pem -pubout -out attacker.pub.pem 2>/dev/null
ATTK=$(prove attacker.key.pem GET /v1/calendar "$TOKEN")
curl -s -o body.json -w "HTTP %{http_code}\n" -H "Authorization: Bearer $TOKEN" -H "Agent-Proof: $ATTK" "$RESOURCE/v1/calendar"
jq -r 'del(.events) | to_entries[] | "\(.key): \(.value)"' body.json
Output del terminale: HTTP 401, errore key_mismatch, la chiave della prova non è quella a cui il token è legato
Passo 8. Una chiave a cui il token non è legato viene rifiutata.

Passo 9: Una prova che sostituisce la chiave pubblica dell’agente fallisce la firma#

bash
FORGED=$(prove attacker.key.pem GET /v1/calendar "$TOKEN" agent.key.pem)
curl -s -o body.json -w "HTTP %{http_code}\n" -H "Authorization: Bearer $TOKEN" -H "Agent-Proof: $FORGED" "$RESOURCE/v1/calendar"
jq -r 'del(.events) | to_entries[] | "\(.key): \(.value)"' body.json
Output del terminale: HTTP 401, errore bad_signature, la firma della prova non viene verificata
Passo 9. Mettere la chiave pubblica giusta accanto a una firma sbagliata fallisce.

L’attaccante può copiare la chiave pubblica dall’header, ma non può firmare con la chiave privata. Il controllo della firma è quello che lo ferma.

Passo 10: Il vecchio endpoint accetta ancora il solo bearer token#

bash
curl -s -o body.json -w "HTTP %{http_code}\n" -H "Authorization: Bearer $TOKEN" "$RESOURCE/v1/legacy/calendar"
jq -r 'del(.events) | to_entries[] | "\(.key): \(.value)"' body.json
Output del terminale: HTTP 200 con ok true sull'endpoint legacy del calendario, e proof not required
Passo 10. L'endpoint legacy accetta il caso del token rubato.

Questa è la lezione del lab. Ogni endpoint che accetta il bearer token senza la prova fa funzionare un token copiato. La soluzione è richiedere la prova in ogni punto in cui il token viene accettato.

Passo 11: Leggi che cosa ha registrato il server di risorse#

bash
cat run/resource.log
Output del terminale: le righe di log del server di risorse, una per richiesta, con lo stato e il motivo
Passo 11. Ogni rifiuto ha un motivo nel log.

Passo 12: Pulizia#

bash
kill $(cat run/pids)
rm -f body.json agent.key.pem agent.pub.pem attacker.key.pem attacker.pub.pem issuer.key.pem issuer.pub.pem pop_lab.py agent_proof.sh
cd .. && rm -rf pop-lab
lsof -nP -iTCP:4901-4902 -sTCP:LISTEN || echo "no listener on 4901 or 4902"
pgrep -fl pop_lab.py || echo "no pop_lab.py process left"
Output del terminale: il controllo di pulizia, nessun ascoltatore sulla 4901 o 4902 e nessun processo pop_lab.py
Passo 12. I servizi e le chiavi sono spariti.

Che cosa non copre questo lab#

  • Il formato della prova è quello di questo lab, non il formato della specifica per agenti. La specifica per agenti resta da tenere d’occhio finché non sarà pubblicata.
  • Emittente e server di risorse condividono una sola configurazione di fiducia locale e girano ciascuno in un processo. Un’installazione reale richiede rotazione delle chiavi, un archivio delle chiavi e durate dei token adeguate.
  • L’endpoint legacy c’è apposta, per mostrare la falla.

Ripeti questo lab#

Servono Python 3 (senza pacchetti extra), curl, jq, openssl e tr. Tutto gira sul tuo computer, sulle porte 4901 e 4902, senza account e senza accesso a internet dopo il download. Calcola circa 15 minuti. Questa è una demo generica di proof of possession, non la specifica AAuth, che non è ancora pubblicata.

Ogni valore tra parentesi angolari è un segnaposto per il tuo ambiente di test. Nulla in questa pagina è un identificativo o un segreto reale.

Imposta le variabili una sola volta

bash · env.sh
export ISSUER=http://127.0.0.1:4901 RESOURCE=http://127.0.0.1:4902
Passo 1Scarica il servizio e crea due chiavi

Obiettivo. Crea una cartella di lavoro, scarica il servizio e le funzioni di supporto, e crea una chiave dell'emittente e una chiave dell'agente che solo tu puoi leggere.

Prerequisiti

  • Python 3, curl, jq, openssl e tr
bash · setup.sh
mkdir -p pop-lab/run && cd pop-lab
curl -s -O https://iamops-dev.github.io/lab-files/agent-proof-of-possession-lab/pop_lab.py
curl -s -O https://iamops-dev.github.io/lab-files/agent-proof-of-possession-lab/agent_proof.sh
openssl ecparam -name prime256v1 -genkey -noout -out issuer.key.pem
openssl ec -in issuer.key.pem -pubout -out issuer.pub.pem 2>/dev/null
openssl ecparam -name prime256v1 -genkey -noout -out agent.key.pem
openssl ec -in agent.key.pem -pubout -out agent.pub.pem 2>/dev/null
chmod 600 issuer.key.pem agent.key.pem

Output atteso

output
(no output)
Passo 2Avvia l'emittente e il server di risorse

Obiettivo. Avvia entrambi i servizi su 127.0.0.1. L'emittente firma i token, e il server di risorse controlla il token e la prova a ogni chiamata.

Prerequisiti

  • Il passo di preparazione è completato
  • Le porte 4901 e 4902 sono libere
bash · start.sh
python3 -I pop_lab.py --role issuer --port 4901 --key issuer.key.pem --log run/issuer.log > run/issuer.out 2>&1 &
echo $! >> run/pids
python3 -I pop_lab.py --role resource --port 4902 --issuer-pub issuer.pub.pem --log run/resource.log > run/resource.out 2>&1 &
echo $! >> run/pids
sleep 1
cat run/issuer.out run/resource.out

Output atteso

output
issuer listening on 127.0.0.1:4901
resource listening on 127.0.0.1:4902
Passo 3Controlla entrambi gli endpoint di stato

Obiettivo. Verifica che entrambi i servizi rispondano prima di inviare qualsiasi richiesta.

Prerequisiti

  • Il passo di avvio è completato
bash · health.sh
curl -s "$ISSUER/healthz" | jq -c .
curl -s "$RESOURCE/healthz" | jq -c .

Output atteso

output
{"role":"issuer","ok":true}
{"role":"resource","ok":true}
Passo 4Ottieni un token legato alla chiave dell'agente

Obiettivo. L'emittente restituisce un token per l'agente. Il token porta un claim di conferma, cnf.jkt, che è l'impronta della chiave pubblica dell'agente.

Prerequisiti

  • Il passo di controllo è completato
bash · token.sh
source ./agent_proof.sh
TOKEN=$(curl -s -X POST "$ISSUER/token" -d agent=demo-agent -d scope=calendar.read --data-urlencode "public_key=$(cat agent.pub.pem)" | jq -r .access_token)
decode_part "$(printf '%s' "$TOKEN" | cut -d. -f2)" | jq -r '"sub: \(.sub)", "scope: \(.scope)", "cnf.jkt: \(.cnf.jkt)"'

Output atteso

output
sub: demo-agent
scope: calendar.read
cnf.jkt: _AjEqWefsVfSdbOwV9dDsLuYAJia9ZPF9PrLfV2N37A
Passo 5Un bearer token da solo viene rifiutato

Obiettivo. Invia il token senza prova. Il server di risorse lo rifiuta, perché un bearer token copiato non basta.

Prerequisiti

  • Il passo del token è completato
bash · bearer-only.sh
curl -s -o body.json -w "HTTP %{http_code}\n" -H "Authorization: Bearer $TOKEN" "$RESOURCE/v1/calendar"
jq -r 'del(.events) | to_entries[] | "\(.key): \(.value)"' body.json

Output atteso

output
HTTP 401
error: proof_required
detail: this endpoint needs an Agent-Proof header signed with the key the token is bound to
Passo 6L'agente firma la richiesta e la chiamata riesce

Obiettivo. Firma il metodo, il percorso e l'hash del token con la chiave dell'agente, e invia la prova in un header insieme al token.

Prerequisiti

  • Il passo del token è completato
bash · proof.sh
PROOF=$(prove agent.key.pem GET /v1/calendar "$TOKEN")
curl -s -o body.json -w "HTTP %{http_code}\n" -H "Authorization: Bearer $TOKEN" -H "Agent-Proof: $PROOF" "$RESOURCE/v1/calendar"
jq -r 'del(.events) | to_entries[] | "\(.key): \(.value)"' body.json

Output atteso

output
HTTP 200
ok: true
endpoint: /v1/calendar
agent: demo-agent
scope: calendar.read
proof: checked
Passo 7La stessa prova non può essere usata due volte

Obiettivo. Invia una seconda volta la prova del passo precedente. Ogni prova porta un identificativo usa-e-getta, e il server di risorse rifiuta la ripetizione.

Prerequisiti

  • Il passo della prova è completato
bash · replay.sh
curl -s -o body.json -w "HTTP %{http_code}\n" -H "Authorization: Bearer $TOKEN" -H "Agent-Proof: $PROOF" "$RESOURCE/v1/calendar"
jq -r 'del(.events) | to_entries[] | "\(.key): \(.value)"' body.json

Output atteso

output
HTTP 401
error: replay
detail: this proof was already used once
Passo 8Una prova con un'altra chiave viene rifiutata

Obiettivo. Crea una seconda chiave, firma una prova con essa e inviala insieme al token dell'agente. Il token è legato alla prima chiave, quindi la chiamata fallisce.

Prerequisiti

  • Il passo del token è completato
bash · other-key.sh
openssl ecparam -name prime256v1 -genkey -noout -out attacker.key.pem
openssl ec -in attacker.key.pem -pubout -out attacker.pub.pem 2>/dev/null
ATTK=$(prove attacker.key.pem GET /v1/calendar "$TOKEN")
curl -s -o body.json -w "HTTP %{http_code}\n" -H "Authorization: Bearer $TOKEN" -H "Agent-Proof: $ATTK" "$RESOURCE/v1/calendar"
jq -r 'del(.events) | to_entries[] | "\(.key): \(.value)"' body.json

Output atteso

output
HTTP 401
error: key_mismatch
detail: the proof key is not the key this token is bound to
Passo 9Una prova che sostituisce la chiave pubblica dell'agente fallisce la firma

Obiettivo. Firma con la seconda chiave, ma metti nell'header la chiave pubblica dell'agente. La chiave corrisponde al token, ma la firma non si verifica.

Prerequisiti

  • Il passo con un'altra chiave è completato
bash · forged-key.sh
FORGED=$(prove attacker.key.pem GET /v1/calendar "$TOKEN" agent.key.pem)
curl -s -o body.json -w "HTTP %{http_code}\n" -H "Authorization: Bearer $TOKEN" -H "Agent-Proof: $FORGED" "$RESOURCE/v1/calendar"
jq -r 'del(.events) | to_entries[] | "\(.key): \(.value)"' body.json

Output atteso

output
HTTP 401
error: bad_signature
detail: the proof signature does not verify
Passo 10Il vecchio endpoint accetta ancora il solo bearer token

Obiettivo. Chiama l'endpoint legacy con il solo bearer token. Lo accetta, ed è la falla che userebbe un token rubato.

Prerequisiti

  • Il passo del token è completato
bash · legacy.sh
curl -s -o body.json -w "HTTP %{http_code}\n" -H "Authorization: Bearer $TOKEN" "$RESOURCE/v1/legacy/calendar"
jq -r 'del(.events) | to_entries[] | "\(.key): \(.value)"' body.json

Output atteso

output
HTTP 200
ok: true
endpoint: /v1/legacy/calendar
agent: demo-agent
scope: calendar.read
proof: not required
Passo 11Pulizia

Obiettivo. Ferma entrambi i servizi, elimina le chiavi e i file del servizio, rimuovi la cartella di lavoro e verifica che nessuno ascolti sulle due porte.

Prerequisiti

  • I passi precedenti sono completati
bash · cleanup.sh
kill $(cat run/pids)
rm -f body.json agent.key.pem agent.pub.pem attacker.key.pem attacker.pub.pem issuer.key.pem issuer.pub.pem pop_lab.py agent_proof.sh
cd .. && rm -rf pop-lab
lsof -nP -iTCP:4901-4902 -sTCP:LISTEN || echo "no listener on 4901 or 4902"
pgrep -fl pop_lab.py || echo "no pop_lab.py process left"

Output atteso

output
no listener on 4901 or 4902
no pop_lab.py process left

Fonti#

Discutine su X o LinkedIn

Segui su X

Nuovi articoli via email

Una email a settimana con nuovi articoli e lab. Disiscrizione in qualsiasi momento.

Il tuo indirizzo è salvato su Kit, il servizio che invia le email. Dettagli

Ricerche e laboratori correlati

Scrivi per cercare in tutte le pagine. Usa le frecce per scorrere i risultati.

    Apri la pagina di ricerca