mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2026-05-12 22:10:25 +00:00
Protect OIDC tokens generated by Forgejo Actions from threats arising when users or repositories are renamed or deleted, freeing their names up for reuse by another user. In this threat environment, relying on the name of users and repositories in validating JWT claims is unsafe because they can change. Adds three new claims to Actions' OIDC tokens: - `actor_id` -- the immutable identifier of the actor who triggered an Action run - `repository_id` -- the immutable identifier of the repository on which the Action is running - `repository_owner_id` -- the immutable identifier of the owner of the repository on which the Action is running Repositories will change their subject (`sub`) OIDC claims to include these immutable identifiers. Existing repositories will not change, in order to maintain compatibility with existing JWT usage. The new format will be applied to new repositories, or can be applied by disabling and enabling the Actions unit. The new format embeds the identifiers: - **Existing repos:** `repo:my-org/my-repo:ref:refs/heads/main` - **New repos:** `repo:my-org-123456/my-repo-456789:ref:refs/heads/main` Fixes #12244. ## Checklist The [contributor guide](https://forgejo.org/docs/next/contributor/) contains information that will be helpful to first time contributors. All work and communication must conform to Forgejo's [AI Agreement](https://codeberg.org/forgejo/governance/src/branch/main/AIAgreement.md). There also are a few [conditions for merging Pull Requests in Forgejo repositories](https://codeberg.org/forgejo/governance/src/branch/main/PullRequestsAgreement.md). You are also welcome to join the [Forgejo development chatroom](https://matrix.to/#/#forgejo-development:matrix.org). ### Tests for Go changes - I added test coverage for Go changes... - [x] in their respective `*_test.go` for unit tests. - [ ] in the `tests/integration` directory if it involves interactions with a live Forgejo server. - I ran... - [x] `make pr-go` before pushing ### Documentation - [x] I created a pull request [to the documentation](https://codeberg.org/forgejo/docs) to explain to Forgejo users how to use this change. - New fields will be added to documentation soon. - [ ] I did not document these changes and I do not expect someone else to do it. ### Release notes - [x] This change will be noticed by a Forgejo user or admin (feature, bug fix, performance, etc.). I suggest to include a release note for this change. - [ ] This change is not visible to a Forgejo user or admin (refactor, dependency upgrade, etc.). I think there is no need to add a release note for this change. Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/12355 Reviewed-by: Andreas Ahlenstorf <aahlenst@noreply.codeberg.org>
255 lines
8.2 KiB
Go
255 lines
8.2 KiB
Go
// Copyright 2024 The Gitea Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package actions
|
|
|
|
import (
|
|
"net/http"
|
|
"testing"
|
|
|
|
actions_model "forgejo.org/models/actions"
|
|
repo_model "forgejo.org/models/repo"
|
|
"forgejo.org/modules/json"
|
|
"forgejo.org/modules/setting"
|
|
|
|
"github.com/golang-jwt/jwt/v5"
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestCreateAuthorizationToken(t *testing.T) {
|
|
task := &actions_model.ActionTask{
|
|
ID: 23,
|
|
Job: &actions_model.ActionRunJob{
|
|
ID: 2,
|
|
RunID: 1,
|
|
},
|
|
}
|
|
|
|
testcases := []struct {
|
|
name string
|
|
enableOpenIDConnect bool
|
|
gitCtx map[string]any
|
|
}{
|
|
{
|
|
name: "enableOpenIDConnect false",
|
|
enableOpenIDConnect: false,
|
|
gitCtx: map[string]any{},
|
|
},
|
|
{
|
|
name: "enableOpenIDConnect true",
|
|
enableOpenIDConnect: true,
|
|
gitCtx: map[string]any{
|
|
"actor": "user1",
|
|
"actor_id": "123",
|
|
"base_ref": "master",
|
|
"event_name": "push",
|
|
"head_ref": "master",
|
|
"ref": "refs/heads/master",
|
|
"ref_protected": "false",
|
|
"ref_type": "branch",
|
|
"repository": "mpminardi/testing",
|
|
"repository_id": "456",
|
|
"repository_owner": "mpminardi",
|
|
"repository_owner_id": "789",
|
|
"run_attempt": "1",
|
|
"run_id": "1",
|
|
"run_number": "1",
|
|
"sha": "pretend-sha",
|
|
"workflow": "test.yml",
|
|
"workflow_ref": "pretend-ref",
|
|
},
|
|
},
|
|
}
|
|
|
|
for _, tc := range testcases {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
token, err := CreateAuthorizationToken(task, tc.gitCtx, tc.enableOpenIDConnect,
|
|
&repo_model.ActionsConfig{})
|
|
require.NoError(t, err)
|
|
assert.NotEmpty(t, token)
|
|
claims := jwt.MapClaims{}
|
|
_, err = jwt.ParseWithClaims(token, claims, func(t *jwt.Token) (any, error) {
|
|
return setting.GetGeneralTokenSigningSecret(), nil
|
|
})
|
|
require.NoError(t, err)
|
|
scp, ok := claims["scp"]
|
|
assert.True(t, ok, "Has scp claim in jwt token")
|
|
assert.Contains(t, scp, "Actions.Results:1:2")
|
|
taskIDClaim, ok := claims["TaskID"]
|
|
assert.True(t, ok, "Has TaskID claim in jwt token")
|
|
assert.InDelta(t, float64(task.ID), taskIDClaim, 0, "Supplied taskid must match stored one")
|
|
acClaim, ok := claims["ac"]
|
|
assert.True(t, ok, "Has ac claim in jwt token")
|
|
ac, ok := acClaim.(string)
|
|
assert.True(t, ok, "ac claim is a string for buildx gha cache")
|
|
scopes := []actionsCacheScope{}
|
|
err = json.Unmarshal([]byte(ac), &scopes)
|
|
require.NoError(t, err, "ac claim is a json list for buildx gha cache")
|
|
assert.GreaterOrEqual(t, len(scopes), 1, "Expected at least one action cache scope for buildx gha cache")
|
|
|
|
if tc.enableOpenIDConnect {
|
|
assert.Contains(t, scp, "generate_id_token:1:2")
|
|
oidcSubClaim, ok := claims["oidc_sub"]
|
|
assert.True(t, ok, "Has oidc_sub claim in jwt token")
|
|
assert.Equal(t, "repo:mpminardi-789/testing-456:ref:refs/heads/master", oidcSubClaim)
|
|
oidcExtraClaim, ok := claims["oidc_extra"]
|
|
assert.True(t, ok, "Has oidc_extra claim in jwt token")
|
|
val, err := json.Marshal(tc.gitCtx)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, string(val), oidcExtraClaim)
|
|
} else {
|
|
assert.NotContains(t, scp, "generate_id_token")
|
|
_, ok := claims["oidc_sub"]
|
|
assert.False(t, ok, "Does not have oidc_sub claim in jwt token")
|
|
_, ok = claims["oidc_extra"]
|
|
assert.False(t, ok, "Does not have oidc_extra claim in jwt token")
|
|
}
|
|
|
|
token, err = CreateAuthorizationToken(task, tc.gitCtx, tc.enableOpenIDConnect,
|
|
&repo_model.ActionsConfig{OIDCSubjectFormat: repo_model.OIDCSubjectFormatLegacyForgejo15})
|
|
require.NoError(t, err)
|
|
assert.NotEmpty(t, token)
|
|
claims = jwt.MapClaims{}
|
|
_, err = jwt.ParseWithClaims(token, claims, func(t *jwt.Token) (any, error) {
|
|
return setting.GetGeneralTokenSigningSecret(), nil
|
|
})
|
|
require.NoError(t, err)
|
|
if tc.enableOpenIDConnect {
|
|
oidcSubClaim, ok := claims["oidc_sub"]
|
|
assert.True(t, ok, "Has oidc_sub claim in jwt token")
|
|
assert.Equal(t, "repo:mpminardi/testing:ref:refs/heads/master", oidcSubClaim)
|
|
} else {
|
|
assert.NotContains(t, scp, "generate_id_token")
|
|
_, ok := claims["oidc_sub"]
|
|
assert.False(t, ok, "Does not have oidc_sub claim in jwt token")
|
|
_, ok = claims["oidc_extra"]
|
|
assert.False(t, ok, "Does not have oidc_extra claim in jwt token")
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestParseAuthorizationToken(t *testing.T) {
|
|
task := &actions_model.ActionTask{
|
|
ID: 23,
|
|
Job: &actions_model.ActionRunJob{
|
|
ID: 2,
|
|
RunID: 1,
|
|
},
|
|
}
|
|
token, err := CreateAuthorizationToken(task, map[string]any{}, false, &repo_model.ActionsConfig{})
|
|
require.NoError(t, err)
|
|
assert.NotEmpty(t, token)
|
|
headers := http.Header{}
|
|
headers.Set("Authorization", "Bearer "+token)
|
|
rTaskID, err := ParseAuthorizationToken(&http.Request{
|
|
Header: headers,
|
|
})
|
|
require.NoError(t, err)
|
|
assert.Equal(t, task.ID, rTaskID)
|
|
}
|
|
|
|
func TestParseAuthorizationTokenClaims(t *testing.T) {
|
|
task := &actions_model.ActionTask{
|
|
ID: 23,
|
|
Job: &actions_model.ActionRunJob{
|
|
ID: 2,
|
|
RunID: 1,
|
|
},
|
|
}
|
|
gitCtx := map[string]any{
|
|
"actor": "user1",
|
|
"actor_id": "123",
|
|
"base_ref": "master",
|
|
"event_name": "push",
|
|
"head_ref": "master",
|
|
"ref": "refs/heads/master",
|
|
"ref_protected": "false",
|
|
"ref_type": "branch",
|
|
"repository": "mpminardi/testing",
|
|
"repository_id": "456",
|
|
"repository_owner": "mpminardi",
|
|
"repository_owner_id": "789",
|
|
"run_attempt": "1",
|
|
"run_id": "1",
|
|
"run_number": "1",
|
|
"sha": "pretend-sha",
|
|
"workflow": "test.yml",
|
|
"workflow_ref": "pretend-ref",
|
|
}
|
|
token, err := CreateAuthorizationToken(task, gitCtx, true, &repo_model.ActionsConfig{OIDCSubjectFormat: repo_model.OIDCSubjectFormatLegacyForgejo15})
|
|
require.NoError(t, err)
|
|
assert.NotEmpty(t, token)
|
|
headers := http.Header{}
|
|
headers.Set("Authorization", "Bearer "+token)
|
|
tokenClaims, err := ParseAuthorizationTokenClaims(&http.Request{
|
|
Header: headers,
|
|
})
|
|
require.NoError(t, err)
|
|
assert.Equal(t, task.ID, tokenClaims.TaskID)
|
|
assert.Equal(t, task.Job.ID, tokenClaims.JobID)
|
|
assert.Equal(t, task.Job.RunID, tokenClaims.RunID)
|
|
var customClaims map[string]any
|
|
err = json.Unmarshal([]byte(tokenClaims.OIDCExtra), &customClaims)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, gitCtx, customClaims)
|
|
assert.Equal(t, "repo:mpminardi/testing:ref:refs/heads/master", tokenClaims.OIDCSub)
|
|
}
|
|
|
|
func TestParseAuthorizationTokenNoAuthHeader(t *testing.T) {
|
|
headers := http.Header{}
|
|
rTaskID, err := ParseAuthorizationToken(&http.Request{
|
|
Header: headers,
|
|
})
|
|
require.NoError(t, err)
|
|
assert.Equal(t, int64(0), rTaskID)
|
|
}
|
|
|
|
func TestGenerateOIDCSub(t *testing.T) {
|
|
t.Run("pull_request event", func(t *testing.T) {
|
|
sub := generateOIDCSub(map[string]any{
|
|
"event_name": "pull_request",
|
|
"repository": "mpminardi/testing",
|
|
"ref": "refs/heads/master",
|
|
"repository_owner": "mpminardi",
|
|
"repository_owner_id": "123",
|
|
"repository_id": "456",
|
|
})
|
|
assert.Equal(t, "repo:mpminardi-123/testing-456:pull_request", sub)
|
|
})
|
|
|
|
t.Run("other event", func(t *testing.T) {
|
|
sub := generateOIDCSub(map[string]any{
|
|
"event_name": "random",
|
|
"repository": "mpminardi/testing",
|
|
"ref": "refs/heads/master",
|
|
"repository_owner": "mpminardi",
|
|
"repository_owner_id": "123",
|
|
"repository_id": "456",
|
|
})
|
|
assert.Equal(t, "repo:mpminardi-123/testing-456:ref:refs/heads/master", sub)
|
|
})
|
|
}
|
|
|
|
func TestLegacyGenerateOIDCSub(t *testing.T) {
|
|
t.Run("pull_request event", func(t *testing.T) {
|
|
sub := legacyGenerateOIDCSub(map[string]any{
|
|
"event_name": "pull_request",
|
|
"repository": "mpminardi/testing",
|
|
"ref": "refs/heads/master",
|
|
})
|
|
|
|
assert.Equal(t, "repo:mpminardi/testing:pull_request", sub)
|
|
})
|
|
|
|
t.Run("other event", func(t *testing.T) {
|
|
sub := legacyGenerateOIDCSub(map[string]any{
|
|
"event_name": "random",
|
|
"repository": "mpminardi/testing",
|
|
"ref": "refs/heads/master",
|
|
})
|
|
|
|
assert.Equal(t, "repo:mpminardi/testing:ref:refs/heads/master", sub)
|
|
})
|
|
}
|