From 205b12589bb6802357747307d73dc0dc008858d7 Mon Sep 17 00:00:00 2001 From: ghptc Date: Tue, 27 Oct 2026 16:14:42 -0300 Subject: [PATCH] fix(security): keep screenshot filenames literal in shell hooks --- docs/security/screenshot-hook.md | 23 +++++++++++++++++++++ src/sv_screenshot.c | 5 ++++- tests/test_screenshot_security.py | 33 +++++++++++++++++++++++++++++++ 3 files changed, 60 insertions(+), 1 deletion(-) create mode 100644 docs/security/screenshot-hook.md create mode 100644 tests/test_screenshot_security.py diff --git a/docs/security/screenshot-hook.md b/docs/security/screenshot-hook.md new file mode 100644 index 00000000..fd4b7abb --- /dev/null +++ b/docs/security/screenshot-hook.md @@ -0,0 +1,23 @@ +# Screenshot hook filename boundary + +The optional Unix screenshot hook invokes a trusted configured program through a +shell. Double quotes around its filename argument do not prevent command +substitution. Before this fix, a player name containing `$()` or backticks could +survive screenshot basename validation and execute shell syntax when an operator +requested a screenshot and `sv_screenshotArrivedCmd` was configured. The default +hook is empty. No production hook or exploit was invoked during verification. + +Portable ASCII letters, digits, underscore and hyphen now survive basename +validation; other bytes become underscores. Normal player names and screenshot +numbering remain intact. The optional hook's configured program/path remains +trusted operator input; this change does not turn general process launch into a +sandbox. Replacing shell-based hooks with argument-vector process launch remains +a broader follow-up. + +`tests/test_screenshot_security.py` executes the real sanitizer and evaluates only +a harmless printf through the same double-quoted Unix argument boundary. It covers +command substitution, backticks, Windows expansion tokens, quote delimiters, +separators and path/control characters. No game client or remote endpoint is used. +Rollback is a reviewed revert; do not restore the vulnerable sanitizer while the +optional hook is in use. Deployment and any hook configuration require separate +operator approval. diff --git a/src/sv_screenshot.c b/src/sv_screenshot.c index 7b4b84d6..e700f654 100644 --- a/src/sv_screenshot.c +++ b/src/sv_screenshot.c @@ -58,7 +58,10 @@ void SV_SSFilename( int number, const char* basename, char *fileName ) { char FilenameCharValidate(char k) { - if(k <= ' ' || k == 0xff || k == '.' || k == '/' || k == '\\' || k == ':' || k == '*' || k == '?' || k == '\"' || k == '<' || k == '>' || k == '|') + /* Player-controlled basenames also reach the optional shell hook. Keep + * only portable literal filename bytes; quoting does not stop $()/backticks. */ + if(!((k >= 'a' && k <= 'z') || (k >= 'A' && k <= 'Z') || + (k >= '0' && k <= '9') || k == '_' || k == '-')) { return '_'; } diff --git a/tests/test_screenshot_security.py b/tests/test_screenshot_security.py new file mode 100644 index 00000000..d7a6b436 --- /dev/null +++ b/tests/test_screenshot_security.py @@ -0,0 +1,33 @@ +"""Execute real filename sanitizers and the optional Unix hook quoting boundary.""" +from pathlib import Path +import subprocess +import tempfile +import unittest + +ROOT = Path(__file__).resolve().parents[1] + + +class ScreenshotSecurity(unittest.TestCase): + def test_player_names_remain_literal_shell_arguments(self): + source = (ROOT / 'src/sv_screenshot.c').read_text() + functions = source[source.index('char FilenameCharValidate('):source.index('void SV_ScreenshotBuildName(')] + with tempfile.TemporaryDirectory() as directory: + fixture = Path(directory) / 'filename.c' + binary = Path(directory) / 'filename' + fixture.write_text('#include \n' + functions + + '\nint main(int argc,char **argv) { if(argc!=2)return 1; ValidateFilename(argv[1]); puts(argv[1]); return 0; }\n') + subprocess.run(['cc', '-Wall', '-Wextra', '-Werror', str(fixture), '-o', str(binary)], check=True) + for name in ('Player-One_123', '$(printf${IFS}EXPANDED)', '`printf${IFS}EXPANDED`', + '%COMSPEC%!PATH!', '";printf EXPANDED;', 'a\\b/c\nname', ''): + with self.subTest(name=name): + safe = subprocess.check_output([str(binary), name], text=True).strip() + # Same double-quoted filename argument as SV_ScreenshotArrived; + # only harmless printf runs, with no game/network/file effects. + result = subprocess.check_output(['/bin/sh', '-c', 'printf "%s" "' + safe + '"'], text=True) + self.assertEqual(result, safe) + self.assertRegex(safe, r'^[A-Za-z0-9_-]+$') + if name == 'Player-One_123': self.assertEqual(safe, name) + + +if __name__ == '__main__': + unittest.main()