1import base64
2from contextlib import closing
3import importlib
4import json
5import os
6from pathlib import Path
7import posixpath
8import re
9import sqlite3
10import subprocess
11import sys
12import tempfile
13from urllib.parse import parse_qsl, quote, unquote, urlencode, urlsplit
14
15import pygit2
16from argon2 import PasswordHasher
17from argon2.exceptions import VerificationError, InvalidHashError
18
19Error = importlib.import_module('dashboard-run').Error
20# The root host helper reads repositories owned by the Shale service UID.
21pygit2.option(pygit2.GIT_OPT_SET_OWNER_VALIDATION, 0)
22
23
24def restricted_path(path):
25 parts = path.split('/')
26 return parts[0] == 'knife' or 'Cargo.lock' in parts
27
28
29def allowed_account(db, user_id):
30 row = db.execute("SELECT a.profile FROM users u JOIN auth.users a ON a.id=u.snowflake "
31 "WHERE u.id=? AND u.provider=? AND EXISTS(SELECT 1 FROM auth.memberships m "
32 "JOIN auth.roles r ON r.id=m.role_id WHERE m.user_id=a.id AND r.name='knife-native')",
33 (user_id, 'snowglobe.' + os.environ['SNOWGLOBE_DOMAIN'])).fetchone()
34 if not row:
35 return False
36 profile = json.loads(row[0])
37 return (profile.get('enabled') is True and not profile.get('requiredActions')
38 and profile.get('kind') != 'ai')
39
40
41def instance(service):
42 if service != 'shale' and not re.fullmatch(r'shale-preview-[0-9a-f]{8}', service):
43 raise Error(400, 'Choose a Shale instance.')
44 if fixture_root := os.environ.get('SNOWGLOBE_SHALE_ROOT'):
45 return Path(fixture_root)
46 site = json.loads(subprocess.check_output(['pkl', 'eval', '-f', 'json', str(Path(__file__).resolve().parent.parent / 'config/site.pkl')], text=True, timeout=5))
47 return Path(site['root']) / ('prod/shale' if service == 'shale' else 'staging/' + service)
48
49
50def target(request_uri):
51 uri = urlsplit(request_uri)
52 path = uri.path
53 for _ in range(4):
54 decoded = unquote(path, errors='strict')
55 if decoded == path:
56 break
57 path = decoded
58 path = posixpath.normpath('/' + path.lstrip('/'))
59 return uri, path, path.strip('/').split('/')
60
61
62def upload_pack(uri, path, parts):
63 """Returns the CGI path of an anonymous fetch request, the only Git route served without a token."""
64 if parts[0] not in ('the-knife', 'the-knife.git') or '%' in path or '\\' in path:
65 return None
66 if parts[1:] == ['info', 'refs'] and uri.query == 'service=git-upload-pack' or parts[1:] == ['git-upload-pack'] and not uri.query:
67 return '/the-knife.git/' + '/'.join(parts[1:])
68 return None
69
70
71def repository(root, db):
72 row = db.execute("SELECT uuid FROM repositories WHERE name='the-knife'").fetchone()
73 return root / 'repositories_owned' / row[0] if row and re.fullmatch(r'[A-Z0-9]{26}', row[0]) else None
74
75
76def protected_commits(repo, native):
77 policy = json.loads(Path(os.environ.get('SNOWGLOBE_SHALE_POLICY', str(Path(__file__).resolve().parent.parent / 'config/shale.json'))).read_text())['nativeRestriction']
78 tips = [repo[tip].peel(pygit2.Commit).id for tip in policy['commits']]
79 if native:
80 tips.append(native)
81 return {str(commit.id) for tip in tips for commit in repo.walk(tip)}
82
83
84def access(request, accounts=None):
85 root = instance(request['service'])
86 uri, path, parts = target(request['uri'])
87 if parts[0].lower() not in ('the-knife', 'the-knife.git'):
88 return {'allowed': False, 'deny': True}
89 git = parts[0].lower().endswith('.git') or len(parts) > 1 and parts[1] in ('info', 'objects', 'git-upload-pack', 'git-receive-pack', 'HEAD')
90 if not request['authorization'] and upload_pack(uri, path, parts):
91 return {'allowed': False, 'deny': False, 'clone': True}
92 with closing(sqlite3.connect((root / 'data/astheno.shale.db').as_uri() + '?mode=ro', uri=True, timeout=2)) as db:
93 accounts = Path(accounts or os.environ.get('SNOWGLOBE_DASHBOARD_DATA', '/var/lib/snowglobe/dashboard'))
94 db.execute('ATTACH DATABASE ? AS auth', ((accounts / 'accounts.sqlite').as_uri() + '?mode=ro',))
95 authorization = request['authorization']
96 allowed = False
97 if authorization:
98 try:
99 scheme, value = authorization.split(' ', 1)
100 if scheme.lower() == 'basic':
101 name, secret = base64.b64decode(value, validate=True).decode().split(':', 1)
102 users = db.execute('SELECT id FROM users WHERE name=?', (name,)).fetchall()
103 if len(users) == 1 and allowed_account(db, users[0][0]):
104 for (hashed,) in db.execute('SELECT secret FROM personal_tokens WHERE agent=?', users[0]):
105 try:
106 if PasswordHasher().verify(hashed, secret):
107 allowed = True
108 break
109 except (VerificationError, InvalidHashError):
110 pass
111 except (ValueError, UnicodeError):
112 pass
113 elif request['account'] and not git:
114 users = db.execute('SELECT id FROM users WHERE name=?', (request['account'],)).fetchall()
115 allowed = len(users) == 1 and allowed_account(db, users[0][0])
116 if allowed:
117 return {'allowed': True, 'deny': False}
118 location = repository(root, db)
119 if not location:
120 return {'allowed': False, 'deny': True}
121 repo = pygit2.Repository(str(location))
122 try:
123 head = repo.lookup_reference('HEAD').target
124 references = {name: repo.lookup_reference(name).resolve().target for name in repo.references}
125 protected = protected_commits(repo, references.get('refs/heads/native'))
126 result = {'allowed': False, 'deny': True, 'commits': sorted(protected), 'htmlOnly': True, 'authenticate': git and not authorization}
127 if parts[0] != 'the-knife' or '%' in path or '\\' in path:
128 return result
129 route = parts[1] if len(parts) > 1 else ''
130 query = dict(parse_qsl(uri.query, keep_blank_values=True))
131 if len(query) != len(parse_qsl(uri.query, keep_blank_values=True)):
132 return result
133 if route in ('', 'issues'):
134 if route == '' and query:
135 return result
136 result['htmlOnly'] = route != 'issues'
137 result['deny'] = False
138 return result
139 if route in ('heads', 'tags') and len(parts) == 2 and not query:
140 result['deny'] = False
141 return result
142 if route in ('log', 'commit') and len(parts) != (2 if route == 'log' else 3):
143 return result
144 if route not in ('log', 'tree', 'plain', 'commit'):
145 return result
146 revision = query.get('from', '-') if route == 'log' else parts[2] if len(parts) > 2 else '-'
147 suffix = ''
148 if route == 'commit':
149 revision, dot, suffix = revision.partition('.')
150 if dot and suffix not in ('diff', 'patch', 'stat', 'diffraw'):
151 return result
152 if not revision or revision == '-':
153 revision = 'HEAD'
154 # Only named refs and object IDs have the same meaning in Shale and libgit2.
155 if not re.fullmatch(r'[A-Za-z0-9_/-]+', revision):
156 return result
157 try:
158 if revision == 'HEAD':
159 oid = references.get(head) if isinstance(head, str) else head
160 else:
161 candidates = {references[name] for name in (revision, 'refs/heads/' + revision, 'refs/tags/' + revision) if name in references}
162 if len(candidates) > 1:
163 return result
164 if candidates:
165 oid = candidates.pop()
166 elif re.fullmatch(r'[0-9a-fA-F]{4,40}', revision):
167 oid = repo[revision].id
168 else:
169 return result
170 if oid is None:
171 return result
172 commit = repo[oid].peel(pygit2.Commit)
173 except (KeyError, ValueError, pygit2.GitError):
174 return result
175 if str(commit.id) in protected:
176 return result
177 if route in ('tree', 'plain'):
178 result['htmlOnly'] = route != 'plain'
179 file = '/'.join(parts[3:])
180 if restricted_path(file):
181 return result
182 if query.keys() - ({'from', 'q'} if route == 'log' else {'diff'} if route == 'commit' else set()):
183 return result
184 if route == 'commit':
185 diffs = [repo.diff(parent.tree, commit.tree) for parent in commit.parents] if commit.parents else [commit.tree.diff_to_tree(swap=True)]
186 sensitive = any(restricted_path(delta.old_file.path) or restricted_path(delta.new_file.path) for diff in diffs for delta in diff.deltas)
187 if sensitive:
188 if suffix in ('stat', 'diffraw'):
189 return result
190 result['safeDiff'] = ''.join(patch.text for patch in diffs[0] if not restricted_path(patch.delta.old_file.path) and not restricted_path(patch.delta.new_file.path)) + "\nThe Knife's Native UI is restricted\n"
191 if suffix:
192 result['rawDiff'] = True
193 result['deny'] = False
194 return result
195 if route == 'log':
196 result['uri'] = '/the-knife/log/?' + urlencode({**query, 'from': str(commit.id)})
197 else:
198 parts[2:3] = [str(commit.id) + ('.' + suffix if suffix else '')]
199 result['uri'] = '/' + '/'.join(quote(part, safe='') for part in parts) + ('/' if uri.path.endswith('/') else '')
200 if query:
201 result['uri'] += '?' + urlencode(query)
202 result['deny'] = False
203 return result
204 finally:
205 repo.free()
206
207
208def clone(request):
209 root = instance(request['service'])
210 uri, path, parts = target(request['uri'])
211 location = upload_pack(uri, path, parts)
212 if not location:
213 raise Error(404, 'Clone The Knife over HTTPS.')
214 with closing(sqlite3.connect((root / 'data/astheno.shale.db').as_uri() + '?mode=ro', uri=True, timeout=2)) as db:
215 knife = repository(root, db)
216 if not knife:
217 raise Error(404, 'The Knife is unavailable.')
218 repo = pygit2.Repository(str(knife))
219 try:
220 main = repo.lookup_reference('refs/heads/main').resolve().target
221 native = repo.lookup_reference('refs/heads/native').resolve().target if 'refs/heads/native' in repo.references else None
222 protected = protected_commits(repo, native)
223 if any(str(commit.id) in protected for commit in repo.walk(main)):
224 raise Error(403, "The Knife's main branch includes native history.")
225 finally:
226 repo.free()
227 return {'objects': str(knife / 'objects'), 'main': str(main), 'environment': {
228 'REQUEST_METHOD': request['method'], 'PATH_INFO': location, 'QUERY_STRING': uri.query,
229 'CONTENT_TYPE': request['contentType'], 'HTTP_CONTENT_ENCODING': request['contentEncoding'],
230 }}
231
232
233def serve(objects, main):
234 """Runs git http-backend on a repository whose only ref is main, borrowing the Knife's objects."""
235 repository = Path(tempfile.mkdtemp()) / 'the-knife.git'
236 (repository / 'objects/info').mkdir(parents=True)
237 (repository / 'refs/heads').mkdir(parents=True)
238 (repository / 'config').write_text('[core]\n\trepositoryformatversion = 0\n\tbare = true\n')
239 (repository / 'HEAD').write_text('ref: refs/heads/main\n')
240 (repository / 'refs/heads/main').write_text(main + '\n')
241 (repository / 'objects/info/alternates').write_text(objects + '\n')
242 # Protocol v2 serves any object the alternate holds; omitting GIT_PROTOCOL keeps v0's check that wants are reachable from main.
243 environment = {name: os.environ[name] for name in ('PATH', 'REQUEST_METHOD', 'PATH_INFO', 'QUERY_STRING', 'CONTENT_TYPE', 'HTTP_CONTENT_ENCODING') if os.environ.get(name)}
244 os.execvpe('git', ['git', 'http-backend'], environment | {
245 'GIT_PROJECT_ROOT': str(repository.parent), 'GIT_HTTP_EXPORT_ALL': '1', 'GIT_CONFIG_NOSYSTEM': '1', 'GIT_CONFIG_GLOBAL': '/dev/null',
246 })
247
248
249if __name__ == '__main__':
250 serve(*sys.argv[1:])