|
| 1 | +/* |
| 2 | + * Nuts node |
| 3 | + * Copyright (C) 2024 Nuts community |
| 4 | + * |
| 5 | + * This program is free software: you can redistribute it and/or modify |
| 6 | + * it under the terms of the GNU General Public License as published by |
| 7 | + * the Free Software Foundation, either version 3 of the License, or |
| 8 | + * (at your option) any later version. |
| 9 | + * |
| 10 | + * This program is distributed in the hope that it will be useful, |
| 11 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | + * GNU General Public License for more details. |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU General Public License |
| 16 | + * along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 17 | + */ |
| 18 | + |
| 19 | +package echo |
| 20 | + |
| 21 | +// TestDPoP_StripNilDerefDoS is a regression test for a nil pointer dereference in |
| 22 | +// crypto/dpop.strip() when the DPoP token's htu claim contains an invalid |
| 23 | +// percent-encoded sequence (e.g. "%zz"). url.Parse returns (nil, err) for such |
| 24 | +// input; without the fix the function dereferenced the nil pointer, causing a panic. |
| 25 | +// |
| 26 | +// Attack path (before fix): |
| 27 | +// Attacker crafts a DPoP JWT with htu="%zz", signs it with their own key, |
| 28 | +// and POSTs it to POST /internal/auth/v2/dpop/validate. |
| 29 | +// dpop.Parse() accepted the token (htu is non-empty → passes the only check). |
| 30 | +// dpopToken.Match() called strip(token.HTU()) → url.Parse("%zz") → nil → PANIC. |
| 31 | +// |
| 32 | +// Expected outcome (after fix): |
| 33 | +// dpop.Parse() rejects the token because the htu value is not a valid URL. |
| 34 | +// ValidateDPoPProof returns 200 with Valid=false and a reason string. |
| 35 | + |
| 36 | +import ( |
| 37 | + "bytes" |
| 38 | + "crypto" |
| 39 | + "crypto/ecdsa" |
| 40 | + "crypto/elliptic" |
| 41 | + "crypto/rand" |
| 42 | + "encoding/base64" |
| 43 | + "encoding/json" |
| 44 | + "net/http" |
| 45 | + "testing" |
| 46 | + |
| 47 | + "github.com/lestrrat-go/jwx/v2/jwa" |
| 48 | + "github.com/lestrrat-go/jwx/v2/jwk" |
| 49 | + "github.com/nuts-foundation/nuts-node/crypto/dpop" |
| 50 | + "github.com/nuts-foundation/nuts-node/test/node" |
| 51 | + "github.com/stretchr/testify/assert" |
| 52 | + "github.com/stretchr/testify/require" |
| 53 | +) |
| 54 | + |
| 55 | +func TestDPoP_StripNilDerefDoS(t *testing.T) { |
| 56 | + internalBaseURL, _, _ := node.StartServer(t) |
| 57 | + |
| 58 | + // Generate an EC key pair. The DPoP spec requires the public key in the JWT header, |
| 59 | + // so we need a real key to produce a valid signature that passes dpop.Parse(). |
| 60 | + keyPair, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader) |
| 61 | + require.NoError(t, err) |
| 62 | + |
| 63 | + // Build a syntactically valid DPoP token using a placeholder URL, then overwrite |
| 64 | + // the htu claim with an invalid percent-encoded string. "%zz" is invalid because |
| 65 | + // 'z' is not a hexadecimal digit, so url.Parse will return (nil, error). |
| 66 | + req, err := http.NewRequest(http.MethodPost, "https://example.com/token", nil) |
| 67 | + require.NoError(t, err) |
| 68 | + |
| 69 | + token := dpop.New(*req) |
| 70 | + // Overwrite htu with a malformed percent-encoded value. |
| 71 | + // dpop.Parse() only checks !ok || v == "", so "%zz" passes that guard. |
| 72 | + require.NoError(t, token.Token.Set(dpop.HTUKey, "%zz")) |
| 73 | + |
| 74 | + dpopProof, err := token.Sign("kid", keyPair, jwa.ES256) |
| 75 | + require.NoError(t, err) |
| 76 | + |
| 77 | + // Compute the JWK thumbprint that the server will verify. Match() checks the |
| 78 | + // thumbprint first; an incorrect value short-circuits before strip() is reached. |
| 79 | + pubJWK, err := jwk.FromRaw(keyPair.Public()) |
| 80 | + require.NoError(t, err) |
| 81 | + thumbprintBytes, err := pubJWK.Thumbprint(crypto.SHA256) |
| 82 | + require.NoError(t, err) |
| 83 | + thumbprint := base64.RawURLEncoding.EncodeToString(thumbprintBytes) |
| 84 | + |
| 85 | + // POST to the internal validate endpoint. Any authenticated caller of the |
| 86 | + // internal API (e.g. a resource server) can reach this endpoint. |
| 87 | + body, err := json.Marshal(map[string]string{ |
| 88 | + "dpop_proof": dpopProof, |
| 89 | + "method": http.MethodPost, |
| 90 | + "thumbprint": thumbprint, |
| 91 | + "token": "dummy-access-token", |
| 92 | + "url": "https://example.com/token", |
| 93 | + }) |
| 94 | + require.NoError(t, err) |
| 95 | + |
| 96 | + resp, err := http.Post( |
| 97 | + internalBaseURL+"/internal/auth/v2/dpop/validate", |
| 98 | + "application/json", |
| 99 | + bytes.NewReader(body), |
| 100 | + ) |
| 101 | + require.NoError(t, err, "server should not close the connection (no panic)") |
| 102 | + defer resp.Body.Close() |
| 103 | + |
| 104 | + // The fix rejects the token at dpop.Parse() time (invalid htu URL), so the |
| 105 | + // server returns 200 with Valid=false rather than panicking. |
| 106 | + require.Equal(t, http.StatusOK, resp.StatusCode) |
| 107 | + var result map[string]interface{} |
| 108 | + require.NoError(t, json.NewDecoder(resp.Body).Decode(&result)) |
| 109 | + assert.Equal(t, false, result["valid"], "valid should be false for an invalid DPoP token") |
| 110 | + assert.NotEmpty(t, result["reason"], "a human-readable rejection reason should be present") |
| 111 | +} |
0 commit comments