Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/vm-agent/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ toolchain go1.26.11
godebug multipathtcp=0

require (
github.com/MicahParks/keyfunc/v3 v3.9.1
github.com/MicahParks/keyfunc/v3 v3.9.2
github.com/coder/acp-go-sdk v0.13.5
github.com/compose-spec/compose-go/v2 v2.16.2
github.com/creack/pty v1.1.24
Expand Down
4 changes: 2 additions & 2 deletions packages/vm-agent/go.sum
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
github.com/MicahParks/jwkset v0.11.4 h1:sm6pqlEpa1YDu82Hoxho/O+Y291lPcksNImVIsV74gg=
github.com/MicahParks/jwkset v0.11.4/go.mod h1:U2oRhRaLgDCLjtpGL2GseNKGmZtLs/3O7p+OZaL5vo0=
github.com/MicahParks/keyfunc/v3 v3.9.1 h1:10dlh2kS3kRtfj5x2bokCxLhTpmIpf3WtsvBdJ2TfOo=
github.com/MicahParks/keyfunc/v3 v3.9.1/go.mod h1:h8DMhdulFa3yjG1q74FqaehNDekB18HrVCXPl9Raa4I=
github.com/MicahParks/keyfunc/v3 v3.9.2 h1:yWvqmCIWMnyp8tJFR3VmNQRBGmNRJ3cMvN9bcG9Bfbc=
github.com/MicahParks/keyfunc/v3 v3.9.2/go.mod h1:h8DMhdulFa3yjG1q74FqaehNDekB18HrVCXPl9Raa4I=
github.com/coder/acp-go-sdk v0.13.5 h1:LI9jq5xon7xslaYlnoktvTVyDlE37yIk2daT7N9ASYk=
github.com/coder/acp-go-sdk v0.13.5/go.mod h1:yKzM/3R9uELp4+nBAwwtkS0aN1FOFjo11CNPy37yFko=
github.com/compose-spec/compose-go/v2 v2.16.2 h1:pTp1DqQwJzFd85GzlWsSszKA1ABrV/ySqsClujIcWA8=
Expand Down
74 changes: 74 additions & 0 deletions packages/vm-agent/internal/auth/jwt_dependency_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package auth

import (
"crypto/rand"
"crypto/rsa"
"net/http"
"net/http/httptest"
"strings"
"testing"
"time"

"github.com/golang-jwt/jwt/v5"
)

// Exercise keyfunc's no-kid lookup through the real SAM validator and an HTTP
// JWKS response. A valid signature must not override a conflicting JWK alg.
func TestJWTValidatorEnforcesJWKAlgorithmWithoutKeyID(t *testing.T) {
for _, tc := range []struct {
name string
jwkAlg string
withKid bool
wantErr bool
}{
{name: "matching algorithm without kid", jwkAlg: "RS256"},
{name: "mismatched algorithm without kid", jwkAlg: "RS512", wantErr: true},
{name: "normal keyed token", jwkAlg: "RS256", withKid: true},
{name: "mismatched algorithm with kid", jwkAlg: "RS512", withKid: true, wantErr: true},
} {
t.Run(tc.name, func(t *testing.T) {
key, err := rsa.GenerateKey(rand.Reader, 2048)
if err != nil {
t.Fatal(err)
}
jwks := strings.Replace(string(buildJWKSJSON(&key.PublicKey)), `"alg":"RS256"`, `"alg":"`+tc.jwkAlg+`"`, 1)
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
w.Header().Set("Content-Type", "application/json")
_, _ = w.Write([]byte(jwks))
}))
t.Cleanup(server.Close)
validator, err := NewJWTValidator(server.URL, "node-1", "test-issuer", "node-management")
if err != nil {
t.Fatal(err)
}
t.Cleanup(validator.Close)
token := jwt.NewWithClaims(jwt.SigningMethodRS256, Claims{
RegisteredClaims: jwt.RegisteredClaims{
Issuer: "test-issuer", Audience: jwt.ClaimStrings{"node-management"},
ExpiresAt: jwt.NewNumericDate(time.Now().Add(time.Minute)),
},
Node: "node-1", Workspace: "workspace-1",
})
if tc.withKid {
token.Header["kid"] = "test-key-1"
}
signed, err := token.SignedString(key)
if err != nil {
t.Fatal(err)
}
claims, err := validator.ValidateNodeManagementToken(signed, "workspace-1")
if tc.wantErr {
if err == nil {
t.Fatal("accepted token with a conflicting JWK algorithm")
}
return
}
if err != nil {
t.Fatalf("valid token rejected: %v", err)
}
if claims.Node != "node-1" || claims.Workspace != "workspace-1" {
t.Fatalf("unexpected identity: node=%q workspace=%q", claims.Node, claims.Workspace)
}
})
}
}
Loading