-
-
Notifications
You must be signed in to change notification settings - Fork 6.7k
feat: Add configurable permissions for Actions automatic tokens #36173
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
332 commits
Select commit
Hold shift + click to select a range
f5d4ce6
compile errors
Excellencedev 012c9e0
...
Excellencedev b37a967
logs
Excellencedev 15c19f6
...
Excellencedev 350bcab
fix bug
Excellencedev d262948
Merge branch 'main' into fix-24635
Excellencedev 9610d7f
Implement all requested changes
Excellencedev b4a17df
lints
Excellencedev 3fb03fb
lint
Excellencedev dcf43af
fix ui issues
Excellencedev ba3a3a7
Feedback
Excellencedev 655733e
fix
Excellencedev 28dff2a
update parser
Excellencedev 43931dc
refactor the parser
Excellencedev a5debd9
add releases and projects units
Excellencedev f8a0b25
resolve todo
Excellencedev 1dfc172
fixes
Excellencedev 1840f0a
formatting issues
Excellencedev 7364953
...
Excellencedev 4e524d3
improve comments
Excellencedev 79c38fe
Merge branch 'main' into fix-24635
Excellencedev f7fc879
Merge branch 'main' into fix-24635
Excellencedev feb791c
feedback fixes
Excellencedev 7cccb84
fixes
Excellencedev 6d14a69
ui fixes
Excellencedev 87d9f86
Merge branch 'main' into fix-24635
Excellencedev 01a328b
fixes
Excellencedev 5067f1d
fixes
Excellencedev 27a4055
fix
Excellencedev 2317080
logs
Excellencedev 130b94b
..
Excellencedev 4ed81fe
fix
Excellencedev a8c2d13
refactor ui
Excellencedev 068ef6d
lints
Excellencedev 1934c55
Merge branch 'main' into fix-24635
Excellencedev 9352cc2
update test
Excellencedev 493d51d
fix test
Excellencedev b6bf0a1
Merge branch 'fix-24635' of https://github.com/Excellencedev/gitea in…
Excellencedev c770087
fix formatting
Excellencedev 2480c30
CrossRepoMode
Excellencedev f5c5775
CrossRepoMode tests
Excellencedev 77871ef
fixes
Excellencedev c9ea591
format
Excellencedev 4410d73
use ToString
Excellencedev ef139fa
Merge branch 'main' into fix-24635
Excellencedev 57d8b82
readd migration
Excellencedev 0d63aa8
...
Excellencedev 33ef56e
lints
Excellencedev f7a4406
copyright
Excellencedev a7c2c96
fix saving issue
Excellencedev 574d317
Update models/migrations/migrations.go
ChristopherHX 883d652
patch-1
Excellencedev 493f133
patch-2
Excellencedev 1de6d43
Merge branch 'fix-24635' of https://github.com/Excellencedev/gitea in…
Excellencedev 440cfa1
fmt
Excellencedev e0c5381
Update models/migrations/v1_26/V326.go
Excellencedev cee21ad
Update tests/integration/actions_persistence_test.go
Excellencedev 916806c
Update routers/web/org/setting/actions.go
Excellencedev 2107fd6
Update routers/api/packages/api.go
Excellencedev f5c4c33
Update routers/api/v1/repo/file.go
Excellencedev 005de17
Address Copilot review
Excellencedev 7636c6c
remove actionsperm
Excellencedev 2096877
fmt
Excellencedev f08356b
Merge branch 'main' into fix-24635
Excellencedev a2b88d6
ActionsTaskID
Excellencedev 75023da
missing imports
Excellencedev ea981c2
fmt
Excellencedev 6a7c528
Merge branch 'main' into fix-24635
Excellencedev a59b428
f8xes from feedback
Excellencedev d3fa0ec
update refernces
Excellencedev e2c14de
Merge branch 'main' into fix-24635
Excellencedev 0912b12
fix lint
Excellencedev b95361c
extra blank lines
Excellencedev a2fc97a
Fix comment
ChristopherHX 5c3493f
logic fixes
ChristopherHX 9425be4
cross repo package access respect permission none and require package…
ChristopherHX 0a3049a
revert, packages_model.Type(ctx.PathParam("type")) does not work
ChristopherHX fd0aed3
Refactor repo_permissions to repect none permission
ChristopherHX 137efea
Store Readonly in database for PR from forks
ChristopherHX b04f759
refactor functions Effective => Default / no fork param
ChristopherHX c2045d2
Allow public read for public repos
ChristopherHX fc57605
clamp pr from fork
ChristopherHX d8ce6d9
Refactor Simplify broken container permission checks
ChristopherHX fc1620c
update tests
ChristopherHX 455dfbb
Fix lint
ChristopherHX a6b5417
spelling
ChristopherHX e91e7ee
Merge branch 'main' into fix-24635
Excellencedev aba7f66
Actions package permissions
ChristopherHX 42e9a6d
fix grantMode for private org
ChristopherHX 50405cb
fix typo
ChristopherHX 8736615
fix pkg type pub
ChristopherHX eed2ad9
fix code
ChristopherHX 1806b29
fix lint
ChristopherHX 9c01831
remove test stub
ChristopherHX 56486b8
Revert "remove test stub"
ChristopherHX ae81b00
Revert Broken Package Access + single test
ChristopherHX 9ce2472
add missing return
ChristopherHX 6b4ed50
Merge branch 'main' into fix-24635
silverwind b4c61a4
Merge branch 'main' into fix-24635
Excellencedev 08063e7
Apply suggestion from @silverwind
silverwind 3e3e081
Update routers/private/hook_pre_receive.go
Excellencedev c9999c4
Address feedbac
Excellencedev ca6b32f
next
Excellencedev 9e7db02
..
Excellencedev 04299c9
fmt
Excellencedev 7755c83
Refactor Cross-Repo Permission Logic
Excellencedev d7a8957
revert pacage.go
Excellencedev a8b9575
update the permission parser tests to include the projects scope
Excellencedev c636e81
bug
Excellencedev f9a64b6
simplify code
Zettat123 2d9794b
Merge pull request #2 from Zettat123/improve-token-perms-check
Excellencedev c684dd9
Merge branch 'main' into fix-24635
silverwind 42fcef9
remove .CsrfTokenHtml
silverwind e17a531
address silverwind review
Excellencedev ddbc47a
Revert "remove .CsrfTokenHtml"
Excellencedev e6d5e82
Revert "address silverwind review"
Excellencedev 18f8247
Reapply "remove .CsrfTokenHtml"
Excellencedev c31ebf6
...
Excellencedev 89a6783
...
Excellencedev b7a92b8
...
Excellencedev d1126e9
fix tests
Excellencedev b1d1c64
Apply suggestion from @silverwind
silverwind 214424c
Apply suggestion from @silverwind
silverwind ae62bad
Apply suggestion from @silverwind
silverwind 3411280
Apply suggestion from @silverwind
silverwind ea4a284
Apply suggestion from @silverwind
silverwind 8ea2cbd
Apply suggestion from @silverwind
silverwind 381bc93
silverwind review
Excellencedev 018339e
Merge branch 'fix-24635' of https://github.com/Excellencedev/gitea in…
Excellencedev ebed469
Fix build error: uncomment ActionsTokenPermissionModeCustom constant
silverwind 618e21e
unt
Excellencedev b66c331
remove
Excellencedev 8caf9e4
unused
Excellencedev 6333714
Merge branch 'main' into fix-24635
Excellencedev 8770d0b
newline
Excellencedev ed525fa
Merge branch 'main' into fix-24635
silverwind f0fc5a2
cleanup
Excellencedev ed80060
fmt
Excellencedev 481b9ca
test
Excellencedev 61c8bda
cleanup
Excellencedev b097095
cleanup
Excellencedev 20ad9de
FEEDBACK
Excellencedev cb93b4b
lnt
Excellencedev f386234
Merge branch 'main' into fix-24635
silverwind 2efe144
Fix form alignment in actions general settings shared template
silverwind 0498df7
Fix review findings for actions token permissions
silverwind 6110201
Extract shared permissions table template to reduce duplication
silverwind 0bbafb6
Validate stored ActionsTokenPermissionMode against known values
silverwind 2eb2044
Fix default cross-repo access mode to allow access by default
silverwind 827c278
Merge branch 'main' into fix-24635
Excellencedev 0dac17a
Merge branch 'main' into fix-24635
Excellencedev 41c1d97
Latest feedback
Excellencedev c933f1c
com errors
Excellencedev 9cc110f
fx
Excellencedev e9c5382
fx
Excellencedev b325f3e
Merge branch 'main' into fix-24635
Excellencedev 0880942
add docs
Excellencedev 5b7d7a5
doc changes
Excellencedev b1acdc2
last changes
Excellencedev 9d2f890
feedback
Excellencedev 195ee7d
err
Excellencedev 9b95e0a
test
Excellencedev 0427102
changes
Excellencedev 81d4263
Merge branch 'main' into fix-24635
Excellencedev 2084180
feedbac
Excellencedev b7f6bba
evrythng
Excellencedev 17cc432
refactor
wxiaoguang ca2b627
clean up
wxiaoguang 3eef9ed
clean up
Excellencedev 85c4bef
remove fallback
Excellencedev 400ab94
update document for packages permission
wxiaoguang 393f9c4
refactor
wxiaoguang 6a9dcb3
revert
wxiaoguang d2245c1
fix lint
wxiaoguang 4cc1c1c
change
Excellencedev 85d8431
change
Excellencedev 952510a
Merge branch 'main' into fix-24635
Excellencedev d755ad4
fixes
Zettat123 1633d7b
fix test
Zettat123 1e439da
fix db query
Zettat123 7da98cc
check target repo config
Zettat123 290b2ce
new test
Excellencedev 00b9f0f
fmt
Excellencedev bd7e748
fx test
Excellencedev 1974c31
change
Excellencedev d7cf2b9
.
Excellencedev dd25d16
.
Excellencedev d22a375
Merge branch 'main' into fix-24635
silverwind 0641565
Fix actions token permission bugs found during review
silverwind b9012fd
do not use GetXxx
wxiaoguang dd4f6f4
split actions unit config and rename vars
wxiaoguang f200c1d
merge git push env
wxiaoguang e32e37f
merge git push env
wxiaoguang 5c6ae7e
remove ActionsCrossRepoMode=all (confusing and will be abused), clean up
wxiaoguang c6bb15c
dead translation
wxiaoguang 109dc10
fix text
wxiaoguang c39b98f
Merge branch 'main' into fix-24635
Excellencedev 01a6ce8
conflct
Excellencedev 2f2bd8d
delete
Excellencedev 1dce09b
conflct
Excellencedev d29f20c
fmt
Excellencedev 2e4a779
26
Excellencedev ed82dff
address feedback
Excellencedev 753d8c8
.
Excellencedev fda2362
changes
Excellencedev 4bdecd2
fx tests
Excellencedev 57e1288
Handle Nil Doer in Pushing Environment
Excellencedev fd8db9f
Fixing Test Failure
Excellencedev 952fb03
fmt
Excellencedev 824ed3c
Merge branch 'main' into fix-24635
wxiaoguang 65ccecf
comment
Excellencedev 0ea45c2
Merge branch 'fix-24635' of https://github.com/Excellencedev/gitea in…
Excellencedev 8a54bcd
Update modules/repository/env.go
wxiaoguang b362a2a
fix git http push env
wxiaoguang dda0dff
remove duplicate EnvRepoID, it has already been set in DoerPushingEnv…
wxiaoguang 5b38323
Update models/perm/access/repo_permission.go
Excellencedev 3278b5e
Update models/perm/access/repo_permission.go
Excellencedev 2a6f60d
fix test, that asserted now unexpected behavior
ChristopherHX 0716eeb
WIP
Excellencedev 183692c
Merge branch 'fix-24635' of https://github.com/Excellencedev/gitea in…
Excellencedev 5a7bfdb
Merge branch 'main' into feature-actions-permission
wxiaoguang b7a91a7
refactor
wxiaoguang 278bf61
fix layout bugs
wxiaoguang 1e6fa1b
fix test
wxiaoguang 1e1e1bf
fix test
wxiaoguang cb2a88a
fix test
wxiaoguang c989cd0
clean up
wxiaoguang 172b18f
reformat doc
wxiaoguang 8a268e9
fix migration
wxiaoguang aebde5e
fix unrelated repos bug
Excellencedev 35a91b9
fine tune help text
wxiaoguang 5b40171
fix layout
wxiaoguang 598530b
fine tune help text
wxiaoguang 4bb177f
fix tr key
wxiaoguang 459d6de
show correct permission table inherited from owner
wxiaoguang 0975485
do not render Actions Token Permissions if Actions is disabled
wxiaoguang b10db5d
change
Excellencedev 46f9ad1
Merge branch 'fix-24635' of https://github.com/Excellencedev/gitea in…
Excellencedev eacdd04
remove useless attribute
Excellencedev bb64b07
Migration fix
Excellencedev e1d11ae
fix form
wxiaoguang ade5a6c
use explicit var name
wxiaoguang 09454d7
Merge branch 'main' into feature-actions-permission
wxiaoguang f84981d
fix merge
wxiaoguang eb4ead7
fix tests
wxiaoguang 9950752
fix tests
wxiaoguang c556da0
fix tests
wxiaoguang 4ac9a9c
fix tests
wxiaoguang 319fb31
Merge branch 'main' into fix-24635
lunny 6aeda11
Merge branch 'main' into fix-24635
GiteaBot 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,74 @@ | ||
| // Copyright 2026 The Gitea Authors. All rights reserved. | ||
| // SPDX-License-Identifier: MIT | ||
|
|
||
| package actions | ||
|
|
||
| import ( | ||
| "context" | ||
|
|
||
| "code.gitea.io/gitea/models/perm" | ||
| repo_model "code.gitea.io/gitea/models/repo" | ||
| user_model "code.gitea.io/gitea/models/user" | ||
| "code.gitea.io/gitea/modules/json" | ||
| "code.gitea.io/gitea/modules/util" | ||
|
|
||
| "xorm.io/xorm/convert" | ||
| ) | ||
|
|
||
| // OwnerActionsConfig defines the Actions configuration for a user or organization | ||
| type OwnerActionsConfig struct { | ||
| // TokenPermissionMode defines the default permission mode (permissive, restricted) | ||
| TokenPermissionMode repo_model.ActionsTokenPermissionMode `json:"token_permission_mode,omitempty"` | ||
|
|
||
| // MaxTokenPermissions defines the absolute maximum permissions any token can have in this context. | ||
| MaxTokenPermissions *repo_model.ActionsTokenPermissions `json:"max_token_permissions,omitempty"` | ||
|
|
||
| // AllowedCrossRepoIDs is a list of specific repo IDs that can be accessed cross-repo | ||
| AllowedCrossRepoIDs []int64 `json:"allowed_cross_repo_ids,omitempty"` | ||
| } | ||
|
|
||
| var _ convert.ConversionFrom = (*OwnerActionsConfig)(nil) | ||
|
|
||
| func (cfg *OwnerActionsConfig) FromDB(bytes []byte) error { | ||
| _ = json.Unmarshal(bytes, cfg) | ||
| cfg.TokenPermissionMode, _ = util.EnumValue(cfg.TokenPermissionMode) | ||
| return nil | ||
| } | ||
|
|
||
| // GetOwnerActionsConfig loads the OwnerActionsConfig for a user or organization from user settings | ||
| // It returns a default config if no setting is found | ||
| func GetOwnerActionsConfig(ctx context.Context, userID int64) (ret OwnerActionsConfig, err error) { | ||
| return user_model.GetUserSettingJSON(ctx, userID, user_model.SettingsKeyActionsConfig, ret) | ||
| } | ||
|
|
||
| // SetOwnerActionsConfig saves the OwnerActionsConfig for a user or organization to user settings | ||
| func SetOwnerActionsConfig(ctx context.Context, userID int64, cfg OwnerActionsConfig) error { | ||
| return user_model.SetUserSettingJSON(ctx, userID, user_model.SettingsKeyActionsConfig, cfg) | ||
| } | ||
|
|
||
| // GetDefaultTokenPermissions returns the default token permissions by its TokenPermissionMode. | ||
| func (cfg *OwnerActionsConfig) GetDefaultTokenPermissions() repo_model.ActionsTokenPermissions { | ||
| switch cfg.TokenPermissionMode { | ||
| case repo_model.ActionsTokenPermissionModeRestricted: | ||
| return repo_model.MakeRestrictedPermissions() | ||
| case repo_model.ActionsTokenPermissionModePermissive: | ||
| return repo_model.MakeActionsTokenPermissions(perm.AccessModeWrite) | ||
| default: | ||
| return repo_model.MakeActionsTokenPermissions(perm.AccessModeNone) | ||
| } | ||
| } | ||
|
|
||
| // GetMaxTokenPermissions returns the maximum allowed permissions | ||
| func (cfg *OwnerActionsConfig) GetMaxTokenPermissions() repo_model.ActionsTokenPermissions { | ||
| if cfg.MaxTokenPermissions != nil { | ||
| return *cfg.MaxTokenPermissions | ||
| } | ||
| // Default max is write for everything | ||
| return repo_model.MakeActionsTokenPermissions(perm.AccessModeWrite) | ||
| } | ||
|
|
||
| // ClampPermissions ensures that the given permissions don't exceed the maximum | ||
| func (cfg *OwnerActionsConfig) ClampPermissions(perms repo_model.ActionsTokenPermissions) repo_model.ActionsTokenPermissions { | ||
| maxPerms := cfg.GetMaxTokenPermissions() | ||
| return repo_model.ClampActionsTokenPermissions(perms, maxPerms) | ||
| } |
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,60 @@ | ||
| // Copyright 2026 The Gitea Authors. All rights reserved. | ||
| // SPDX-License-Identifier: MIT | ||
|
|
||
| package actions | ||
|
|
||
| import ( | ||
| "context" | ||
|
|
||
| repo_model "code.gitea.io/gitea/models/repo" | ||
| "code.gitea.io/gitea/models/unit" | ||
| ) | ||
|
|
||
| // ComputeTaskTokenPermissions computes the effective permissions for a job token against the target repository. | ||
| // It uses the job's stored permissions (if any), then applies org/repo clamps and fork/cross-repo restrictions. | ||
| // Note: target repository access policy checks are enforced in GetActionsUserRepoPermission; this function only computes the job token's effective permission ceiling. | ||
| func ComputeTaskTokenPermissions(ctx context.Context, task *ActionTask, targetRepo *repo_model.Repository) (ret repo_model.ActionsTokenPermissions, err error) { | ||
| if err := task.LoadJob(ctx); err != nil { | ||
| return ret, err | ||
| } | ||
| if err := task.Job.LoadRepo(ctx); err != nil { | ||
| return ret, err | ||
| } | ||
| runRepo := task.Job.Repo | ||
|
|
||
| if err := runRepo.LoadOwner(ctx); err != nil { | ||
| return ret, err | ||
| } | ||
|
|
||
| repoActionsCfg := runRepo.MustGetUnit(ctx, unit.TypeActions).ActionsConfig() | ||
| ownerActionsCfg, err := GetOwnerActionsConfig(ctx, runRepo.OwnerID) | ||
| if err != nil { | ||
| return ret, err | ||
| } | ||
|
|
||
| var jobDeclaredPerms repo_model.ActionsTokenPermissions | ||
| if task.Job.TokenPermissions != nil { | ||
| jobDeclaredPerms = *task.Job.TokenPermissions | ||
| } else if repoActionsCfg.OverrideOwnerConfig { | ||
| jobDeclaredPerms = repoActionsCfg.GetDefaultTokenPermissions() | ||
| } else { | ||
| jobDeclaredPerms = ownerActionsCfg.GetDefaultTokenPermissions() | ||
| } | ||
|
|
||
| var effectivePerms repo_model.ActionsTokenPermissions | ||
| if repoActionsCfg.OverrideOwnerConfig { | ||
| effectivePerms = repoActionsCfg.ClampPermissions(jobDeclaredPerms) | ||
| } else { | ||
| effectivePerms = ownerActionsCfg.ClampPermissions(jobDeclaredPerms) | ||
| } | ||
|
|
||
| // Cross-repository access and fork pull requests are strictly read-only for security. | ||
| // This ensures a "task repo" cannot gain write access to other repositories via CrossRepoAccess settings. | ||
| isSameRepo := task.Job.RepoID == targetRepo.ID | ||
| restrictCrossRepoAccess := task.IsForkPullRequest || !isSameRepo | ||
| if restrictCrossRepoAccess { | ||
| effectivePerms = repo_model.ClampActionsTokenPermissions(effectivePerms, repo_model.MakeRestrictedPermissions()) | ||
| } | ||
|
|
||
| return effectivePerms, nil | ||
| } |
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,16 @@ | ||
| // Copyright 2026 The Gitea Authors. All rights reserved. | ||
| // SPDX-License-Identifier: MIT | ||
|
|
||
| package v1_26 | ||
|
|
||
| import ( | ||
| "xorm.io/xorm" | ||
| ) | ||
|
|
||
| func AddTokenPermissionsToActionRunJob(x *xorm.Engine) error { | ||
| type ActionRunJob struct { | ||
| TokenPermissions string `xorm:"JSON TEXT"` | ||
| } | ||
| _, err := x.SyncWithOptions(xorm.SyncOptions{IgnoreDropIndices: true}, new(ActionRunJob)) | ||
| return err | ||
| } |
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,155 @@ | ||
| // Copyright 2026 The Gitea Authors. All rights reserved. | ||
| // SPDX-License-Identifier: MIT | ||
|
|
||
| package access | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| actions_model "code.gitea.io/gitea/models/actions" | ||
| "code.gitea.io/gitea/models/db" | ||
| perm_model "code.gitea.io/gitea/models/perm" | ||
| repo_model "code.gitea.io/gitea/models/repo" | ||
| "code.gitea.io/gitea/models/unit" | ||
| "code.gitea.io/gitea/models/unittest" | ||
| user_model "code.gitea.io/gitea/models/user" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func TestGetActionsUserRepoPermission(t *testing.T) { | ||
| require.NoError(t, unittest.PrepareTestDatabase()) | ||
| ctx := t.Context() | ||
|
|
||
| // Use fixtures for repos and users | ||
| repo4 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 4}) // Public, Owner 5, has Actions unit | ||
| repo2 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 2}) // Private, Owner 2, no Actions unit in fixtures | ||
| repo15 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 15}) // Private, Owner 2, no Actions unit in fixtures | ||
| owner2 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2}) | ||
| actionsUser := user_model.NewActionsUser() | ||
|
|
||
| // Ensure repo2 and repo15 have Actions units for testing configuration | ||
| for _, r := range []*repo_model.Repository{repo2, repo15} { | ||
| require.NoError(t, db.Insert(ctx, &repo_model.RepoUnit{ | ||
| RepoID: r.ID, | ||
| Type: unit.TypeActions, | ||
| Config: &repo_model.ActionsConfig{}, | ||
| })) | ||
| } | ||
|
|
||
| t.Run("SameRepo_Public", func(t *testing.T) { | ||
| task47 := unittest.AssertExistsAndLoadBean(t, &actions_model.ActionTask{ID: 47}) | ||
| require.Equal(t, repo4.ID, task47.RepoID) | ||
|
|
||
| perm, err := GetActionsUserRepoPermission(ctx, repo4, actionsUser, task47.ID) | ||
| require.NoError(t, err) | ||
|
|
||
| // Public repo, bot should have Read access even if not collaborator | ||
| assert.Equal(t, perm_model.AccessModeNone, perm.AccessMode) | ||
| assert.True(t, perm.CanRead(unit.TypeCode)) | ||
| }) | ||
|
|
||
| t.Run("SameRepo_Private", func(t *testing.T) { | ||
| // Use Task 53 which is already in Repo 2 (Private) | ||
| task53 := unittest.AssertExistsAndLoadBean(t, &actions_model.ActionTask{ID: 53}) | ||
| require.Equal(t, repo2.ID, task53.RepoID) | ||
|
|
||
| perm, err := GetActionsUserRepoPermission(ctx, repo2, actionsUser, task53.ID) | ||
| require.NoError(t, err) | ||
|
|
||
| // Private repo, bot has no base access, but gets Write from effective tokens perms (Permissive by default) | ||
| assert.Equal(t, perm_model.AccessModeNone, perm.AccessMode) | ||
| assert.True(t, perm.CanWrite(unit.TypeCode)) | ||
| }) | ||
|
|
||
| t.Run("CrossRepo_Denied_None", func(t *testing.T) { | ||
| task53 := unittest.AssertExistsAndLoadBean(t, &actions_model.ActionTask{ID: 53}) | ||
|
|
||
| // Set owner policy to nil allowed repos (None) | ||
| cfg := actions_model.OwnerActionsConfig{} | ||
| require.NoError(t, actions_model.SetOwnerActionsConfig(ctx, owner2.ID, cfg)) | ||
|
|
||
| perm, err := GetActionsUserRepoPermission(ctx, repo15, actionsUser, task53.ID) | ||
| require.NoError(t, err) | ||
|
|
||
| // Should NOT have access to the private repo. | ||
| assert.False(t, perm.CanRead(unit.TypeCode)) | ||
| }) | ||
|
|
||
| t.Run("ForkPR_NoCrossRepo", func(t *testing.T) { | ||
| task53 := unittest.AssertExistsAndLoadBean(t, &actions_model.ActionTask{ID: 53}) | ||
| task53.IsForkPullRequest = true | ||
| require.NoError(t, actions_model.UpdateTask(ctx, task53, "is_fork_pull_request")) | ||
|
|
||
| // Policy contains repo15 | ||
| cfg := actions_model.OwnerActionsConfig{ | ||
| AllowedCrossRepoIDs: []int64{repo15.ID}, | ||
| } | ||
| require.NoError(t, actions_model.SetOwnerActionsConfig(ctx, owner2.ID, cfg)) | ||
|
|
||
| perm, err := GetActionsUserRepoPermission(ctx, repo15, actionsUser, task53.ID) | ||
| require.NoError(t, err) | ||
|
|
||
| // Fork PR never gets cross-repo access to other private repos | ||
| assert.False(t, perm.CanRead(unit.TypeCode)) | ||
| }) | ||
|
|
||
| t.Run("Inheritance_And_Clamping", func(t *testing.T) { | ||
| task53 := unittest.AssertExistsAndLoadBean(t, &actions_model.ActionTask{ID: 53}) | ||
| task53.IsForkPullRequest = false | ||
| require.NoError(t, actions_model.UpdateTask(ctx, task53, "is_fork_pull_request")) | ||
|
|
||
| // Owner policy: Restricted mode (Read-only Code) | ||
| ownerCfg := actions_model.OwnerActionsConfig{ | ||
| TokenPermissionMode: repo_model.ActionsTokenPermissionModeRestricted, | ||
| MaxTokenPermissions: &repo_model.ActionsTokenPermissions{ | ||
| UnitAccessModes: map[unit.Type]perm_model.AccessMode{ | ||
| unit.TypeCode: perm_model.AccessModeRead, | ||
| }, | ||
| }, | ||
| } | ||
| require.NoError(t, actions_model.SetOwnerActionsConfig(ctx, owner2.ID, ownerCfg)) | ||
|
|
||
| // Repo policy: OverrideOwnerConfig = false (should inherit owner's restricted mode) | ||
| repo2ActionsUnit := repo2.MustGetUnit(ctx, unit.TypeActions) | ||
| repo2ActionsCfg := repo2ActionsUnit.ActionsConfig() | ||
| repo2ActionsCfg.OverrideOwnerConfig = false | ||
| require.NoError(t, repo_model.UpdateRepoUnitConfig(ctx, repo2ActionsUnit)) | ||
|
|
||
| perm, err := GetActionsUserRepoPermission(ctx, repo2, actionsUser, task53.ID) | ||
| require.NoError(t, err) | ||
|
|
||
| // Should be clamped to Read-only | ||
| assert.Equal(t, perm_model.AccessModeRead, perm.UnitAccessMode(unit.TypeCode)) | ||
| assert.False(t, perm.CanWrite(unit.TypeCode)) | ||
| }) | ||
|
|
||
| t.Run("RepoOverride_Clamping", func(t *testing.T) { | ||
| task53 := unittest.AssertExistsAndLoadBean(t, &actions_model.ActionTask{ID: 53}) | ||
|
|
||
| // Owner policy: Permissive (Write access) | ||
| ownerCfg := actions_model.OwnerActionsConfig{ | ||
| TokenPermissionMode: repo_model.ActionsTokenPermissionModePermissive, | ||
| } | ||
| require.NoError(t, actions_model.SetOwnerActionsConfig(ctx, owner2.ID, ownerCfg)) | ||
|
|
||
| // Repo policy: OverrideOwnerConfig = true, MaxTokenPermissions = Read | ||
| repo2ActionsUnit := repo2.MustGetUnit(ctx, unit.TypeActions) | ||
| repo2ActionsCfg := repo2ActionsUnit.ActionsConfig() | ||
| repo2ActionsCfg.OverrideOwnerConfig = true | ||
| repo2ActionsCfg.TokenPermissionMode = repo_model.ActionsTokenPermissionModeRestricted | ||
| repo2ActionsCfg.MaxTokenPermissions = &repo_model.ActionsTokenPermissions{ | ||
| UnitAccessModes: map[unit.Type]perm_model.AccessMode{ | ||
| unit.TypeCode: perm_model.AccessModeRead, | ||
| }, | ||
| } | ||
| require.NoError(t, repo_model.UpdateRepoUnitConfig(ctx, repo2ActionsUnit)) | ||
|
|
||
| perm, err := GetActionsUserRepoPermission(ctx, repo2, actionsUser, task53.ID) | ||
| require.NoError(t, err) | ||
|
|
||
| // Should be clamped to Read-only | ||
| assert.Equal(t, perm_model.AccessModeRead, perm.UnitAccessMode(unit.TypeCode)) | ||
| }) | ||
| } |
Oops, something went wrong.
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.