Sign Gitea source links with the Gitea token
settings.py, SECRET_KEY included, is part of the public repository, so links signed with it could be forged to read any repository the token can see. The token in Config/credentials.gitea is gitignored and is now the signing key. Without a token, Gitea tests are refused at creation and no link validates. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
c721cc6b5b
commit
754283d95e
2 files changed
+23
-11
No files matched your search
+18
-8
@@ -28,6 +28,11 @@
|
||||
# are public, so workers fetch them directly. Gitea servers may require a login
|
||||
# even to read, so the server hands workers a signed link to /giteaSource/,
|
||||
# which fetches the archive with the token and passes it through.
|
||||
#
|
||||
# The links are signed with the Gitea token itself, not Django's SECRET_KEY:
|
||||
# settings.py is part of the public repository, and a forged link would let
|
||||
# anyone read every repository the token can see. Replacing the token
|
||||
# invalidates the source links of tests created before.
|
||||
|
||||
import hashlib
|
||||
import hmac
|
||||
@@ -39,7 +44,7 @@ import requests
|
||||
|
||||
import OpenBench.config
|
||||
|
||||
from OpenSite.settings import PROJECT_PATH, SECRET_KEY
|
||||
from OpenSite.settings import PROJECT_PATH
|
||||
|
||||
GITHUB_PREFIX = 'https://github.com/'
|
||||
GITHUB_PATTERN = r'^https:\/\/github\.com\/[A-Za-z0-9-]+\/[A-Za-z0-9_.-]+\/?$'
|
||||
@@ -159,8 +164,13 @@ def compare_url(repo, base_sha, dev_sha):
|
||||
return '%s/compare/%s..%s' % (repo, base_sha, dev_sha)
|
||||
|
||||
def source_signature(repo, sha):
|
||||
message = ('%s\n%s' % (repo.rstrip('/'), sha.lower())).encode('utf-8')
|
||||
return hmac.new(SECRET_KEY.encode('utf-8'), message, hashlib.sha256).hexdigest()
|
||||
|
||||
# Without a token there is nothing secret to sign with, and nothing to fetch with
|
||||
if not (key := read_gitea_token()):
|
||||
return None
|
||||
|
||||
message = ('openbench-gitea-source\n%s\n%s' % (repo.rstrip('/'), sha.lower())).encode('utf-8')
|
||||
return hmac.new(key.encode('utf-8'), message, hashlib.sha256).hexdigest()
|
||||
|
||||
def source_url(request, repo, commit):
|
||||
|
||||
@@ -178,11 +188,11 @@ def source_url(request, repo, commit):
|
||||
|
||||
def verify_source_request(repo, sha, sig):
|
||||
|
||||
return (
|
||||
is_valid_repo(repo) and is_gitea(repo)
|
||||
and re.match('^[0-9a-fA-F]{40}$', sha or '')
|
||||
and hmac.compare_digest(source_signature(repo, sha), sig or '')
|
||||
)
|
||||
if not (is_valid_repo(repo) and is_gitea(repo) and re.match('^[0-9a-fA-F]{40}$', sha or '')):
|
||||
return False
|
||||
|
||||
expected = source_signature(repo, sha)
|
||||
return bool(expected) and hmac.compare_digest(expected, sig or '')
|
||||
|
||||
def open_gitea_archive(repo, sha):
|
||||
|
||||
|
||||
@@ -390,9 +390,11 @@ def collect_github_info(errors, request, field):
|
||||
errors.append('OpenBench may only reach Github\'s API or a configured Gitea server')
|
||||
return (None, None)
|
||||
|
||||
# Gitea servers get their own token, and nothing else
|
||||
if gitea:
|
||||
headers = OpenBench.git_hosts.gitea_headers()
|
||||
# Gitea servers get their own token, and nothing else. The token also signs
|
||||
# the source links handed to workers, so there is no Gitea without one.
|
||||
if gitea and not (headers := OpenBench.git_hosts.gitea_headers()):
|
||||
errors.append('Server does not have a Gitea token (Config/credentials.gitea)')
|
||||
return (None, None)
|
||||
|
||||
## Step 2: Connect to the Github or Gitea API for the given Branch or Commit SHA.
|
||||
## [A] We will attempt to parse the most recent commit message for a
|
||||
|
||||
Reference in new issue
Block a user