Skip to content

Commit 24cf065

Browse files
GiteaBotwolfogre
andauthored
Restrict [actions].DEFAULT_ACTIONS_URL to only github or self (#25581) (#25604)
Backport #25581 by @wolfogre Resolve #24789 ## ⚠️ BREAKING ⚠️ Before this, `DEFAULT_ACTIONS_URL` cound be set to any custom URLs like `https://gitea.com` or `http://your-git-server,https://gitea.com`, and the default value was `https://gitea.com`. But now, `DEFAULT_ACTIONS_URL` supports only `github`(`https://github.com`) or `self`(the root url of current Gitea instance), and the default value is `github`. If it has configured with a URL, an error log will be displayed and it will fallback to `github`. Actually, what we really want to do is always make it `https://github.com`, however, this may not be acceptable for some instances of internal use, so there's extra support for `self`, but no more, even `https://gitea.com`. Please note that `uses: https://xxx/yyy/zzz` always works and it does exactly what it is supposed to do. Although it's breaking, I belive it should be backported to `v1.20` due to some security issues. Follow-up on the runner side: - https://gitea.com/gitea/act_runner/pulls/262 - https://gitea.com/gitea/act/pulls/70 Co-authored-by: Jason Song <i@wolfogre.com>
1 parent 0b6f7fb commit 24cf065

File tree

5 files changed

+139
-33
lines changed

5 files changed

+139
-33
lines changed

custom/conf/app.example.ini

+2-2
Original file line numberDiff line numberDiff line change
@@ -2541,8 +2541,8 @@ LEVEL = Info
25412541
;; Enable/Disable actions capabilities
25422542
;ENABLED = false
25432543
;;
2544-
;; Default address to get action plugins, e.g. the default value means downloading from "https://gitea.com/actions/checkout" for "uses: actions/checkout@v3"
2545-
;DEFAULT_ACTIONS_URL = https://gitea.com
2544+
;; Default platform to get action plugins, `github` for `https://github.com`, `self` for the current Gitea instance.
2545+
;DEFAULT_ACTIONS_URL = github
25462546

25472547
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
25482548
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

docs/content/doc/administration/config-cheat-sheet.en-us.md

+11-28
Original file line numberDiff line numberDiff line change
@@ -1374,39 +1374,22 @@ PROXY_HOSTS = *.github.com
13741374
## Actions (`actions`)
13751375

13761376
- `ENABLED`: **false**: Enable/Disable actions capabilities
1377-
- `DEFAULT_ACTIONS_URL`: **https://gitea.com**: Default address to get action plugins, e.g. the default value means downloading from "<https://gitea.com/actions/checkout>" for "uses: actions/checkout@v3"
1377+
- `DEFAULT_ACTIONS_URL`: **github**: Default platform to get action plugins, `github` for `https://github.com`, `self` for the current Gitea instance.
13781378
- `STORAGE_TYPE`: **local**: Storage type for actions logs, `local` for local disk or `minio` for s3 compatible object storage service, default is `local` or other name defined with `[storage.xxx]`
13791379
- `MINIO_BASE_PATH`: **actions_log/**: Minio base path on the bucket only available when STORAGE_TYPE is `minio`
13801380

1381-
`DEFAULT_ACTIONS_URL` indicates where should we find the relative path action plugin. i.e. when use an action in a workflow file like
1382-
1383-
```yaml
1384-
name: versions
1385-
on:
1386-
push:
1387-
branches:
1388-
- main
1389-
- releases/*
1390-
jobs:
1391-
build:
1392-
runs-on: ubuntu-latest
1393-
steps:
1394-
- uses: actions/checkout@v3
1395-
```
1396-
1397-
Now we need to know how to get actions/checkout, this configuration is the default git server to get it. That means we will get the repository via git clone ${DEFAULT_ACTIONS_URL}/actions/checkout and fetch tag v3.
1398-
1399-
To help people who don't want to mirror these actions in their git instances, the default value is https://gitea.com
1400-
To help people run actions totally in their network, they can change the value and copy all necessary action repositories into their git server.
1381+
`DEFAULT_ACTIONS_URL` indicates where the Gitea Actions runners should find the actions with relative path.
1382+
For example, `uses: actions/checkout@v3` means `https://github.com/actions/checkout@v3` since the value of `DEFAULT_ACTIONS_URL` is `github`.
1383+
And it can be changed to `self` to make it `root_url_of_your_gitea/actions/checkout@v3`.
14011384

1402-
Of course we should support the form in future PRs like
1403-
1404-
```yaml
1405-
steps:
1406-
- uses: gitea.com/actions/checkout@v3
1407-
```
1385+
Please note that using `self` is not recommended for most cases, as it could make names globally ambiguous.
1386+
Additionally, it requires you to mirror all the actions you need to your Gitea instance, which may not be worth it.
1387+
Therefore, please use `self` only if you understand what you are doing.
14081388

1409-
although Github don't support this form.
1389+
In earlier versions (<= 1.19), `DEFAULT_ACTIONS_URL` cound be set to any custom URLs like `https://gitea.com` or `http://your-git-server,https://gitea.com`, and the default value was `https://gitea.com`.
1390+
However, later updates removed those options, and now the only options are `github` and `self`, with the default value being `github`.
1391+
However, if you want to use actions from other git server, you can use a complete URL in `uses` field, it's supported by Gitea (but not GitHub).
1392+
Like `uses: https://gitea.com/actions/checkout@v3` or `uses: http://your-git-server/actions/checkout@v3`.
14101393

14111394
## Other (`other`)
14121395

modules/setting/actions.go

+41-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ package setting
55

66
import (
77
"fmt"
8+
"strings"
9+
10+
"code.gitea.io/gitea/modules/log"
811
)
912

1013
// Actions settings
@@ -13,20 +16,56 @@ var (
1316
LogStorage *Storage // how the created logs should be stored
1417
ArtifactStorage *Storage // how the created artifacts should be stored
1518
Enabled bool
16-
DefaultActionsURL string `ini:"DEFAULT_ACTIONS_URL"`
19+
DefaultActionsURL defaultActionsURL `ini:"DEFAULT_ACTIONS_URL"`
1720
}{
1821
Enabled: false,
19-
DefaultActionsURL: "https://gitea.com",
22+
DefaultActionsURL: defaultActionsURLGitHub,
2023
}
2124
)
2225

26+
type defaultActionsURL string
27+
28+
func (url defaultActionsURL) URL() string {
29+
switch url {
30+
case defaultActionsURLGitHub:
31+
return "https://github.com"
32+
case defaultActionsURLSelf:
33+
return strings.TrimSuffix(AppURL, "/")
34+
default:
35+
// This should never happen, but just in case, use GitHub as fallback
36+
return "https://github.com"
37+
}
38+
}
39+
40+
const (
41+
defaultActionsURLGitHub = "github" // https://github.com
42+
defaultActionsURLSelf = "self" // the root URL of the self-hosted Gitea instance
43+
// DefaultActionsURL only supports GitHub and the self-hosted Gitea.
44+
// It's intentionally not supported more, so please be cautious before adding more like "gitea" or "gitlab".
45+
// If you get some trouble with `uses: username/action_name@version` in your workflow,
46+
// please consider to use `uses: https://the_url_you_want_to_use/username/action_name@version` instead.
47+
)
48+
2349
func loadActionsFrom(rootCfg ConfigProvider) error {
2450
sec := rootCfg.Section("actions")
2551
err := sec.MapTo(&Actions)
2652
if err != nil {
2753
return fmt.Errorf("failed to map Actions settings: %v", err)
2854
}
2955

56+
if urls := string(Actions.DefaultActionsURL); urls != defaultActionsURLGitHub && urls != defaultActionsURLSelf {
57+
url := strings.Split(urls, ",")[0]
58+
if strings.HasPrefix(url, "https://") || strings.HasPrefix(url, "http://") {
59+
log.Error("[actions] DEFAULT_ACTIONS_URL does not support %q as custom URL any longer, fallback to %q",
60+
urls,
61+
defaultActionsURLGitHub,
62+
)
63+
Actions.DefaultActionsURL = defaultActionsURLGitHub
64+
} else {
65+
return fmt.Errorf("unsupported [actions] DEFAULT_ACTIONS_URL: %q", urls)
66+
}
67+
}
68+
3069
// don't support to read configuration from [actions]
3170
Actions.LogStorage, err = getStorage(rootCfg, "actions_log", "", nil)
3271
if err != nil {

modules/setting/actions_test.go

+84
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"testing"
99

1010
"github.com/stretchr/testify/assert"
11+
"github.com/stretchr/testify/require"
1112
)
1213

1314
func Test_getStorageInheritNameSectionTypeForActions(t *testing.T) {
@@ -95,3 +96,86 @@ STORAGE_TYPE = minio
9596
assert.EqualValues(t, "local", Actions.ArtifactStorage.Type)
9697
assert.EqualValues(t, "actions_artifacts", filepath.Base(Actions.ArtifactStorage.Path))
9798
}
99+
100+
func Test_getDefaultActionsURLForActions(t *testing.T) {
101+
oldActions := Actions
102+
oldAppURL := AppURL
103+
defer func() {
104+
Actions = oldActions
105+
AppURL = oldAppURL
106+
}()
107+
108+
AppURL = "http://test_get_default_actions_url_for_actions:3000/"
109+
110+
tests := []struct {
111+
name string
112+
iniStr string
113+
wantErr assert.ErrorAssertionFunc
114+
wantURL string
115+
}{
116+
{
117+
name: "default",
118+
iniStr: `
119+
[actions]
120+
`,
121+
wantErr: assert.NoError,
122+
wantURL: "https://github.com",
123+
},
124+
{
125+
name: "github",
126+
iniStr: `
127+
[actions]
128+
DEFAULT_ACTIONS_URL = github
129+
`,
130+
wantErr: assert.NoError,
131+
wantURL: "https://github.com",
132+
},
133+
{
134+
name: "self",
135+
iniStr: `
136+
[actions]
137+
DEFAULT_ACTIONS_URL = self
138+
`,
139+
wantErr: assert.NoError,
140+
wantURL: "http://test_get_default_actions_url_for_actions:3000",
141+
},
142+
{
143+
name: "custom url",
144+
iniStr: `
145+
[actions]
146+
DEFAULT_ACTIONS_URL = https://gitea.com
147+
`,
148+
wantErr: assert.NoError,
149+
wantURL: "https://github.com",
150+
},
151+
{
152+
name: "custom urls",
153+
iniStr: `
154+
[actions]
155+
DEFAULT_ACTIONS_URL = https://gitea.com,https://github.com
156+
`,
157+
wantErr: assert.NoError,
158+
wantURL: "https://github.com",
159+
},
160+
{
161+
name: "invalid",
162+
iniStr: `
163+
[actions]
164+
DEFAULT_ACTIONS_URL = gitea
165+
`,
166+
wantErr: assert.Error,
167+
wantURL: "https://github.com",
168+
},
169+
}
170+
171+
for _, tt := range tests {
172+
t.Run(tt.name, func(t *testing.T) {
173+
cfg, err := NewConfigProviderFromData(tt.iniStr)
174+
require.NoError(t, err)
175+
if !tt.wantErr(t, loadActionsFrom(cfg)) {
176+
return
177+
}
178+
assert.EqualValues(t, tt.wantURL, Actions.DefaultActionsURL.URL())
179+
})
180+
}
181+
}

routers/api/actions/runner/utils.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ func generateTaskContext(t *actions_model.ActionTask) *structpb.Struct {
139139
"workspace": "", // string, The default working directory on the runner for steps, and the default location of your repository when using the checkout action.
140140

141141
// additional contexts
142-
"gitea_default_actions_url": setting.Actions.DefaultActionsURL,
142+
"gitea_default_actions_url": setting.Actions.DefaultActionsURL.URL(),
143143
})
144144
if err != nil {
145145
log.Error("structpb.NewStruct failed: %v", err)

0 commit comments

Comments
 (0)