| 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 |
use std::path::{Path, PathBuf}; |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
#[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 41 |
pub enum Operation { |
| 42 |
|
| 43 |
UploadPack, |
| 44 |
|
| 45 |
ReceivePack, |
| 46 |
|
| 47 |
UploadArchive, |
| 48 |
} |
| 49 |
|
| 50 |
impl Operation { |
| 51 |
|
| 52 |
#[must_use] |
| 53 |
pub fn command(self) -> &'static str { |
| 54 |
match self { |
| 55 |
Self::UploadPack => "git-upload-pack", |
| 56 |
Self::ReceivePack => "git-receive-pack", |
| 57 |
Self::UploadArchive => "git-upload-archive", |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
fn from_wire(s: &str) -> Option<Self> { |
| 62 |
match s { |
| 63 |
"git-upload-pack" => Some(Self::UploadPack), |
| 64 |
"git-receive-pack" => Some(Self::ReceivePack), |
| 65 |
"git-upload-archive" => Some(Self::UploadArchive), |
| 66 |
_ => None, |
| 67 |
} |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
#[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 77 |
pub enum ParseError { |
| 78 |
|
| 79 |
NoArgument, |
| 80 |
|
| 81 |
UnsupportedOperation, |
| 82 |
|
| 83 |
MissingOwner, |
| 84 |
|
| 85 |
InvalidSegment, |
| 86 |
} |
| 87 |
|
| 88 |
impl std::fmt::Display for ParseError { |
| 89 |
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 90 |
let s = match self { |
| 91 |
Self::NoArgument => "no repository argument", |
| 92 |
Self::UnsupportedOperation => "unsupported git operation", |
| 93 |
Self::MissingOwner => "repository path names no owner", |
| 94 |
Self::InvalidSegment => "invalid owner or repository name", |
| 95 |
}; |
| 96 |
f.write_str(s) |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
impl std::error::Error for ParseError {} |
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
#[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 108 |
pub struct Request<'a> { |
| 109 |
|
| 110 |
pub operation: Operation, |
| 111 |
|
| 112 |
pub owner: &'a str, |
| 113 |
|
| 114 |
|
| 115 |
pub repo: &'a str, |
| 116 |
} |
| 117 |
|
| 118 |
impl Request<'_> { |
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
#[must_use] |
| 127 |
pub fn repo_dir(&self, root: impl AsRef<Path>) -> PathBuf { |
| 128 |
root.as_ref() |
| 129 |
.join(self.owner) |
| 130 |
.join(format!("{}.git", self.repo)) |
| 131 |
} |
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
#[must_use] |
| 139 |
pub fn shell_command(&self) -> String { |
| 140 |
format!( |
| 141 |
"{} '/{}/{}.git'", |
| 142 |
self.operation.command(), |
| 143 |
self.owner, |
| 144 |
self.repo |
| 145 |
) |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
const SEGMENT_MAX: usize = 64; |
| 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 |
#[must_use] |
| 178 |
pub fn valid_segment(s: &str) -> bool { |
| 179 |
if s.is_empty() || s.len() > SEGMENT_MAX { |
| 180 |
return false; |
| 181 |
} |
| 182 |
if s.starts_with('.') || s.ends_with('.') || s.contains("..") { |
| 183 |
return false; |
| 184 |
} |
| 185 |
s.bytes() |
| 186 |
.all(|b| b.is_ascii_alphanumeric() || b == b'-' || b == b'_' || b == b'.') |
| 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 |
pub fn parse(command_line: &str) -> Result<Request<'_>, ParseError> { |
| 222 |
let (verb, rest) = command_line.split_once(' ').ok_or(ParseError::NoArgument)?; |
| 223 |
let operation = Operation::from_wire(verb).ok_or(ParseError::UnsupportedOperation)?; |
| 224 |
|
| 225 |
|
| 226 |
let path = unquote(rest.trim()); |
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
let path = path.strip_prefix('/').unwrap_or(path); |
| 231 |
|
| 232 |
let (owner, rest) = path.split_once('/').ok_or(ParseError::MissingOwner)?; |
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
let repo = rest.strip_suffix(".git").unwrap_or(rest); |
| 237 |
|
| 238 |
|
| 239 |
if !valid_segment(owner) || !valid_segment(repo) { |
| 240 |
return Err(ParseError::InvalidSegment); |
| 241 |
} |
| 242 |
|
| 243 |
Ok(Request { |
| 244 |
operation, |
| 245 |
owner, |
| 246 |
repo, |
| 247 |
}) |
| 248 |
} |
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
fn unquote(s: &str) -> &str { |
| 255 |
for q in ['\'', '"'] { |
| 256 |
if let Some(inner) = s.strip_prefix(q) |
| 257 |
&& let Some(inner) = inner.strip_suffix(q) |
| 258 |
{ |
| 259 |
return inner; |
| 260 |
} |
| 261 |
} |
| 262 |
s |
| 263 |
} |
| 264 |
|
| 265 |
pub mod oracle { |
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
use super::{Request, valid_segment}; |
| 279 |
use std::path::{Component, Path}; |
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
pub fn check(request: &Request<'_>) { |
| 287 |
|
| 288 |
assert!( |
| 289 |
valid_segment(request.owner), |
| 290 |
"accepted an invalid owner: {:?}", |
| 291 |
request.owner |
| 292 |
); |
| 293 |
assert!( |
| 294 |
valid_segment(request.repo), |
| 295 |
"accepted an invalid repo: {:?}", |
| 296 |
request.repo |
| 297 |
); |
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
let root = Path::new("/srv/git"); |
| 303 |
let dir = request.repo_dir(root); |
| 304 |
assert!( |
| 305 |
dir.starts_with(root), |
| 306 |
"repo_dir escaped the root: {}", |
| 307 |
dir.display() |
| 308 |
); |
| 309 |
let extra: Vec<_> = dir |
| 310 |
.strip_prefix(root) |
| 311 |
.expect("starts_with just held") |
| 312 |
.components() |
| 313 |
.collect(); |
| 314 |
assert_eq!( |
| 315 |
extra.len(), |
| 316 |
2, |
| 317 |
"repo_dir added {} components, not owner + repo: {}", |
| 318 |
extra.len(), |
| 319 |
dir.display() |
| 320 |
); |
| 321 |
for c in extra { |
| 322 |
assert!( |
| 323 |
matches!(c, Component::Normal(_)), |
| 324 |
"repo_dir grew a non-normal component: {}", |
| 325 |
dir.display() |
| 326 |
); |
| 327 |
} |
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
let rebuilt = request.shell_command(); |
| 334 |
match super::parse(&rebuilt) { |
| 335 |
Ok(again) => assert_eq!( |
| 336 |
&again, request, |
| 337 |
"shell_command did not round-trip: {rebuilt:?}" |
| 338 |
), |
| 339 |
Err(e) => panic!("shell_command produced an unparsable line {rebuilt:?}: {e}"), |
| 340 |
} |
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
let arg = rebuilt |
| 346 |
.strip_prefix(request.operation.command()) |
| 347 |
.expect("rebuilt starts with the verb"); |
| 348 |
assert_eq!( |
| 349 |
arg.matches('\'').count(), |
| 350 |
2, |
| 351 |
"shell argument is not one quoted word: {rebuilt:?}" |
| 352 |
); |
| 353 |
assert!( |
| 354 |
arg.starts_with(" '") && arg.ends_with('\''), |
| 355 |
"shell argument is not one quoted word: {rebuilt:?}" |
| 356 |
); |
| 357 |
} |
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
pub fn check_line(line: &str) -> bool { |
| 371 |
match super::parse(line) { |
| 372 |
Ok(request) => { |
| 373 |
check(&request); |
| 374 |
true |
| 375 |
} |
| 376 |
Err(_) => false, |
| 377 |
} |
| 378 |
} |
| 379 |
} |
| 380 |
|
| 381 |
#[cfg(test)] |
| 382 |
mod tests { |
| 383 |
use super::*; |
| 384 |
|
| 385 |
fn ok(cmd: &str) -> Request<'_> { |
| 386 |
parse(cmd).expect("should parse") |
| 387 |
} |
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
#[test] |
| 392 |
fn the_three_verbs() { |
| 393 |
assert_eq!( |
| 394 |
ok("git-upload-pack '/max/shop.git'").operation, |
| 395 |
Operation::UploadPack |
| 396 |
); |
| 397 |
assert_eq!( |
| 398 |
ok("git-receive-pack '/max/shop.git'").operation, |
| 399 |
Operation::ReceivePack |
| 400 |
); |
| 401 |
assert_eq!( |
| 402 |
ok("git-upload-archive '/max/shop.git'").operation, |
| 403 |
Operation::UploadArchive |
| 404 |
); |
| 405 |
} |
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
#[test] |
| 411 |
fn the_full_charset_is_accepted() { |
| 412 |
let r = ok("git-upload-pack my_user-1/my-repo_v2.0.git"); |
| 413 |
assert_eq!((r.owner, r.repo), ("my_user-1", "my-repo_v2.0")); |
| 414 |
} |
| 415 |
|
| 416 |
#[test] |
| 417 |
fn every_parse_error_says_which_one() { |
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
for (line, want) in [ |
| 422 |
("git-upload-pack", ParseError::NoArgument), |
| 423 |
("git-foo /max/shop.git", ParseError::UnsupportedOperation), |
| 424 |
("git-upload-pack shop.git", ParseError::MissingOwner), |
| 425 |
("git-upload-pack /max/../etc", ParseError::InvalidSegment), |
| 426 |
] { |
| 427 |
let err = parse(line).expect_err("should refuse"); |
| 428 |
assert_eq!(err, want, "{line}"); |
| 429 |
assert!(!err.to_string().is_empty(), "{want:?} displays as nothing"); |
| 430 |
} |
| 431 |
|
| 432 |
let shown = [ |
| 433 |
ParseError::NoArgument, |
| 434 |
ParseError::UnsupportedOperation, |
| 435 |
ParseError::MissingOwner, |
| 436 |
ParseError::InvalidSegment, |
| 437 |
] |
| 438 |
.map(|e| e.to_string()); |
| 439 |
let mut uniq = shown.to_vec(); |
| 440 |
uniq.sort(); |
| 441 |
uniq.dedup(); |
| 442 |
assert_eq!(uniq.len(), 4, "ParseError messages collide: {shown:?}"); |
| 443 |
} |
| 444 |
|
| 445 |
#[test] |
| 446 |
fn quoting_and_slashes_a_client_may_send() { |
| 447 |
for cmd in [ |
| 448 |
"git-upload-pack '/max/shop.git'", |
| 449 |
"git-upload-pack \"/max/shop.git\"", |
| 450 |
"git-upload-pack /max/shop.git", |
| 451 |
"git-upload-pack max/shop.git", |
| 452 |
"git-upload-pack max/shop", |
| 453 |
] { |
| 454 |
let r = ok(cmd); |
| 455 |
assert_eq!((r.owner, r.repo), ("max", "shop"), "{cmd}"); |
| 456 |
} |
| 457 |
} |
| 458 |
|
| 459 |
#[test] |
| 460 |
fn a_repo_may_contain_a_dot_that_is_not_at_either_end() { |
| 461 |
assert_eq!(ok("git-upload-pack max/v1.2.git").repo, "v1.2"); |
| 462 |
assert_eq!(ok("git-upload-pack max/v1.2.3").repo, "v1.2.3"); |
| 463 |
} |
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
#[test] |
| 469 |
fn a_trailing_dot_is_refused_because_the_git_suffix_would_make_it_dotdot() { |
| 470 |
assert!(parse("git-upload-pack max/v1.2.").is_err()); |
| 471 |
assert!(parse("git-upload-pack max./shop").is_err()); |
| 472 |
|
| 473 |
let r = ok("git-upload-pack max/v1.2.3"); |
| 474 |
assert_eq!(parse(&r.shell_command()), Ok(r)); |
| 475 |
} |
| 476 |
|
| 477 |
|
| 478 |
|
| 479 |
#[test] |
| 480 |
fn divergence_1_surrounding_whitespace_is_trimmed() { |
| 481 |
|
| 482 |
assert_eq!(ok("git-upload-pack max/shop.git ").repo, "shop"); |
| 483 |
} |
| 484 |
|
| 485 |
#[test] |
| 486 |
fn divergence_2_unbalanced_quotes_are_refused() { |
| 487 |
|
| 488 |
assert!(parse("git-upload-pack '/max/shop.git").is_err()); |
| 489 |
assert!(parse("git-upload-pack /max/shop.git'").is_err()); |
| 490 |
|
| 491 |
|
| 492 |
assert!(parse("git-upload-pack ''/max/shop.git''").is_err()); |
| 493 |
} |
| 494 |
|
| 495 |
#[test] |
| 496 |
fn divergence_3_only_one_leading_slash_is_stripped() { |
| 497 |
|
| 498 |
|
| 499 |
assert!(parse("git-upload-pack //max/shop.git").is_err()); |
| 500 |
assert!(parse("git-upload-pack ///max/shop.git").is_err()); |
| 501 |
} |
| 502 |
|
| 503 |
#[test] |
| 504 |
fn divergence_4_dotdot_is_tested_before_the_git_suffix_comes_off() { |
| 505 |
|
| 506 |
|
| 507 |
|
| 508 |
assert!(parse("git-upload-pack /max/shop..git").is_err()); |
| 509 |
assert!(parse("git-upload-pack /max/..git").is_err()); |
| 510 |
} |
| 511 |
|
| 512 |
|
| 513 |
|
| 514 |
#[test] |
| 515 |
fn traversal_in_either_segment() { |
| 516 |
for cmd in [ |
| 517 |
"git-upload-pack /../etc/passwd", |
| 518 |
"git-upload-pack /max/../../etc", |
| 519 |
"git-upload-pack ../max/shop.git", |
| 520 |
"git-upload-pack /max/..", |
| 521 |
] { |
| 522 |
assert!(parse(cmd).is_err(), "{cmd} should not parse"); |
| 523 |
} |
| 524 |
} |
| 525 |
|
| 526 |
#[test] |
| 527 |
fn a_nested_path_is_not_a_repo_name() { |
| 528 |
|
| 529 |
|
| 530 |
|
| 531 |
assert!(parse("git-upload-pack /max/a/b.git").is_err()); |
| 532 |
} |
| 533 |
|
| 534 |
#[test] |
| 535 |
fn leading_dot_segments() { |
| 536 |
assert!(parse("git-upload-pack /max/.hidden.git").is_err()); |
| 537 |
assert!(parse("git-upload-pack /.max/shop.git").is_err()); |
| 538 |
assert!(parse("git-upload-pack /max/.git").is_err()); |
| 539 |
} |
| 540 |
|
| 541 |
#[test] |
| 542 |
fn empty_segments() { |
| 543 |
assert!(parse("git-upload-pack /max/").is_err()); |
| 544 |
assert!(parse("git-upload-pack //shop.git").is_err()); |
| 545 |
assert!(parse("git-upload-pack /max").is_err()); |
| 546 |
} |
| 547 |
|
| 548 |
#[test] |
| 549 |
fn charset_is_a_whitelist() { |
| 550 |
for bad in [ |
| 551 |
"max/sh op", |
| 552 |
"max/sh;op", |
| 553 |
"max/sh\0op", |
| 554 |
"max/shüp", |
| 555 |
"ma x/shop", |
| 556 |
] { |
| 557 |
assert!( |
| 558 |
parse(&format!("git-upload-pack {bad}")).is_err(), |
| 559 |
"{bad} should not parse" |
| 560 |
); |
| 561 |
} |
| 562 |
} |
| 563 |
|
| 564 |
#[test] |
| 565 |
fn segment_length_is_capped() { |
| 566 |
let long = "a".repeat(SEGMENT_MAX + 1); |
| 567 |
assert!(parse(&format!("git-upload-pack max/{long}.git")).is_err()); |
| 568 |
let at_limit = "a".repeat(SEGMENT_MAX); |
| 569 |
assert!(parse(&format!("git-upload-pack max/{at_limit}")).is_ok()); |
| 570 |
} |
| 571 |
|
| 572 |
|
| 573 |
|
| 574 |
#[test] |
| 575 |
fn a_line_with_no_argument() { |
| 576 |
assert_eq!(parse("git-upload-pack"), Err(ParseError::NoArgument)); |
| 577 |
assert_eq!(parse(""), Err(ParseError::NoArgument)); |
| 578 |
} |
| 579 |
|
| 580 |
#[test] |
| 581 |
fn a_verb_that_is_not_served() { |
| 582 |
assert_eq!( |
| 583 |
parse("git-foo /max/shop.git"), |
| 584 |
Err(ParseError::UnsupportedOperation) |
| 585 |
); |
| 586 |
|
| 587 |
assert_eq!(parse("repo list"), Err(ParseError::UnsupportedOperation)); |
| 588 |
|
| 589 |
assert_eq!(parse("rm -rf /"), Err(ParseError::UnsupportedOperation)); |
| 590 |
} |
| 591 |
|
| 592 |
#[test] |
| 593 |
fn a_path_with_no_owner() { |
| 594 |
assert_eq!( |
| 595 |
parse("git-upload-pack shop.git"), |
| 596 |
Err(ParseError::MissingOwner) |
| 597 |
); |
| 598 |
} |
| 599 |
|
| 600 |
|
| 601 |
|
| 602 |
#[test] |
| 603 |
fn repo_dir_stays_under_the_root() { |
| 604 |
let r = ok("git-receive-pack '/max/shop.git'"); |
| 605 |
assert_eq!( |
| 606 |
r.repo_dir("/var/lib/mnw/git"), |
| 607 |
PathBuf::from("/var/lib/mnw/git/max/shop.git") |
| 608 |
); |
| 609 |
} |
| 610 |
|
| 611 |
#[test] |
| 612 |
fn shell_command_is_rebuilt_not_forwarded() { |
| 613 |
let r = ok("git-upload-pack \"max/shop\""); |
| 614 |
assert_eq!(r.shell_command(), "git-upload-pack '/max/shop.git'"); |
| 615 |
} |
| 616 |
|
| 617 |
|
| 618 |
|
| 619 |
|
| 620 |
|
| 621 |
|
| 622 |
|
| 623 |
fn req(owner: &'static str, repo: &'static str) -> Request<'static> { |
| 624 |
Request { |
| 625 |
operation: Operation::UploadPack, |
| 626 |
owner, |
| 627 |
repo, |
| 628 |
} |
| 629 |
} |
| 630 |
|
| 631 |
#[test] |
| 632 |
fn the_oracle_accepts_a_good_request() { |
| 633 |
oracle::check(&req("max", "shop")); |
| 634 |
} |
| 635 |
|
| 636 |
#[test] |
| 637 |
fn the_oracle_rejects_a_traversing_owner() { |
| 638 |
assert!(std::panic::catch_unwind(|| oracle::check(&req("..", "shop"))).is_err()); |
| 639 |
} |
| 640 |
|
| 641 |
#[test] |
| 642 |
fn the_oracle_rejects_a_separator_in_a_segment() { |
| 643 |
|
| 644 |
assert!(std::panic::catch_unwind(|| oracle::check(&req("max", "a/b"))).is_err()); |
| 645 |
} |
| 646 |
|
| 647 |
#[test] |
| 648 |
fn the_oracle_rejects_a_name_that_would_break_out_of_the_quotes() { |
| 649 |
|
| 650 |
|
| 651 |
assert!(std::panic::catch_unwind(|| oracle::check(&req("max", "x'; rm -rf /"))).is_err()); |
| 652 |
} |
| 653 |
|
| 654 |
#[test] |
| 655 |
fn check_line_reports_whether_it_checked() { |
| 656 |
assert!(oracle::check_line("git-upload-pack /max/shop.git")); |
| 657 |
assert!(!oracle::check_line("not a git command")); |
| 658 |
} |
| 659 |
} |
| 660 |
|