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