launcher: pass cmd.exe arguments through environment variables #2218
No reviewers
Labels
No labels
bug
documentation
duplicate
enhancement
GPU
GUI
help wanted
question
Shaders
Video
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
github/KytyPS5--KytyPS5!2218
Loading…
Reference in a new issue
No description provided.
Delete branch "github-bot/gh-2218"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
@Nawid3333 on GitHub · 2026-11-11 18:29 UTC · original
Nawid3333:fix/launcher-cmd-args-via-env→main· +277 −26 · 6 files · 5 commits · openCause
On Windows the launcher starts the emulator through
cmd.exe /K "...", so the console stays open after an error (#244 moved the command intosetNativeArguments; #1869 added closing the console after a clean exit).CmdCommandbuilds that command from the emulator path and every argument, each in quotes with embedded quotes doubled. cmd.exe reads the string before the emulator does, and two things in a value still reach it:%NAME%is expanded inside quotes, so the emulator gets a different string than the launcher sent. #2210 reports this expansion (thanks @CodeStartUp, also for #2192).The values come from the launcher's settings: the game and add-on paths, the user name, the folders for shader logs and dumps, key map entries.
Change
CmdCommandmoves tosrc/launcher/include/cmdCommand.h. It puts each value into an environment variable of the cmd.exe process (KYTY_ARG_0,KYTY_ARG_1, ...) and writes only references to them into the command:(
&& exitonly with "close after a clean exit".)/V:ONturns on delayed expansion for this cmd.exe. It replaces!KYTY_ARG_n!after the line is parsed and does not read the inserted text again, so cmd.exe takes no character of a value as syntax. Quotes and the backslashes before them are still escaped for the emulator's own argument parsing (CrtQuotedContent). An empty value is written as"", because an environment variable cannot be empty.cmd.exe does not run a command whose expanded text after the program name is longer than 8191 characters. Its own command line,
cmd.exeand the/V:ON /Kline, has the same limit (measured on Windows 11: 8183 characters aftercmd.exerun, 8184 do not); each reference takes about 17 characters there, so with very many short values this limit is reached first (third commit). An environment variable cannot hold a NUL. In these cases the launcher now shows an error box and does not start the emulator.RunInterpreterreports that toRun, which takes the game out of its running state again, so it can be started once the setting is corrected (second commit).The emulator inherits the
KYTY_ARG_nvariables and does not read them.#2210 proposes to start the emulator without cmd.exe (
QProcess::setProgramandsetArguments). That also keeps cmd.exe away from the values, but the console then closes together with the emulator, so the error text that the console keeps on screen would need a replacement. This change keeps the console as it is.Tests
launcher_cmd_command_tests(Windows only, registered with CTest aslauncher_cmd_command; the last commit adds it to the Windows job's build targets and test list) starts itself through cmd.exe with the commandCmdCommandreturns and compares the arguments it receives with the ones sent, with and without "close after a clean exit":a\\"b), a quote before cmd operators (x" & y), one and two trailing backslashes,!PATH! %PATH% ^! ^^ !!,R&D <1|2> (x) && y || z, a path with spaces and parentheses, non-ASCII text, an empty value, leading and trailing spaces, and values with LF, CR and CRLF;CmdCommandaccepts arrive. Without the third commit it accepted about 2,000 of them, and cmd.exe could not even be started with that line.exit 7to cmd.exe's input. With "close after a clean exit" and a program that returns 0, cmd.exe ends with 0 before it reads that line; after a program that returns 1, and without the option, it ends with 7, so it was still open.With main's construction of the command in place of the new one, the same test fails at the first comparison:
arguments changed on the way: sent 14, received 161on the fork's Windows runner (the count depends on the machine's environment, which%PATH%expands into; locally it was 58). The console check before it passes there too, so this change keeps that behaviour. With this change the test passes. Both were built and run on a Windows runner in the fork's CI (run 43075361317); the test with this change was also run locally on Windows 11 with clang-cl. The upstream Windows job now builds and runs it as well: in run 45040538610 on this head,launcher_cmd_commandpassed together with the other 32 selected tests, and the Linux and macOS jobs passed.Costs
Each launch copies the launcher's environment and adds one variable per argument (about 40). The emulator sees these variables and ignores them.
In a game
Demon's Souls (PPSA01341) was started with a double click in the launcher window (Windows 11, RX 9070 XT), with the global user name set to
%OS% & !x!, and the arguments of the runningkyty_emulator.exewere read from the process:bc934ef): the emulator gets--user-name "Windows_NT & !x!";%OS% & !x!, the other 40 arguments are the same as on main, and the game starts.With the user name
R&D %OS% !x!main's launcher does not start the game: after the expansion the name is longer than the 16 bytes the emulator accepts, so it prints its usage text and the console stays at the prompt. With this change the game starts with that name.With the shader log folder set to a path of more than 8,300 characters the launcher shows the error box and starts nothing, and a second double click shows the box again. Without the second commit the second double click did nothing, because the game was still marked as running.
Written with Claude Code (AI): Claude Code wrote the change and the test and ran them; GLM reviewed the diff.
@coderabbitai[bot] on GitHub · 2026-11-11 18:34 UTC · original · edited 2026-11-22 15:53 UTC
No actionable comments were generated in the recent review.
Walkthrough
The launcher adds a Windows command builder that passes nonempty arguments through environment variables and enforces the command-length limit. Interpreter startup uses the builder and reports success or failure. If startup setup fails, the caller clears the item's running state, decrements the running-game count, and schedules process deletion. A Windows-only test target checks command construction, argument handling, length limits, and console behavior.
Suggested reviewers:
nmzik,lukanatroshvili67,pipe530Change: Bug fix
Merge Risk:
Minimal · up to
68d53The Windows launcher now passes emulator arguments through environment variables and rejects arguments that are too long or contain NUL. The change is covered by a dedicated Windows test that runs in CI, and no material merge risk remains.
Pre-merge checks:
4
Full details: Title check
Explanation
The title "launcher: pass cmd.exe arguments through environment variables" matches the main change. The launcher now passes emulator argument values through KYTY_ARG_n environment variables and references them with delayed expansion in the cmd.exe command. The title is short, specific, and names the primary mechanism, so it is clear to a reviewer scanning history.
Full details: Description check
Explanation
The description explains the cause (cmd.exe expanding %NAME% inside quotes and line breaks ending the command), the change (CmdCommand moved to cmdCommand.h, values passed via KYTY_ARG_n environment variables, error handling for length and NUL limits), and the tests added. It is directly related to the changeset, so the check passes.
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.
Share
d5a06299-7757-43e7-838a-14c01b7eef0aReviewing files that changed from the base of the PR and between
63ee00ef70and68d5346cca..github/workflows/build.ymlsrc/launcher/CMakeLists.txtIncluded review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 8 remain after this review.
@coderabbitai[bot] on GitHub · 2026-11-11 18:34 UTC · original · reviewed
82e7552a-2692-49c8-aa77-dffb4abcb320Reviewing files that changed from the base of the PR and between
bc934ef034and e4964d5f807d60ffb91b870663989a4714b72527.src/launcher/CMakeLists.txtsrc/launcher/include/cmdCommand.hsrc/launcher/include/mainDialog.hsrc/launcher/src/mainDialog.cpptests/LauncherCmdCommandTests.cppIncluded review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review.
@CodeStartUp on GitHub · 2026-11-12 02:54 UTC · original · reviewed
Approved — independently verified.
I reviewed the implementation and independently verified the fix without relying on this PR's own tests.
I replicated the exact
lpCommandLineandKYTY_ARG_nenvironment produced by the newCmdCommandimplementation and executed it through real Windowscmd.exeusing the bypass payloads from the security audit.Tests included:
%CMDCMDLINE:~0,-1%and%CMDCMDLINE:~0,-8%slice payloads.%VAR%value containing a quote.R&D <1|2> (x) && y || z.!PATH!,%PATH%,^!,^^, and!!.!KYTY_ARG_0!.None of the tested payloads executed an additional command, and every tested value arrived at the emulator unchanged.
As a control, I tested the same slice payload against the previous command construction used by
main; it still executed a command. This confirms that the test distinguishes the vulnerable implementation from the proposed fix.The fix works structurally: argument values are stored in environment variables and inserted through delayed expansion (
/V:ON). The values are expanded aftercmd.exehas parsed the command operators and quotes, so they cannot alter the command structure or introduce additional commands through the tested mechanism.I also verified that close-on-clean-exit behaviour remains correct, including exit codes. Overlong and NUL-containing arguments are rejected with an error message instead of being silently truncated, while the game remains startable.
Non-blocking observations:
cmd.exe(#2210) remains a simpler long-term design if console behaviour permits it.cmd.exestartup failure on Windows still does not display an error box; this appears to be pre-existing behaviour.Based on this independent testing, I consider the proposed fix effective against the tested command-injection payloads and approve this PR.
KytyPS5-PR2218-Verification.zip
@Nawid3333 on GitHub · 2026-11-12 03:25 UTC · original
Yes, both, on Windows:
echo <marker>as the injected command. The same construction as in this PR passed: every value arrived unchanged and no marker was printed. Main's construction failed with "a value ran a command". The test in this PR keeps only harmless values.LauncherCmdCommandTests: ok.No value goes through cmd's parser. Each non-empty argument is stored in an environment variable (
KYTY_ARG_n), and the command line only names it with delayed expansion (/V:ON,"!KYTY_ARG_n!"); an empty argument is written as"". cmd.exe replaces!KYTY_ARG_n!after it has parsed the line and does not read the inserted text again, so a%,",&or!inside a value cannot end its quoted argument.@CodeStartUp on GitHub · 2026-11-12 03:48 UTC · original · approved these changes
@CodeStartUp on GitHub · 2026-11-12 04:28 UTC · original · approved these changes
@coderabbitai[bot] on GitHub · 2026-11-21 19:43 UTC · original · reviewed
Actionable comments posted: 1
e1aebc67-3cfd-44ac-a27f-bbe607f664dcReviewing files that changed from the base of the PR and between b3c8a3061a0245949e2b805e37b30db2c21e72f6 and
63ee00ef70.src/launcher/CMakeLists.txtIncluded review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 8 remain after this review.
@ -58,2 +58,4 @@set_target_properties(launcher_cheat_tests PROPERTIES AUTOMOC ON)if(WIN32)add_executable(launcher_cmd_command_tests EXCLUDE_FROM_ALL@coderabbitai[bot] on GitHub · 2026-11-21 19:43 UTC · original · edited 2026-11-21 19:55 UTC
Register the new Windows test with the regression targets.
The README says the
kyty_teststarget builds every regression executable and thatctestruns the registered tests.launcher_cmd_command_testsis neither akyty_testsdependency nor a CTest test. Neither documented command builds or runs it, so a regression in the argument-passing path would not be caught by the documented workflow. Add the target to thekyty_testsdependencies, and register it withadd_testin the Windows-only block. Follow theBUILD_TESTINGguard that the rootCMakeLists.txtuses for other tests. If launcher tests are intentionally kept outsidekyty_tests, document that exception in the README instead.@ -58,2 +58,4 @@set_target_properties(launcher_cheat_tests PROPERTIES AUTOMOC ON)if(WIN32)add_executable(launcher_cmd_command_tests EXCLUDE_FROM_ALL@Nawid3333 on GitHub · 2026-11-21 19:55 UTC · original
launcher_game_views_testsandlauncher_cheat_testsare registered the same way on main:EXCLUDE_FROM_ALLexecutables insrc/launcher/CMakeLists.txt, not inkyty_testsand withoutadd_test.kyty_testsis defined in the rootCMakeLists.txtbefore the launcher subdirectory is added, and that subdirectory is optional (KYTY_BUILD_LAUNCHER), so naming a launcher target there would break builds without the launcher. Wiring the launcher tests intokyty_testsand CTest is a change for all three together, so this test stays in line with the other two.@ -58,2 +58,4 @@set_target_properties(launcher_cheat_tests PROPERTIES AUTOMOC ON)if(WIN32)add_executable(launcher_cmd_command_tests EXCLUDE_FROM_ALL@coderabbitai[bot] on GitHub · 2026-11-21 19:56 UTC · original
Analysis chain
Script executed:
Length of output: 20881
Script executed:
Length of output: 557
@Nawid3333You’re right: the new test follows the existing launcher test setup. I should have noted that the other launcher tests are also outsidekyty_testsand CTest, and that the launcher is optional.I accept keeping the tests consistent in this PR. I won’t mark the finding as addressed, because the documented commands still omit these tests.
You are interacting with an AI system.
@ -58,2 +58,4 @@set_target_properties(launcher_cheat_tests PROPERTIES AUTOMOC ON)if(WIN32)add_executable(launcher_cmd_command_tests EXCLUDE_FROM_ALL@Nawid3333 on GitHub · 2026-11-22 16:53 UTC · original
68d5346ccregisters the test with CTest aslauncher_cmd_commandand adds it to the Windows job's build targets and test list, soctestand the Windows CI run it now (it passed in run 45040538610).kyty_testsstays as it is: likelauncher_game_views_testsandlauncher_cheat_tests, the launcher tests are not part of it.@ -58,2 +58,4 @@set_target_properties(launcher_cheat_tests PROPERTIES AUTOMOC ON)if(WIN32)add_executable(launcher_cmd_command_tests EXCLUDE_FROM_ALL@coderabbitai[bot] on GitHub · 2026-11-22 16:54 UTC · original
Analysis chain
Script executed:
Length of output: 14143
@Nawid3333Confirmed: CTest registerslauncher_cmd_command, and Windows CI builds and selects it. The Windows test step passed in the linked run.I accept leaving the launcher tests outside
kyty_tests. On a clean Windows build, the documented local workflow still needs a separate build oflauncher_cmd_command_testsbefore CTest can run it. I am therefore not marking the finding as addressed.You are interacting with an AI system.
View command line instructions
Checkout
From your project repository, check out a new branch and test the changes.Merge
Merge the changes and update on Forgejo.