Compare
1af4c26d6972ebf85633e56e67e28868b43f22be..b86132120dd67d67da285f075884aa9efc4c59d7
t/lib-httpd: make http-429 first-request check atomic
http-429.sh is a helper for testing retry logic. It uses "test -f" to check for the existence of a state file and later uses "touch" or "rm -f" on that file to determine if it should return a 429. This method of managing state can fail if the helper script is invoked concurrently. However, this failure does not currently manifest itself since the helper is invoked sequentially. As a preventive measure, fix the state management logic so it relies on an atomic mkdir operation to mark that a 429 was returned. When $retry_after is "permanent", always return 429 now that we do not rely on a state file that is "touch"ed and "rm"ed to indicate when to respond with a 429. Signed-off-by: Michael Montalbo <mmontalbo@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Michael Montalbo <mmontalbo@gmail.com> · 2026-09-01 15:53 UTC · committed by Junio C Hamano
5 files changed,
+160
−59
- builtin/checkout.c
- t/lib-httpd/apply-one-time-script.sh
- t/lib-httpd/http-429.sh
- t/meson.build
- t/t5567-one-time-script.sh
modified builtin/checkout.c
| @@ -1340,34 +1340,6 @@ enum checkout_command { | |||
| 1340 | 1340 | CHECKOUT_RESTORE = 3, | |
| 1341 | 1341 | }; | |
| 1342 | 1342 | ||
| 1343 | - | static void advise_disambiguating_remotes(enum checkout_command which_command) | |
| 1344 | - | { | |
| 1345 | - | const char *cmdname; | |
| 1346 | - | ||
| 1347 | - | switch (which_command) { | |
| 1348 | - | case CHECKOUT_CHECKOUT: | |
| 1349 | - | cmdname = "checkout"; | |
| 1350 | - | break; | |
| 1351 | - | case CHECKOUT_SWITCH: | |
| 1352 | - | cmdname = "switch"; | |
| 1353 | - | break; | |
| 1354 | - | default: | |
| 1355 | - | BUG("command <%d> should not reach advise_disambiguating_remotes", | |
| 1356 | - | which_command); | |
| 1357 | - | break; | |
| 1358 | - | } | |
| 1359 | - | ||
| 1360 | - | advise(_("If you meant to check out a remote tracking branch on, e.g. 'origin',\n" | |
| 1361 | - | "you can do so by fully qualifying the name with the --track option:\n" | |
| 1362 | - | "\n" | |
| 1363 | - | " git %s --track origin/<name>\n" | |
| 1364 | - | "\n" | |
| 1365 | - | "If you'd like to always have checkouts of an ambiguous <name> prefer\n" | |
| 1366 | - | "one remote, e.g. the 'origin' remote, consider setting\n" | |
| 1367 | - | "checkout.defaultRemote=origin in your config."), | |
| 1368 | - | cmdname); | |
| 1369 | - | } | |
| 1370 | - | ||
| 1371 | 1343 | static char *parse_remote_branch(const char *arg, | |
| 1372 | 1344 | struct object_id *rev, | |
| 1373 | 1345 | int could_be_checkout_paths, | |
| @@ -1383,11 +1355,35 @@ static char *parse_remote_branch(const char *arg, | |||
| 1383 | 1355 | } | |
| 1384 | 1356 | ||
| 1385 | 1357 | if (!remote && num_matches > 1) { | |
| 1386 | - | if (advice_enabled(ADVICE_CHECKOUT_AMBIGUOUS_REMOTE_BRANCH_NAME)) | |
| 1387 | - | advise_disambiguating_remotes(which_command); | |
| 1388 | - | ||
| 1389 | - | die(_("'%s' matched multiple (%d) remote tracking branches"), | |
| 1390 | - | arg, num_matches); | |
| 1358 | + | if (advice_enabled(ADVICE_CHECKOUT_AMBIGUOUS_REMOTE_BRANCH_NAME)) { | |
| 1359 | + | const char *cmdname; | |
| 1360 | + | ||
| 1361 | + | switch (which_command) { | |
| 1362 | + | case CHECKOUT_CHECKOUT: | |
| 1363 | + | cmdname = "checkout"; | |
| 1364 | + | break; | |
| 1365 | + | case CHECKOUT_SWITCH: | |
| 1366 | + | cmdname = "switch"; | |
| 1367 | + | break; | |
| 1368 | + | default: | |
| 1369 | + | BUG("command <%d> should not reach parse_remote_branch", | |
| 1370 | + | which_command); | |
| 1371 | + | break; | |
| 1372 | + | } | |
| 1373 | + | ||
| 1374 | + | advise(_("If you meant to check out a remote tracking branch on, e.g. 'origin',\n" | |
| 1375 | + | "you can do so by fully qualifying the name with the --track option:\n" | |
| 1376 | + | "\n" | |
| 1377 | + | " git %s --track origin/<name>\n" | |
| 1378 | + | "\n" | |
| 1379 | + | "If you'd like to always have checkouts of an ambiguous <name> prefer\n" | |
| 1380 | + | "one remote, e.g. the 'origin' remote, consider setting\n" | |
| 1381 | + | "checkout.defaultRemote=origin in your config."), | |
| 1382 | + | cmdname); | |
| 1383 | + | } | |
| 1384 | + | ||
| 1385 | + | die(_("'%s' matched multiple (%d) remote tracking branches"), | |
| 1386 | + | arg, num_matches); | |
| 1391 | 1387 | } | |
| 1392 | 1388 | ||
| 1393 | 1389 | return remote; | |
modified t/lib-httpd/apply-one-time-script.sh
| @@ -6,21 +6,31 @@ | |||
| 6 | 6 | # | |
| 7 | 7 | # This can be used to simulate the effects of the repository changing in | |
| 8 | 8 | # between HTTP request-response pairs. | |
| 9 | - | if test -f one-time-script | |
| 10 | - | then | |
| 11 | - | LC_ALL=C | |
| 12 | - | export LC_ALL | |
| 9 | + | test -f one-time-script || exec "$GIT_EXEC_PATH/git-http-backend" | |
| 10 | + | ||
| 11 | + | LC_ALL=C | |
| 12 | + | export LC_ALL | |
| 13 | 13 | ||
| 14 | - | "$GIT_EXEC_PATH/git-http-backend" >out | |
| 15 | - | ./one-time-script out >out_modified | |
| 14 | + | out=out.$$ | |
| 15 | + | modified=out-modified.$$ | |
| 16 | + | "$GIT_EXEC_PATH/git-http-backend" >"$out" | |
| 16 | 17 | ||
| 17 | - | if cmp -s out out_modified | |
| 18 | - | then | |
| 19 | - | cat out | |
| 20 | - | else | |
| 21 | - | cat out_modified | |
| 22 | - | rm one-time-script | |
| 23 | - | fi | |
| 18 | + | # Since Apache can execute this script for multiple requests | |
| 19 | + | # concurrently, we chain "rm one-time-script" with the logic | |
| 20 | + | # for generating a modified response. If the "rm" ran separately, | |
| 21 | + | # a concurrent request could pass the "test -f" above and | |
| 22 | + | # erroneously result in multiple modified responses or an empty | |
| 23 | + | # body depending on the race state. | |
| 24 | + | # | |
| 25 | + | # We discard stderr for ./one-time-script since it is possible | |
| 26 | + | # ./one-time-script has been removed already, which is expected | |
| 27 | + | # sometimes. In this case, the unmodified response will be returned. | |
| 28 | + | if ./one-time-script "$out" 2>/dev/null >"$modified" && | |
| 29 | + | ! cmp -s "$out" "$modified" && | |
| 30 | + | rm one-time-script 2>/dev/null | |
| 31 | + | then | |
| 32 | + | cat "$modified" | |
| 24 | 33 | else | |
| 25 | - | "$GIT_EXEC_PATH/git-http-backend" | |
| 34 | + | cat "$out" | |
| 26 | 35 | fi | |
| 36 | + | rm -f "$out" "$modified" | |
modified t/lib-httpd/http-429.sh
| @@ -3,7 +3,7 @@ | |||
| 3 | 3 | # Script to return HTTP 429 Too Many Requests responses for testing retry logic. | |
| 4 | 4 | # Usage: /http_429/<test-context>/<retry-after-value>/<repo-path> | |
| 5 | 5 | # | |
| 6 | - | # The test-context is a unique identifier for each test to isolate state files. | |
| 6 | + | # The test-context is a unique identifier for each test to isolate state directories. | |
| 7 | 7 | # The retry-after-value can be: | |
| 8 | 8 | # - A number (e.g., "1", "2", "100") - sets Retry-After header to that many seconds | |
| 9 | 9 | # - "none" - no Retry-After header | |
| @@ -26,14 +26,16 @@ repo_path="${remaining#*/}" # Get rest (repo path) | |||
| 26 | 26 | # The repo name is the first component before any "/" | |
| 27 | 27 | repo_name="${repo_path%%/*}" | |
| 28 | 28 | ||
| 29 | - | # Use current directory (HTTPD_ROOT_PATH) for state file | |
| 30 | - | # Create a safe filename from test_context, retry_after and repo_name | |
| 31 | - | # This ensures all requests for the same test context share the same state file | |
| 29 | + | # Use current directory (HTTPD_ROOT_PATH) to hold state directory | |
| 30 | + | # Create a safe directory name from test_context, retry_after and repo_name | |
| 31 | + | # This ensures all requests for the same test context share the same state directory | |
| 32 | 32 | safe_name=$(echo "${test_context}-${retry_after}-${repo_name}" | tr '/' '_' | tr -cd 'a-zA-Z0-9_-') | |
| 33 | - | state_file="http-429-state-${safe_name}" | |
| 33 | + | state="http-429-state-${safe_name}" | |
| 34 | 34 | ||
| 35 | - | # Check if this is the first call (no state file exists) | |
| 36 | - | if test -f "$state_file" | |
| 35 | + | # Check if this is the first call (no state directory exists), or if | |
| 36 | + | # the retry-after-value is "permanent", which indicates a 429 must be | |
| 37 | + | # returned for every request (even if the state directory exists). | |
| 38 | + | if test "$retry_after" != permanent && ! mkdir "$state" 2>/dev/null | |
| 37 | 39 | then | |
| 38 | 40 | # Already returned 429 once, forward to git-http-backend | |
| 39 | 41 | # Set PATH_INFO to just the repo path (without retry-after value) | |
| @@ -52,9 +54,6 @@ then | |||
| 52 | 54 | exec "$GIT_EXEC_PATH/git-http-backend" | |
| 53 | 55 | fi | |
| 54 | 56 | ||
| 55 | - | # Mark that we've returned 429 | |
| 56 | - | touch "$state_file" | |
| 57 | - | ||
| 58 | 57 | # Output HTTP 429 response | |
| 59 | 58 | printf "Status: 429 Too Many Requests\r\n" | |
| 60 | 59 | ||
| @@ -67,8 +66,7 @@ case "$retry_after" in | |||
| 67 | 66 | printf "Retry-After: invalid-format-123abc\r\n" | |
| 68 | 67 | ;; | |
| 69 | 68 | permanent) | |
| 70 | - | # Always return 429, don't set state file for success | |
| 71 | - | rm -f "$state_file" | |
| 69 | + | # Always return 429 | |
| 72 | 70 | printf "Retry-After: 1\r\n" | |
| 73 | 71 | printf "Content-Type: text/plain\r\n" | |
| 74 | 72 | printf "\r\n" | |
modified t/meson.build
| @@ -707,6 +707,7 @@ integration_tests = [ | |||
| 707 | 707 | 't5564-http-proxy.sh', | |
| 708 | 708 | 't5565-push-multiple.sh', | |
| 709 | 709 | 't5566-push-group.sh', | |
| 710 | + | 't5567-one-time-script.sh', | |
| 710 | 711 | 't5570-git-daemon.sh', | |
| 711 | 712 | 't5571-pre-push-hook.sh', | |
| 712 | 713 | 't5572-pull-submodule.sh', | |
added t/t5567-one-time-script.sh mode 100755
| @@ -0,0 +1,96 @@ | |||
| 1 | + | #!/bin/sh | |
| 2 | + | ||
| 3 | + | test_description='apply-one-time-script CGI helper is safe under concurrent requests' | |
| 4 | + | ||
| 5 | + | . ./test-lib.sh | |
| 6 | + | ||
| 7 | + | HELPER="$TEST_DIRECTORY/lib-httpd/apply-one-time-script.sh" | |
| 8 | + | ||
| 9 | + | test_expect_success PIPE 'helper only serves one rewritten response for concurrent requests' ' | |
| 10 | + | mkdir workdir fakebin && | |
| 11 | + | ENTERED="$PWD/entered" && | |
| 12 | + | GATE="$PWD/gate" && | |
| 13 | + | export ENTERED GATE && | |
| 14 | + | mkfifo "$ENTERED" "$GATE" && | |
| 15 | + | ||
| 16 | + | # A stub git-http-backend that returns a response based on | |
| 17 | + | # $ROLE. For $ROLE = modify, return the response string | |
| 18 | + | # "packfile", which ends up being modified by the example | |
| 19 | + | # one-time-script below. | |
| 20 | + | # | |
| 21 | + | # Otherwise, run the branch returning a response that | |
| 22 | + | # should be passed through, and block until released | |
| 23 | + | # by "read -r $GATE". | |
| 24 | + | write_script fakebin/git-http-backend <<-\EOF && | |
| 25 | + | printf "Status: 200 OK\r\n" | |
| 26 | + | printf "Content-Type: application/x-git-result\r\n" | |
| 27 | + | printf "\r\n" | |
| 28 | + | if test "$ROLE" = modify | |
| 29 | + | then | |
| 30 | + | printf "packfile\n" | |
| 31 | + | else | |
| 32 | + | echo entered >"$ENTERED" | |
| 33 | + | read -r released <"$GATE" | |
| 34 | + | printf "refs\n" | |
| 35 | + | fi | |
| 36 | + | EOF | |
| 37 | + | ||
| 38 | + | # An example one-time-script for apply-one-time-script | |
| 39 | + | # to execute. Checks for "packfile" in the response | |
| 40 | + | # that will be returned, and replaces it with a | |
| 41 | + | # modified response. Passes through responses without | |
| 42 | + | # "packfile" in them. | |
| 43 | + | write_script workdir/one-time-script <<-\EOF && | |
| 44 | + | if grep packfile "$1" >/dev/null | |
| 45 | + | then | |
| 46 | + | sed "/packfile/q" "$1" && | |
| 47 | + | printf "REPLACED\n" | |
| 48 | + | else | |
| 49 | + | cat "$1" | |
| 50 | + | fi | |
| 51 | + | EOF | |
| 52 | + | ||
| 53 | + | GIT_EXEC_PATH="$PWD/fakebin" && | |
| 54 | + | export GIT_EXEC_PATH && | |
| 55 | + | ||
| 56 | + | # Ensure $GATE has a reader so the test does not block indefinitely if | |
| 57 | + | # the helper is buggy and "echo released >&9" below does not unblock | |
| 58 | + | # the unmodified response gate. | |
| 59 | + | exec 9<>"$GATE" && | |
| 60 | + | ||
| 61 | + | # Launch the passthrough request in the background. Record its pid | |
| 62 | + | # so it can be killed when the test finishes if, for some reason, the | |
| 63 | + | # request stays blocked and would stall a test runner. | |
| 64 | + | { ( | |
| 65 | + | cd workdir && | |
| 66 | + | ROLE=passthrough sh "$HELPER" >../passthrough.out 2>../passthrough.err | |
| 67 | + | ) & } && | |
| 68 | + | passthrough_pid=$! && | |
| 69 | + | test_when_finished "kill $passthrough_pid 2>/dev/null || :" && | |
| 70 | + | ||
| 71 | + | # Wait until the passthrough request is "in-flight" and paused | |
| 72 | + | # mid-response. | |
| 73 | + | read -r entered <"$ENTERED" && | |
| 74 | + | ||
| 75 | + | # Launch the request for a modified response while the passthrough | |
| 76 | + | # request is concurrently "in-flight" and paused. | |
| 77 | + | ( | |
| 78 | + | cd workdir && | |
| 79 | + | ROLE=modify sh "$HELPER" >../modify.out 2>../modify.err | |
| 80 | + | ) && | |
| 81 | + | ||
| 82 | + | # Unblock the passthrough request, allowing git-http-backend to | |
| 83 | + | # complete its response. | |
| 84 | + | echo released >&9 && | |
| 85 | + | { wait "$passthrough_pid" || :; } && | |
| 86 | + | ||
| 87 | + | test_must_be_empty passthrough.err && | |
| 88 | + | test_must_be_empty modify.err && | |
| 89 | + | test_grep "Status: 200 OK" passthrough.out && | |
| 90 | + | test_grep "Status: 200 OK" modify.out && | |
| 91 | + | test_grep REPLACED modify.out && | |
| 92 | + | test_grep ! REPLACED passthrough.out && | |
| 93 | + | test_grep refs passthrough.out | |
| 94 | + | ' | |
| 95 | + | ||
| 96 | + | test_done | |