gitea/modules
Excellencedev 45809c8f54
feat: Add configurable permissions for Actions automatic tokens (#36173)
## Overview

This PR introduces granular permission controls for Gitea Actions tokens
(`GITEA_TOKEN`), aligning Gitea's security model with GitHub Actions
standards while maintaining compatibility with Gitea's unique repository
unit system.

It addresses the need for finer access control by allowing
administrators and repository owners to define default token
permissions, set maximum permission ceilings, and control
cross-repository access within organizations.

## Key Features

### 1. Granular Token Permissions

- **Standard Keyword Support**: Implements support for the
`permissions:` keyword in workflow and job YAML files (e.g., `contents:
read`, `issues: write`).
- **Permission Modes**:
- **Permissive**: Default write access for most units (backwards
compatible).
- **Restricted**: Default read-only access for `contents` and
`packages`, with no access to other units.
- ~~**Custom**: Allows defining specific default levels for each unit
type (Code, Issues, PRs, Packages, etc.).~~**EDIT removed UI was
confusing**
- **Clamping Logic**: Workflow-defined permissions are automatically
"clamped" by repository or organization-level maximum settings.
Workflows cannot escalate their own permissions beyond these limits.

### 2. Organization & Repository Settings

- **Settings UI**: Added new settings pages at both Organization and
Repository levels to manage Actions token defaults and maximums.
- **Inheritance**: Repositories can be configured to "Follow
organization-level configuration," simplifying management across large
organizations.
- **Cross-Repository Access**: Added a policy to control whether Actions
workflows can access other repositories or packages within the same
organization. This can be set to "None," "All," or restricted to a
"Selected" list of repositories.

### 3. Security Hardening

- **Fork Pull Request Protection**: Tokens for workflows triggered by
pull requests from forks are strictly enforced as read-only, regardless
of repository settings.
- ~~**Package Access**: Actions tokens can now only access packages
explicitly linked to a repository, with cross-repo access governed by
the organization's security policy.~~ **EDIT removed
https://github.com/go-gitea/gitea/pull/36173#issuecomment-3873675346**
- **Git Hook Integration**: Propagates Actions Task IDs to git hooks to
ensure that pushes performed by Actions tokens respect the specific
permissions granted at runtime.

### 4. Technical Implementation

- **Permission Persistence**: Parsed permissions are calculated at job
creation and stored in the `action_run_job` table. This ensures the
token's authority is deterministic throughout the job's lifecycle.
- **Parsing Priority**: Implemented a priority system in the YAML parser
where the broad `contents` scope is applied first, allowing granular
scopes like `code` or `releases` to override it for precise control.
- **Re-runs**: Permissions are re-evaluated during a job re-run to
incorporate any changes made to repository settings in the interim.

### How to Test

1. **Unit Tests**: Run `go test ./services/actions/...` and `go test
./models/repo/...` to verify parsing logic and permission clamping.
2. **Integration Tests**: Comprehensive tests have been added to
`tests/integration/actions_job_token_test.go` covering:
   - Permissive vs. Restricted mode behavior.
   - YAML `permissions:` keyword evaluation.
   - Organization cross-repo access policies.
- Resource access (Git, API, and Packages) under various permission
configs.
3. **Manual Verification**: 
   - Navigate to **Site/Org/Repo Settings -> Actions -> General**.
- Change "Default Token Permissions" and verify that newly triggered
workflows reflect these changes in their `GITEA_TOKEN` capabilities.
- Attempt a cross-repo API call from an Action and verify the Org policy
is enforced.

## Documentation

Added a PR in gitea's docs for this :
https://gitea.com/gitea/docs/pulls/318

## UI:

<img width="1366" height="619" alt="Screenshot 2026-01-24 174112"
src="https://github.com/user-attachments/assets/bfa29c9a-4ea5-4346-9410-16d491ef3d44"
/>

<img width="1360" height="621" alt="Screenshot 2026-01-24 174048"
src="https://github.com/user-attachments/assets/d5ec46c8-9a13-4874-a6a4-fb379936cef5"
/>

/fixes #24635
/claim #24635

---------

Signed-off-by: Excellencedev <ademiluyisuccessandexcellence@gmail.com>
Signed-off-by: ChristopherHX <christopher.homberger@web.de>
Signed-off-by: silverwind <me@silverwind.io>
Signed-off-by: wxiaoguang <wxiaoguang@gmail.com>
Co-authored-by: ChristopherHX <christopher.homberger@web.de>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: silverwind <me@silverwind.io>
Co-authored-by: Zettat123 <zettat123@gmail.com>
Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
2026-03-21 15:39:47 -07:00
..
actions Fix dbfs error handling (#36844) 2026-03-07 00:28:46 +08:00
analyze Fix incorrect vendored detections (#36508) 2026-02-01 10:35:51 +00:00
assetfs Refactor avatar package, support default avatar fallback (#36788) 2026-03-01 13:32:35 +00:00
auth Correct spelling (#36783) 2026-02-28 11:23:20 -08:00
avatar Refactor avatar package, support default avatar fallback (#36788) 2026-03-01 13:32:35 +00:00
badge Add flat-square action badge style (#34062) 2025-04-01 09:42:10 +00:00
base Fix incorrect setting loading order (#36735) 2026-02-24 23:46:08 +08:00
cache enforce explanation for necessary nolints and fix bugs (#34883) 2025-06-27 21:48:03 +08:00
cachegroup Cache GPG keys, emails and users when list commits (#34086) 2025-04-09 16:34:38 +00:00
charset Fix CodeQL code scanning alerts (#36858) 2026-03-08 14:35:50 +00:00
commitstatus enforce nolint scope (#34851) 2025-06-27 07:59:55 +02:00
container
csv Disable Field count validation of CSV viewer (#35228) 2025-09-04 09:54:58 -07:00
dump Use github.com/mholt/archives replace github.com/mholt/archiver (#35390) 2025-09-01 19:40:12 +00:00
emoji Update emoji data for Unicode 16 (#36596) 2026-02-12 21:39:36 +00:00
eventsource Add more check for stopwatch read or list (#36340) 2026-01-13 13:13:39 +00:00
fileicon Add FOLDER_ICON_THEME configuration option (#36496) 2026-01-30 20:48:56 +00:00
generate
git Add a git grep search timeout (#36809) 2026-03-04 12:11:42 -08:00
gitrepo Clean up Makefile, tests and legacy code (#36638) 2026-02-19 01:23:32 +00:00
glob Replace gobwas/glob package (#35478) 2025-09-13 18:01:00 +00:00
globallock Upgrade golang to 1.25.1 and add descriptions for the swagger structs' fields (#35418) 2025-09-06 16:52:41 +00:00
graceful Enable `nilnil` linter for new code (#36591) 2026-02-16 09:57:18 +00:00
gtprof Add start time on perf trace because it seems some steps haven't been recorded. (#35282) 2025-08-18 15:17:19 +00:00
hcaptcha Clean up Makefile, tests and legacy code (#36638) 2026-02-19 01:23:32 +00:00
highlight Fix chroma lexer mapping (#36629) 2026-02-16 02:11:02 +00:00
hostmatcher Run `gopls modernize` on codebase (#34751) 2025-06-18 01:48:09 +00:00
htmlutil Fix markup heading parsing, fix emphasis parsing (#36284) 2026-01-23 20:24:58 +00:00
httpcache Refactor avatar package, support default avatar fallback (#36788) 2026-03-01 13:32:35 +00:00
httplib Fix forwarded proto handling for public URL detection (#36810) 2026-03-06 00:31:52 +08:00
indexer Fix typos in code comments: doesnt, dont, wont (#36890) 2026-03-13 09:58:44 -07:00
issue/template Limit reading bytes instead of ReadAll (#35928) 2025-11-12 19:44:49 +08:00
json Drop json-iterator dependency (#35544) 2025-09-28 22:30:28 +08:00
label Run `gopls modernize` on codebase (#34751) 2025-06-18 01:48:09 +00:00
lfs Refactor git command stdio pipe (#36422) 2026-01-22 06:04:26 +00:00
lfstransfer Correct spelling (#36783) 2026-02-28 11:23:20 -08:00
log Refactor git command context & pipeline (#36406) 2026-01-21 01:35:14 +00:00
markup Fix various trivial problems (#36921) 2026-03-19 07:13:55 +08:00
mcaptcha
metrics Remove incorrect "db.DefaultContext" usages (#35366) 2025-08-28 03:52:43 +00:00
migration fix: prevent panic when GitLab release has more links than sources (#36295) 2026-01-05 21:48:12 +08:00
nosql Refactor git command context & pipeline (#36406) 2026-01-21 01:35:14 +00:00
optional Fix API not persisting pull request unit config when has_pull_requests is not set (#36718) 2026-03-02 22:08:53 +00:00
options Refactor embedded assets and drop unnecessary dependencies (#34692) 2025-06-12 03:59:33 +00:00
packages Fix CRAN package version validation to allow more than 4 version components (#36813) 2026-03-04 01:56:38 +08:00
paginator Only use prev and next buttons for pagination on user dashboard (#33981) 2025-03-23 19:52:43 +00:00
pprof
private feat: Add configurable permissions for Actions automatic tokens (#36173) 2026-03-21 15:39:47 -07:00
process Refactor git command context & pipeline (#36406) 2026-01-21 01:35:14 +00:00
proxy Replace gobwas/glob package (#35478) 2025-09-13 18:01:00 +00:00
proxyprotocol Enable addtional linters (#34085) 2025-04-01 10:14:01 +00:00
public Refactor template render (#36438) 2026-01-24 05:11:49 +00:00
queue Refactor git command context & pipeline (#36406) 2026-01-21 01:35:14 +00:00
recaptcha Fix URLJoin, markup render link reoslving, sign-in/up/linkaccount page common data (#36861) 2026-03-08 15:57:37 +00:00
references Support closing keywords with URL references (#36221) 2025-12-27 09:05:24 -08:00
regexplru Enable testifylint rules (#34075) 2025-03-31 01:53:48 -04:00
repository feat: Add configurable permissions for Actions automatic tokens (#36173) 2026-03-21 15:39:47 -07:00
reqctx Run `gopls modernize` on codebase (#34751) 2025-06-18 01:48:09 +00:00
secret Upgrade golangci-lint to v1.64.5 (#33654) 2025-02-21 00:05:40 +08:00
session Clean up Makefile, tests and legacy code (#36638) 2026-02-19 01:23:32 +00:00
setting Fix URLJoin, markup render link reoslving, sign-in/up/linkaccount page common data (#36861) 2026-03-08 15:57:37 +00:00
sitemap
ssh Update x/crypto package and make builtin SSH use default parameters (#34667) 2025-06-09 19:51:02 +00:00
storage Fix artifacts v4 backend upload problems (#36805) 2026-03-05 16:49:01 +01:00
structs Feature: Add per-runner “Disable/Pause” (#36776) 2026-03-16 10:24:36 -07:00
svg Add render cache for SVG icons (#36863) 2026-03-10 05:26:16 +00:00
system Remove incorrect "db.DefaultContext" usages (#35366) 2025-08-28 03:52:43 +00:00
tailmsg
tempdir Address some CodeQL security concerns (#35572) 2025-10-04 01:21:26 +08:00
templates Fix URLJoin, markup render link reoslving, sign-in/up/linkaccount page common data (#36861) 2026-03-08 15:57:37 +00:00
test Fix various version parsing problems (#36553) 2026-02-08 20:25:30 +00:00
testlogger Clean up Makefile, tests and legacy code (#36638) 2026-02-19 01:23:32 +00:00
timeutil Clean up Makefile, tests and legacy code (#36638) 2026-02-19 01:23:32 +00:00
translation Use flatten translation keys (#36225) 2025-12-25 12:51:33 -08:00
turnstile
typesniffer Fix incorrect text content detection (#36364) 2026-01-14 23:35:52 +08:00
updatechecker Enable addtional linters (#34085) 2025-04-01 10:14:01 +00:00
uri
user
util feat: Add configurable permissions for Actions automatic tokens (#36173) 2026-03-21 15:39:47 -07:00
validation Update tool dependencies (#36445) 2026-01-24 09:58:28 +01:00
web Refactor auth middleware (#36848) 2026-03-08 17:59:46 +08:00
webhook actions: report commit status for pull_request_review events (#36589) 2026-02-20 16:12:22 +00:00
zstd Refactor embedded assets and drop unnecessary dependencies (#34692) 2025-06-12 03:59:33 +00:00