tirbofish/kitgit
v0.1.0 / src / git / http.rs · 9891 bytes
src/git/http.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
use crate::db::models::{Access, Repository, User};
use crate::db::queries;
use crate::git::repo::bare_path;
use crate::state::AppState;
use anyhow::Result;
use axum::body::Body;
use axum::extract::{Path, Query, State};
use axum::http::{header, HeaderMap, Request, Response, StatusCode};
use axum::response::IntoResponse;
use base64::Engine;
use futures::StreamExt;
use serde::Deserialize;
use sqlx::PgPool;
use std::process::Stdio;
use tokio::io::AsyncWriteExt;
use tokio::process::Command;
use tokio_util::io::ReaderStream;
#[derive(Deserialize)]
pub struct ServiceQuery {
pub service: Option<String>,
}
pub async fn info_refs(
State(state): State<AppState>,
Path((owner, repo)): Path<(String, String)>,
Query(q): Query<ServiceQuery>,
headers: HeaderMap,
) -> impl IntoResponse {
let repo_name = strip_git_suffix(&repo);
match authorize_git(&state, &owner, repo_name, &headers).await {
Ok((access, user, _repo)) => {
if !access.can_read() {
return if user.is_none() {
unauthorized()
} else {
forbidden()
};
}
let service = match q.service.as_deref() {
Some("git-upload-pack") => "git-upload-pack",
Some("git-receive-pack") => {
// Unauthenticated → 401 so git prompts for credentials.
// Authenticated but no write → 403.
if !access.can_write() {
return if user.is_none() {
unauthorized()
} else {
forbidden()
};
}
"git-receive-pack"
}
_ => {
return (
StatusCode::BAD_REQUEST,
"missing or unsupported service",
)
.into_response();
}
};
let path = bare_path(&state.config.repos_dir(), &owner, repo_name);
match advertise_refs(&path, service).await {
Ok(body) => Response::builder()
.status(StatusCode::OK)
.header(
header::CONTENT_TYPE,
format!("application/x-{service}-advertisement"),
)
.header(header::CACHE_CONTROL, "no-cache")
.body(Body::from(body))
.unwrap()
.into_response(),
Err(e) => {
tracing::error!("info/refs: {e:#}");
StatusCode::NOT_FOUND.into_response()
}
}
}
Err(_) => unauthorized(),
}
}
pub async fn upload_pack(
State(state): State<AppState>,
Path((owner, repo)): Path<(String, String)>,
headers: HeaderMap,
req: Request<Body>,
) -> impl IntoResponse {
let repo_name = strip_git_suffix(&repo);
match authorize_git(&state, &owner, repo_name, &headers).await {
Ok((access, user, _)) if access.can_read() => {
let path = bare_path(&state.config.repos_dir(), &owner, repo_name);
rpc_service(&path, "git-upload-pack", req).await
}
Ok((_, user, _)) => {
if user.is_none() {
unauthorized()
} else {
forbidden()
}
}
Err(_) => unauthorized(),
}
}
pub async fn receive_pack(
State(state): State<AppState>,
Path((owner, repo)): Path<(String, String)>,
headers: HeaderMap,
req: Request<Body>,
) -> impl IntoResponse {
let repo_name = strip_git_suffix(&repo);
match authorize_git(&state, &owner, repo_name, &headers).await {
Ok((access, user, repository)) if access.can_write() => {
if repository.archived {
return (
StatusCode::FORBIDDEN,
"repository is archived",
)
.into_response();
}
let path = bare_path(&state.config.repos_dir(), &owner, repo_name);
let resp = rpc_service(&path, "git-receive-pack", req).await;
if let Some(u) = user {
let _ = post_push_hooks(&state, &u, &repository, &owner).await;
}
resp
}
Ok((_, user, _)) => {
if user.is_none() {
unauthorized()
} else {
forbidden()
}
}
Err(_) => unauthorized(),
}
}
async fn post_push_hooks(
state: &AppState,
user: &User,
repo: &Repository,
owner: &str,
) -> Result<()> {
queries::record_activity(
&state.pool,
Some(user.id),
Some(repo.id),
"push",
"pushed",
serde_json::json!({}),
)
.await?;
queries::bump_commit_activity(&state.pool, user.id, chrono::Utc::now().date_naive(), 1).await?;
if let Ok(g) = crate::git::open_bare(&state.config.repos_dir(), owner, &repo.name) {
let branch = repo.default_branch.clone();
if let Ok(files) = crate::git::walk_files(&g, &branch) {
let stats = crate::git::languages::detect_languages(&files);
queries::set_language_stats(&state.pool, repo.id, stats).await?;
}
}
let _ = sqlx::query("UPDATE repositories SET updated_at = now() WHERE id = $1")
.bind(repo.id)
.execute(&state.pool)
.await;
crate::webhooks::spawn_dispatch(
state.pool.clone(),
crate::webhooks::EVENT_PUSH,
"push".into(),
repo.clone(),
owner.to_string(),
Some(user.clone()),
serde_json::json!({
"ref": format!("refs/heads/{}", repo.default_branch),
"default_branch": repo.default_branch,
}),
);
Ok(())
}
async fn advertise_refs(path: &std::path::Path, service: &str) -> Result<Vec<u8>> {
let output = Command::new(service)
.arg("--stateless-rpc")
.arg("--advertise-refs")
.arg(path)
.output()
.await?;
if !output.status.success() {
anyhow::bail!(
"{} failed: {}",
service,
String::from_utf8_lossy(&output.stderr)
);
}
let mut body = Vec::new();
let header = format!("# service={service}\n");
body.extend_from_slice(pkt_line(&header).as_slice());
body.extend_from_slice(b"0000");
body.extend_from_slice(&output.stdout);
Ok(body)
}
fn pkt_line(s: &str) -> Vec<u8> {
let len = s.len() + 4;
format!("{len:04x}{s}").into_bytes()
}
async fn rpc_service(path: &std::path::Path, service: &str, req: Request<Body>) -> Response<Body> {
let mut child = match Command::new(service)
.arg("--stateless-rpc")
.arg(path)
.stdin(Stdio::piped())
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.spawn()
{
Ok(c) => c,
Err(e) => {
tracing::error!("spawn {service}: {e}");
return StatusCode::INTERNAL_SERVER_ERROR.into_response();
}
};
let mut stdin = child.stdin.take().unwrap();
let stdout = child.stdout.take().unwrap();
let mut body = req.into_body().into_data_stream();
tokio::spawn(async move {
while let Some(Ok(chunk)) = body.next().await {
if stdin.write_all(&chunk).await.is_err() {
break;
}
}
let _ = stdin.shutdown().await;
});
let stream = ReaderStream::new(stdout);
Response::builder()
.status(StatusCode::OK)
.header(
header::CONTENT_TYPE,
format!("application/x-{service}-result"),
)
.header(header::CACHE_CONTROL, "no-cache")
.body(Body::from_stream(stream))
.unwrap()
}
async fn authorize_git(
state: &AppState,
owner: &str,
name: &str,
headers: &HeaderMap,
) -> Result<(Access, Option<User>, Repository)> {
let (repo, owner_user) = queries::get_repo(&state.pool, owner, name)
.await?
.ok_or_else(|| anyhow::anyhow!("not found"))?;
let mut user = crate::auth::current_user(&state.auth, headers).await?;
if user.is_none() {
user = basic_auth_user(&state.pool, headers).await?;
}
let access = queries::repo_access(&state.pool, &repo, user.as_ref().map(|u| u.id)).await?;
let _ = owner_user;
Ok((access, user, repo))
}
async fn basic_auth_user(pool: &PgPool, headers: &HeaderMap) -> Result<Option<User>> {
let Some(auth) = headers.get(header::AUTHORIZATION) else {
return Ok(None);
};
let Ok(s) = auth.to_str() else {
return Ok(None);
};
let Some(b64) = s.strip_prefix("Basic ") else {
return Ok(None);
};
let decoded = base64::engine::general_purpose::STANDARD.decode(b64)?;
let pair = String::from_utf8(decoded)?;
let (username, _password) = pair.split_once(':').unwrap_or((pair.as_str(), ""));
// For HTTP git with Authentik, prefer session cookie. Basic auth accepts username
// with any password matching an existing user (dev-friendly). Production should
// use deploy tokens later; for now OIDC session or SSH is preferred.
Ok(queries::get_user_by_username(pool, username)
.await?
.filter(|u| !u.is_suspended))
}
fn strip_git_suffix(name: &str) -> &str {
name.strip_suffix(".git").unwrap_or(name)
}
fn unauthorized() -> axum::response::Response {
Response::builder()
.status(StatusCode::UNAUTHORIZED)
.header(header::WWW_AUTHENTICATE, "Basic realm=\"kitgit\"")
.body(Body::from("unauthorized"))
.unwrap()
}
fn forbidden() -> axum::response::Response {
(StatusCode::FORBIDDEN, "forbidden").into_response()
}