From d16723ac7300bfa545f42278663e83f900ab734d Mon Sep 17 00:00:00 2001 From: Bryce Kwon Date: Sat, 7 Nov 2026 14:13:16 -1000 Subject: Run the auth checks against the real libraries The unit checks always took the harness stand-ins and the cgit-level checks needed a module to be missing, so the real luaossl and luaposix were never exercised anywhere. A misuse of their interfaces, or a login through cgit that never worked, passed every run. Where an interpreter has both libraries and its crypt(3) checks a SHA-512 hash from openssl passwd, the unit checks run again against them, adding the secret's mode. Where cgit's own Lua does, a login is posted through cgit and the cookie it grants opens the repository. Both skip where crypt checks no SHA-512 hash or the libraries are missing. --- tests/t0505-auth.sh | 94 ++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 89 insertions(+), 5 deletions(-) (limited to 'tests/t0505-auth.sh') diff --git a/tests/t0505-auth.sh b/tests/t0505-auth.sh index 35bd591..81666d5 100755 --- a/tests/t0505-auth.sh +++ b/tests/t0505-auth.sh @@ -2,17 +2,25 @@ # Checks auth-file.lua and auth-inline.lua, the shipped auth filters, which # differ only in where accounts live. The unit checks meet luaossl and -# luaposix with deterministic stand-ins from the harness, proving tampered -# and expired cookies turned away, header injection stripped, unsafe -# redirects refused and the login flows answering as documented. Through cgit -# itself an unedited copy protects nothing, with or without the modules, and -# when the binary's own Lua lacks them a protected repository stays closed. +# luaposix with deterministic stand-ins from the harness, and once more the +# real libraries wherever they are installed, proving tampered and expired +# cookies turned away, header injection stripped, unsafe redirects refused +# and the login flows answering as documented. Through cgit itself an +# unedited copy protects nothing, with or without the modules. When the +# binary's own Lua lacks them a protected repository stays closed, and when +# it has them a login opens one. A unit run is one test of some two hundred +# checks, and the names of those that failed show only under -v. test_description='Check the shipped auth extensions' CGIT_TEST_NO_CREATE_REPOS=YesPlease . ./setup.sh . "$TEST_OUTPUT_DIRECTORY/extensions/lib.sh" +# The unit checks run once more against the real luaossl and luaposix +# wherever an interpreter has them and its crypt(3) checks a SHA-512 hash. +auth_hash=$(openssl passwd -6 -salt testsalt "open sesame" 2>/dev/null) || + auth_hash= + for lua in $interpreters do for variant in inline file @@ -23,6 +31,24 @@ do "$EXTENSIONS_DIRECTORY/auth-$variant.lua" $variant "$PWD/unit-$lua-$variant" ' done + + auth_libs=AUTH_LIBS_$(echo "$lua" | tr a-z. A-Z_) + if test -n "$auth_hash" && + "$lua" -e " + require('openssl.hmac') require('openssl.rand') require('posix.sys.stat') + assert(require('posix.unistd').crypt('open sesame', '$auth_hash') == '$auth_hash') + " >/dev/null 2>&1 + then + test_set_prereq $auth_libs + fi + for variant in inline file + do + test_expect_success $auth_libs "auth-$variant checks against the real libraries under $lua" ' + mkdir "real-$lua-$variant" && + "$lua" "$EXT_TEST_DIRECTORY/test-auth.lua" \ + "$EXTENSIONS_DIRECTORY/auth-$variant.lua" $variant "$PWD/real-$lua-$variant" "$auth_hash" + ' + done done test_expect_success 'create a repository with one commit' ' @@ -40,6 +66,25 @@ if test "$CGIT_HAS_LUA" -eq 1 && then test_set_prereq CGIT_LUA_NOAUTH say 'cgit lua lacks luaossl or luaposix, expecting protected pages refused' +elif test "$CGIT_HAS_LUA" -eq 1 && test -n "$auth_hash" +then + # A login through cgit also needs the crypt(3) behind cgit's own Lua to + # check the hash. + cat >crypt-probe.lua <<-EOF + function filter_open(...) end + function filter_write(str) end + function filter_close() + if require("posix.unistd").crypt("open sesame", "$auth_hash") == "$auth_hash" then + html("CRYPT_PROBE_YES") + end + return 0 + end + EOF + cgitrc_repo crypt-probe-rc authy "repo.commit-filter=lua:$PWD/crypt-probe.lua" + if cgit_rc crypt-probe-rc "url=authy/commit/" | grep CRYPT_PROBE_YES >/dev/null + then + test_set_prereq CGIT_LUA_AUTH + fi fi test_expect_success CGIT_LUA 'point cgit at the unedited auth filter' ' @@ -77,6 +122,45 @@ test_expect_success CGIT_LUA_NOAUTH 'without the modules a protected repository grep "^\[cgit\] " err ' +# A login through cgit itself, from the form to the protected repository's +# pages, opened with the cookie it grants and closed again without it. +test_expect_success CGIT_LUA_AUTH 'a login through cgit opens a protected repository' ' + cat >grant.awk <<-EOF && + /^local secret_filename = / { print "local secret_filename = \"$PWD/login-secret\""; next } + { print } + /^local protected_repos = [{]\$/ { print "\t[\"authy\"] = { alice = true }," } + /^local users = [{]\$/ { print "\talice = \"$auth_hash\"," } + EOF + awk -f grant.awk "$EXTENSIONS_DIRECTORY/auth-inline.lua" >granted.lua && + grep "login-secret" granted.lua && + grep "testsalt" granted.lua && + grep "\"authy\"" granted.lua && + cgitrc_repo grantrc authy "auth-filter=lua:$PWD/granted.lua" && + (export HTTPS=on && cgit_ok grantrc "url=authy/log/") >form && + grep "
post && + ( + export CGIT_CONFIG="$PWD/grantrc" QUERY_STRING=p=login REQUEST_METHOD=POST \ + HTTPS=on CONTENT_LENGTH=${#body} CONTENT_TYPE=application/x-www-form-urlencoded && + cgit answer 2>err + ) && + test_must_be_empty err && + grep "^Status: 302" answer && + grep "^Location: /authy/log/\$" answer && + cookie=$(sed -n "s/^Set-Cookie: \(__Host-cgitauth=[^;]*\);.*/\1/p" answer) && + test -n "$cookie" && + (export HTTPS=on HTTP_COOKIE="$cookie" && cgit_ok grantrc "url=authy/log/") >tmp && + grep ">guarded commit" tmp && + (export HTTPS=on && cgit_ok grantrc "url=authy/log/") >tmp && + grep "