This commit is contained in:
@@ -8,14 +8,16 @@ import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
actions_model "code.gitea.io/gitea/models/actions"
|
||||
"code.gitea.io/gitea/models/db"
|
||||
actions_module "code.gitea.io/gitea/modules/actions"
|
||||
"code.gitea.io/gitea/modules/log"
|
||||
secret_module "code.gitea.io/gitea/modules/secret"
|
||||
"code.gitea.io/gitea/modules/setting"
|
||||
"code.gitea.io/gitea/modules/timeutil"
|
||||
"code.gitea.io/gitea/modules/util"
|
||||
actions_model "gitea.dev/models/actions"
|
||||
"gitea.dev/models/db"
|
||||
actions_module "gitea.dev/modules/actions"
|
||||
"gitea.dev/modules/actions/jobparser"
|
||||
"gitea.dev/modules/json"
|
||||
"gitea.dev/modules/log"
|
||||
secret_module "gitea.dev/modules/secret"
|
||||
"gitea.dev/modules/setting"
|
||||
"gitea.dev/modules/timeutil"
|
||||
"gitea.dev/modules/util"
|
||||
|
||||
"xorm.io/builder"
|
||||
)
|
||||
@@ -152,16 +154,16 @@ func UpdateSecret(ctx context.Context, secretID int64, data, description string)
|
||||
}
|
||||
|
||||
func GetSecretsOfTask(ctx context.Context, task *actions_model.ActionTask) (map[string]string, error) {
|
||||
secrets := map[string]string{}
|
||||
baseSecrets := map[string]string{}
|
||||
|
||||
secrets["GITHUB_TOKEN"] = task.Token
|
||||
secrets["GITEA_TOKEN"] = task.Token
|
||||
baseSecrets["GITHUB_TOKEN"] = task.Token
|
||||
baseSecrets["GITEA_TOKEN"] = task.Token
|
||||
|
||||
if task.Job.Run.IsForkPullRequest && task.Job.Run.TriggerEvent != actions_module.GithubEventPullRequestTarget {
|
||||
// ignore secrets for fork pull request, except GITHUB_TOKEN and GITEA_TOKEN which are automatically generated.
|
||||
// for the tasks triggered by pull_request_target event, they could access the secrets because they will run in the context of the base branch
|
||||
// see the documentation: https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#pull_request_target
|
||||
return secrets, nil
|
||||
return baseSecrets, nil
|
||||
}
|
||||
|
||||
ownerSecrets, err := db.Find[Secret](ctx, FindSecretsOptions{OwnerID: task.Job.Run.Repo.OwnerID})
|
||||
@@ -181,10 +183,60 @@ func GetSecretsOfTask(ctx context.Context, task *actions_model.ActionTask) (map[
|
||||
log.Error("Unable to decrypt Actions secret %v %q, maybe SECRET_KEY is wrong: %v", secret.ID, secret.Name, err)
|
||||
continue
|
||||
}
|
||||
secrets[secret.Name] = v
|
||||
baseSecrets[secret.Name] = v
|
||||
}
|
||||
|
||||
return secrets, nil
|
||||
return getScopedSecretsForJob(ctx, task.Job, baseSecrets)
|
||||
}
|
||||
|
||||
// getScopedSecretsForJob walks up the caller chain (ParentJobID) and applies
|
||||
// each caller's secrets policy:
|
||||
// - "secrets: inherit" passes the parent scope's secrets through unchanged.
|
||||
// - explicit mapping {alias: SOURCE} only forwards the named secrets, plus the auto-generated tokens.
|
||||
//
|
||||
// For top-level jobs (ParentJobID == 0) the base secrets are returned as-is.
|
||||
func getScopedSecretsForJob(ctx context.Context, job *actions_model.ActionRunJob, baseSecrets map[string]string) (map[string]string, error) {
|
||||
if job.ParentJobID == 0 {
|
||||
return baseSecrets, nil
|
||||
}
|
||||
|
||||
caller, err := actions_model.GetRunJobByRunAndID(ctx, job.RunID, job.ParentJobID)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("load caller job %d: %w", job.ParentJobID, err)
|
||||
}
|
||||
|
||||
parentScope, err := getScopedSecretsForJob(ctx, caller, baseSecrets)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if caller.CallSecrets == jobparser.SecretsInherit {
|
||||
return parentScope, nil
|
||||
}
|
||||
|
||||
// Empty or explicit-mapping path: only auto-tokens + (any) mapped aliases are exposed.
|
||||
scoped := map[string]string{
|
||||
"GITHUB_TOKEN": baseSecrets["GITHUB_TOKEN"],
|
||||
"GITEA_TOKEN": baseSecrets["GITEA_TOKEN"],
|
||||
}
|
||||
if caller.CallSecrets == "" {
|
||||
return scoped, nil
|
||||
}
|
||||
var mapping map[string]string
|
||||
if err := json.Unmarshal([]byte(caller.CallSecrets), &mapping); err != nil {
|
||||
return nil, fmt.Errorf("decode caller %d secret map: %w", caller.ID, err)
|
||||
}
|
||||
for alias, source := range mapping {
|
||||
if v, ok := parentScope[source]; ok {
|
||||
scoped[alias] = v
|
||||
continue
|
||||
}
|
||||
// Secret names are case-insensitive in storage (uppercased).
|
||||
if v, ok := parentScope[strings.ToUpper(source)]; ok {
|
||||
scoped[alias] = v
|
||||
}
|
||||
}
|
||||
return scoped, nil
|
||||
}
|
||||
|
||||
func CountWrongRepoLevelSecrets(ctx context.Context) (int64, error) {
|
||||
|
||||
Reference in New Issue
Block a user