-
Notifications
You must be signed in to change notification settings - Fork 14
feat: implemented JWKS token decoder #54
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
86ec196
fix: allow versions
netbull 942ae53
feat: implemented JSON Web Key Set (JWKS) token decoder
SLRBot f2f608a
Update src/Token/JWKSTokenDecoder.php
netbull ba42bc7
Update src/Token/JWKSTokenDecoder.php
netbull 94dd65a
Update src/Token/JWKSTokenDecoder.php
netbull 9a0bec7
Update src/Token/JWKSTokenDecoder.php
mainick fcd3c54
Update src/Token/JWKSTokenDecoder.php
mainick 0607410
Update src/Token/JWKSTokenDecoder.php
mainick a0ae2e7
Update src/Token/JWKSTokenDecoder.php
mainick 6abbd63
Update src/Token/JWKSTokenDecoder.php
mainick fb93ab8
Update src/Token/TokenDecoderFactory.php
mainick ecf597c
Update src/Token/JWKSTokenDecoder.php
mainick ca3416f
Update src/Token/JWKSTokenDecoder.php
mainick 82be274
Merge branch 'main' into main
mainick 1b6c914
feat: add validation for JWKS endpoint URL and implement domain white…
mainick 87bfc18
feat: enhance token decoding with improved error handling and validation
mainick c34c6fc
feat: enhance token decoding with improved error handling and validation
mainick 378bae4
Merge remote-tracking branch 'netbull/main' into fork/netbull/main
mainick File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,131 @@ | ||
| # Security Features | ||
|
|
||
| ## JWKS Endpoint URL Validation | ||
|
|
||
| ### Overview | ||
|
|
||
| Per prevenire attacchi SSRF (Server-Side Request Forgery), il bundle implementa una validazione rigorosa dell'URL del JWKS endpoint. | ||
|
|
||
| ### Validazione dell'URL Base | ||
|
|
||
| L'URL base di Keycloak (`base_url`) viene validato nel costruttore di `JWKSTokenDecoder` per garantire: | ||
|
|
||
| 1. **Formato URL valido**: L'URL deve avere uno schema (scheme) e un host validi | ||
| 2. **Solo HTTPS**: Viene forzato l'uso di HTTPS per ambienti non-localhost | ||
| 3. **Blocco IP privati**: Gli indirizzi IP privati (RFC 1918) sono bloccati automaticamente | ||
| 4. **Blocco endpoint metadata**: Gli endpoint di metadata cloud (es. 169.254.169.254) sono bloccati | ||
| 5. **Localhost consentito**: HTTP è consentito solo per localhost (127.0.0.1, ::1, localhost) | ||
|
|
||
| ### Whitelist Domini JWKS | ||
|
|
||
| È possibile configurare una whitelist di domini autorizzati per le richieste JWKS: | ||
|
|
||
| ```yaml | ||
| # config/packages/mainick_keycloak_client.yaml | ||
|
|
||
| mainick_keycloak_client: | ||
| keycloak: | ||
| base_url: '%env(IAM_BASE_URL)%' | ||
| realm: '%env(IAM_REALM)%' | ||
| # ... altre configurazioni ... | ||
|
|
||
| # Whitelist di domini consentiti per l'endpoint JWKS | ||
| allowed_jwks_domains: | ||
| - 'keycloak.example.com' | ||
| - '*.auth.example.com' # Supporta wildcard per sottodomini | ||
| ``` | ||
|
|
||
| ### Comportamento Predefinito | ||
|
|
||
| Se non viene specificata alcuna whitelist (`allowed_jwks_domains`), il bundle consente **solo** il dominio presente in `base_url`. | ||
|
|
||
| Esempio: | ||
| - Se `base_url` è `https://keycloak.example.com`, solo questo dominio sarà consentito per le richieste JWKS | ||
| - Qualsiasi tentativo di reindirizzamento o richiesta a un dominio diverso verrà bloccato | ||
|
|
||
| ### Wildcard per Sottodomini | ||
|
|
||
| È possibile utilizzare wildcard per consentire tutti i sottodomini di un dominio specifico: | ||
|
|
||
| ```yaml | ||
| allowed_jwks_domains: | ||
| - '*.example.com' # Consente auth.example.com, keycloak.example.com, ecc. | ||
| ``` | ||
|
|
||
| **Nota**: Il wildcard `*.example.com` consente sia `auth.example.com` che `example.com` stesso. | ||
|
|
||
| ### Validazione HTTPS | ||
|
|
||
| Per gli ambienti non-localhost, l'endpoint JWKS **deve** utilizzare HTTPS. Qualsiasi tentativo di utilizzare HTTP per domini pubblici verrà rifiutato con un'eccezione. | ||
|
|
||
| ### Eccezioni di Sicurezza | ||
|
|
||
| Quando viene rilevata una violazione di sicurezza, viene lanciata un'eccezione `TokenDecoderException` con un messaggio dettagliato che indica: | ||
|
|
||
| - Il dominio non autorizzato | ||
| - Il motivo del rifiuto (non nella whitelist, IP privato, ecc.) | ||
|
|
||
| Esempio di messaggio di errore: | ||
| ``` | ||
| Invalid token: JWKS URL host "malicious.com" is not in the allowed domains whitelist | ||
| ``` | ||
|
|
||
| ### Hosts Bloccati | ||
|
|
||
| I seguenti pattern di host sono automaticamente bloccati: | ||
|
|
||
| - Indirizzi IP privati (10.0.0.0/8, 172.16.0.0/12, 192.168.0.0/16) | ||
| - Indirizzi IP riservati (169.254.0.0/16, 224.0.0.0/4, 240.0.0.0/4) | ||
| - Endpoint metadata cloud: | ||
| - `metadata.google.internal` | ||
| - `169.254.169.254` (AWS metadata) | ||
| - Qualsiasi host contenente `metadata` o `internal` | ||
|
|
||
| ### Best Practices | ||
|
|
||
| 1. **Ambiente di produzione**: Specificare sempre una whitelist esplicita di domini autorizzati | ||
| 2. **HTTPS obbligatorio**: Non utilizzare HTTP per domini pubblici | ||
| 3. **Minimizzare la whitelist**: Includere solo i domini strettamente necessari | ||
| 4. **Evitare wildcard ampi**: Preferire domini specifici quando possibile | ||
| 5. **Monitoraggio**: Registrare e monitorare le eccezioni `TokenDecoderException` per rilevare potenziali tentativi di attacco | ||
|
|
||
| ### Esempio di Configurazione Sicura | ||
|
|
||
| ```yaml | ||
| mainick_keycloak_client: | ||
| keycloak: | ||
| verify_ssl: true | ||
| base_url: 'https://keycloak.example.com' | ||
| realm: 'production' | ||
| client_id: '%env(IAM_CLIENT_ID)%' | ||
| client_secret: '%env(IAM_CLIENT_SECRET)%' | ||
| # ... altre configurazioni ... | ||
|
|
||
| # Whitelist rigorosa per l'ambiente di produzione | ||
| allowed_jwks_domains: | ||
| - 'keycloak.example.com' | ||
| - 'auth.example.com' | ||
| ``` | ||
|
|
||
| ### Test di Sicurezza | ||
|
|
||
| Il bundle include test completi per verificare: | ||
|
|
||
| - Validazione del formato URL | ||
| - Rifiuto di HTTP per domini non-localhost | ||
| - Blocco di IP privati | ||
| - Blocco di endpoint metadata | ||
| - Funzionamento della whitelist domini | ||
| - Supporto wildcard per sottodomini | ||
| - Validazione HTTPS per endpoint JWKS | ||
|
|
||
| Per eseguire i test di sicurezza: | ||
|
|
||
| ```bash | ||
| ./vendor/bin/phpunit tests/Token/JWKSTokenDecoderTest.php | ||
| ``` | ||
|
|
||
| ## Segnalazione Vulnerabilità | ||
|
|
||
| Se scopri una vulnerabilità di sicurezza, ti preghiamo di **NON** aprire un issue pubblico. Invia invece una segnalazione privata seguendo le linee guida nel file `SECURITY.md` nella root del progetto. | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.