Skip to content

Commit f53e46c

Browse files
author
Anthony Wang
committed
If httpsig verification fails, fix Host header and try again
This fixes a very rare bug when Gitea and another AP server (confirmed to happen with Mastodon) are running on the same machine, Gitea fails to verify incoming HTTP signatures. This is because the other AP server creates the sig with the public Gitea domain as the Host. However, when Gitea receives the request, the Host header is instead localhost, so the signature verification fails. Manually changing the host header to the correct value and trying the verification again fixes the bug.
1 parent a312007 commit f53e46c

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

Diff for: routers/api/v1/activitypub/reqsignature.go

+10
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,16 @@ func verifyHTTPSignatures(ctx *gitea_context.APIContext) (authenticated bool, er
9090
// 3. Verify the other actor's key
9191
algo := httpsig.Algorithm(setting.Federation.Algorithms[0])
9292
authenticated = v.Verify(pubKey, algo) == nil
93+
if authenticated {
94+
return
95+
}
96+
// 4. When Gitea and the other ActivityPub server are running on the same machine, the Host header is sometimes incorrect
97+
r.Header["Host"] = []string{setting.Domain}
98+
v, err = httpsig.NewVerifier(r)
99+
if err != nil {
100+
return
101+
}
102+
authenticated = v.Verify(pubKey, algo) == nil
93103
return
94104
}
95105

0 commit comments

Comments
 (0)