Skip to content

Commit fcb535c

Browse files
authored
Sign merges, CRUD, Wiki and Repository initialisation with gpg key (#7631)
This PR fixes #7598 by providing a configurable way of signing commits across the Gitea instance. Per repository configurability and import/generation of trusted secure keys is not provided by this PR - from a security PoV that's probably impossible to do properly. Similarly web-signing, that is asking the user to sign something, is not implemented - this could be done at a later stage however. ## Features - [x] If commit.gpgsign is set in .gitconfig sign commits and files created through repofiles. (merges should already have been signed.) - [x] Verify commits signed with the default gpg as valid - [x] Signer, Committer and Author can all be different - [x] Allow signer to be arbitrarily different - We still require the key to have an activated email on Gitea. A more complete implementation would be to use a keyserver and mark external-or-unactivated with an "unknown" trust level icon. - [x] Add a signing-key.gpg endpoint to get the default gpg pub key if available - Rather than add a fake web-flow user I've added this as an endpoint on /api/v1/signing-key.gpg - [x] Try to match the default key with a user on gitea - this is done at verification time - [x] Make things configurable? - app.ini configuration done - [x] when checking commits are signed need to check if they're actually verifiable too - [x] Add documentation I have decided that adjusting the docker to create a default gpg key is not the correct thing to do and therefore have not implemented this.
1 parent 1b72690 commit fcb535c

40 files changed

+1627
-121
lines changed

Makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,10 @@ fmt-check:
168168
test:
169169
GO111MODULE=on $(GO) test -mod=vendor -tags='sqlite sqlite_unlock_notify' $(PACKAGES)
170170

171+
.PHONY: test\#%
172+
test\#%:
173+
GO111MODULE=on $(GO) test -mod=vendor -tags='sqlite sqlite_unlock_notify' -run $* $(PACKAGES)
174+
171175
.PHONY: coverage
172176
coverage:
173177
@hash gocovmerge > /dev/null 2>&1; if [ $$? -ne 0 ]; then \

custom/conf/app.ini.sample

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,37 @@ WORK_IN_PROGRESS_PREFIXES=WIP:,[WIP]
7474
; List of reasons why a Pull Request or Issue can be locked
7575
LOCK_REASONS=Too heated,Off-topic,Resolved,Spam
7676

77+
[repository.signing]
78+
; GPG key to use to sign commits, Defaults to the default - that is the value of git config --get user.signingkey
79+
; run in the context of the RUN_USER
80+
; Switch to none to stop signing completely
81+
SIGNING_KEY = default
82+
; If a SIGNING_KEY ID is provided and is not set to default, use the provided Name and Email address as the signer.
83+
; These should match a publicized name and email address for the key. (When SIGNING_KEY is default these are set to
84+
; the results of git config --get user.name and git config --get user.email respectively and can only be overrided
85+
; by setting the SIGNING_KEY ID to the correct ID.)
86+
SIGNING_NAME =
87+
SIGNING_EMAIL =
88+
; Determines when gitea should sign the initial commit when creating a repository
89+
; Either:
90+
; - never
91+
; - pubkey: only sign if the user has a pubkey
92+
; - twofa: only sign if the user has logged in with twofa
93+
; - always
94+
; options other than none and always can be combined as comma separated list
95+
INITIAL_COMMIT = always
96+
; Determines when to sign for CRUD actions
97+
; - as above
98+
; - parentsigned: requires that the parent commit is signed.
99+
CRUD_ACTIONS = pubkey, twofa, parentsigned
100+
; Determines when to sign Wiki commits
101+
; - as above
102+
WIKI = never
103+
; Determines when to sign on merges
104+
; - basesigned: require that the parent of commit on the base repo is signed.
105+
; - commitssigned: require that all the commits in the head branch are signed.
106+
MERGES = pubkey, twofa, basesigned, commitssigned
107+
77108
[cors]
78109
; More information about CORS can be found here: https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS#The_HTTP_response_headers
79110
; enable cors headers (disabled by default)

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,25 @@ Values containing `#` or `;` must be quoted using `` ` `` or `"""`.
7676

7777
- `LOCK_REASONS`: **Too heated,Off-topic,Resolved,Spam**: A list of reasons why a Pull Request or Issue can be locked
7878

79+
### Repository - Signing (`repository.signing`)
80+
81+
- `SIGNING_KEY`: **default**: \[none, KEYID, default \]: Key to sign with.
82+
- `SIGNING_NAME` & `SIGNING_EMAIL`: if a KEYID is provided as the `SIGNING_KEY`, use these as the Name and Email address of the signer. These should match publicized name and email address for the key.
83+
- `INITIAL_COMMIT`: **always**: \[never, pubkey, twofa, always\]: Sign initial commit.
84+
- `never`: Never sign
85+
- `pubkey`: Only sign if the user has a public key
86+
- `twofa`: Only sign if the user is logged in with twofa
87+
- `always`: Always sign
88+
- Options other than `never` and `always` can be combined as a comma separated list.
89+
- `WIKI`: **never**: \[never, pubkey, twofa, always, parentsigned\]: Sign commits to wiki.
90+
- `CRUD_ACTIONS`: **pubkey, twofa, parentsigned**: \[never, pubkey, twofa, parentsigned, always\]: Sign CRUD actions.
91+
- Options as above, with the addition of:
92+
- `parentsigned`: Only sign if the parent commit is signed.
93+
- `MERGES`: **pubkey, twofa, basesigned, commitssigned**: \[never, pubkey, twofa, basesigned, commitssigned, always\]: Sign merges.
94+
- `basesigned`: Only sign if the parent commit in the base repo is signed.
95+
- `headsigned`: Only sign if the head commit in the head branch is signed.
96+
- `commitssigned`: Only sign if all the commits in the head branch to the merge point are signed.
97+
7998
## CORS (`cors`)
8099

81100
- `ENABLED`: **false**: enable cors headers (disabled by default)
Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
---
2+
date: "2019-08-17T10:20:00+01:00"
3+
title: "GPG Commit Signatures"
4+
slug: "signing"
5+
weight: 20
6+
toc: false
7+
draft: false
8+
menu:
9+
sidebar:
10+
parent: "advanced"
11+
name: "GPG Commit Signatures"
12+
weight: 20
13+
identifier: "signing"
14+
---
15+
16+
# GPG Commit Signatures
17+
18+
Gitea will verify GPG commit signatures in the provided tree by
19+
checking if the commits are signed by a key within the gitea database,
20+
or if the commit matches the default key for git.
21+
22+
Keys are not checked to determine if they have expired or revoked.
23+
Keys are also not checked with keyservers.
24+
25+
A commit will be marked with a grey unlocked icon if no key can be
26+
found to verify it. If a commit is marked with a red unlocked icon,
27+
it is reported to be signed with a key with an id.
28+
29+
Please note: The signer of a commit does not have to be an author or
30+
committer of a commit.
31+
32+
This functionality requires git >= 1.7.9 but for full functionality
33+
this requires git >= 2.0.0.
34+
35+
## Automatic Signing
36+
37+
There are a number of places where Gitea will generate commits itself:
38+
39+
* Repository Initialisation
40+
* Wiki Changes
41+
* CRUD actions using the editor or the API
42+
* Merges from Pull Requests
43+
44+
Depending on configuration and server trust you may want Gitea to
45+
sign these commits.
46+
47+
## General Configuration
48+
49+
Gitea's configuration for signing can be found with the
50+
`[repository.signing]` section of `app.ini`:
51+
52+
```ini
53+
...
54+
[repository.signing]
55+
SIGNING_KEY = default
56+
SIGNING_NAME =
57+
SIGNING_EMAIL =
58+
INITIAL_COMMIT = always
59+
CRUD_ACTIONS = pubkey, twofa, parentsigned
60+
WIKI = never
61+
MERGES = pubkey, twofa, basesigned, commitssigned
62+
63+
...
64+
```
65+
66+
### `SIGNING_KEY`
67+
68+
The first option to discuss is the `SIGNING_KEY`. There are three main
69+
options:
70+
71+
* `none` - this prevents Gitea from signing any commits
72+
* `default` - Gitea will default to the key configured within
73+
`git config`
74+
* `KEYID` - Gitea will sign commits with the gpg key with the ID
75+
`KEYID`. In this case you should provide a `SIGNING_NAME` and
76+
`SIGNING_EMAIL` to be displayed for this key.
77+
78+
The `default` option will interrogate `git config` for
79+
`commit.gpgsign` option - if this is set, then it will use the results
80+
of the `user.signingkey`, `user.name` and `user.email` as appropriate.
81+
82+
Please note: by adjusting git's `config` file within Gitea's
83+
repositories, `SIGNING_KEY=default` could be used to provide different
84+
signing keys on a per-repository basis. However, this is cleary not an
85+
ideal UI and therefore subject to change.
86+
87+
### `INITIAL_COMMIT`
88+
89+
This option determines whether Gitea should sign the initial commit
90+
when creating a repository. The possible values are:
91+
92+
* `never`: Never sign
93+
* `pubkey`: Only sign if the user has a public key
94+
* `twofa`: Only sign if the user logs in with two factor authentication
95+
* `always`: Always sign
96+
97+
Options other than `never` and `always` can be combined as a comma
98+
separated list.
99+
100+
### `WIKI`
101+
102+
This options determines if Gitea should sign commits to the Wiki.
103+
The possible values are:
104+
105+
* `never`: Never sign
106+
* `pubkey`: Only sign if the user has a public key
107+
* `twofa`: Only sign if the user logs in with two factor authentication
108+
* `parentsigned`: Only sign if the parent commit is signed.
109+
* `always`: Always sign
110+
111+
Options other than `never` and `always` can be combined as a comma
112+
separated list.
113+
114+
### `CRUD_ACTIONS`
115+
116+
This option determines if Gitea should sign commits from the web
117+
editor or API CRUD actions. The possible values are:
118+
119+
* `never`: Never sign
120+
* `pubkey`: Only sign if the user has a public key
121+
* `twofa`: Only sign if the user logs in with two factor authentication
122+
* `parentsigned`: Only sign if the parent commit is signed.
123+
* `always`: Always sign
124+
125+
Options other than `never` and `always` can be combined as a comma
126+
separated list.
127+
128+
### `MERGES`
129+
130+
This option determines if Gitea should sign merge commits from PRs.
131+
The possible options are:
132+
133+
* `never`: Never sign
134+
* `pubkey`: Only sign if the user has a public key
135+
* `twofa`: Only sign if the user logs in with two factor authentication
136+
* `basesigned`: Only sign if the parent commit in the base repo is signed.
137+
* `headsigned`: Only sign if the head commit in the head branch is signed.
138+
* `commitssigned`: Only sign if all the commits in the head branch to the merge point are signed.
139+
* `always`: Always sign
140+
141+
Options other than `never` and `always` can be combined as a comma
142+
separated list.
143+
144+
## Installing and generating a GPG key for Gitea
145+
146+
It is up to a server administrator to determine how best to install
147+
a signing key. Gitea generates all its commits using the server `git`
148+
command at present - and therefore the server `gpg` will be used for
149+
signing (if configured.) Administrators should review best-practices
150+
for gpg - in particular it is probably advisable to only install a
151+
signing secret subkey without the master signing and certifying secret
152+
key.
153+
154+
## Obtaining the Public Key of the Signing Key
155+
156+
The public key used to sign Gitea's commits can be obtained from the API at:
157+
158+
```/api/v1/signing-key.gpg```
159+
160+
In cases where there is a repository specific key this can be obtained from:
161+
162+
```/api/v1/repos/:username/:reponame/signing-key.gpg```

integrations/api_helper_for_declarative_test.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,3 +231,38 @@ func doAPIMergePullRequest(ctx APITestContext, owner, repo string, index int64)
231231
ctx.Session.MakeRequest(t, req, 200)
232232
}
233233
}
234+
235+
func doAPIGetBranch(ctx APITestContext, branch string, callback ...func(*testing.T, api.Branch)) func(*testing.T) {
236+
return func(t *testing.T) {
237+
req := NewRequestf(t, "GET", "/api/v1/repos/%s/%s/branches/%s?token=%s", ctx.Username, ctx.Reponame, branch, ctx.Token)
238+
if ctx.ExpectedCode != 0 {
239+
ctx.Session.MakeRequest(t, req, ctx.ExpectedCode)
240+
return
241+
}
242+
resp := ctx.Session.MakeRequest(t, req, http.StatusOK)
243+
244+
var branch api.Branch
245+
DecodeJSON(t, resp, &branch)
246+
if len(callback) > 0 {
247+
callback[0](t, branch)
248+
}
249+
}
250+
}
251+
252+
func doAPICreateFile(ctx APITestContext, treepath string, options *api.CreateFileOptions, callback ...func(*testing.T, api.FileResponse)) func(*testing.T) {
253+
return func(t *testing.T) {
254+
url := fmt.Sprintf("/api/v1/repos/%s/%s/contents/%s?token=%s", ctx.Username, ctx.Reponame, treepath, ctx.Token)
255+
req := NewRequestWithJSON(t, "POST", url, &options)
256+
if ctx.ExpectedCode != 0 {
257+
ctx.Session.MakeRequest(t, req, ctx.ExpectedCode)
258+
return
259+
}
260+
resp := ctx.Session.MakeRequest(t, req, http.StatusCreated)
261+
262+
var contents api.FileResponse
263+
DecodeJSON(t, resp, &contents)
264+
if len(callback) > 0 {
265+
callback[0](t, contents)
266+
}
267+
}
268+
}

integrations/api_repo_file_create_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ func getExpectedFileResponseForCreate(commitID, treePath string) *api.FileRespon
9191
},
9292
Verification: &api.PayloadCommitVerification{
9393
Verified: false,
94-
Reason: "unsigned",
94+
Reason: "gpg.error.not_signed_commit",
9595
Signature: "",
9696
Payload: "",
9797
},

integrations/api_repo_file_update_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ func getExpectedFileResponseForUpdate(commitID, treePath string) *api.FileRespon
9494
},
9595
Verification: &api.PayloadCommitVerification{
9696
Verified: false,
97-
Reason: "unsigned",
97+
Reason: "gpg.error.not_signed_commit",
9898
Signature: "",
9999
Payload: "",
100100
},

0 commit comments

Comments
 (0)