cdap-ui icon indicating copy to clipboard operation
cdap-ui copied to clipboard

Fix arbitrary code execution and predictable session tokens in /updateTheme

Open herdiyana256 opened this issue 4 months ago • 3 comments

Two issues in server/token.mjs and server/uiThemeWrapper.js combine into an exploitable chain through the POST /updateTheme route in server/express.js.

getSecretFromCDAPConfig() in server/token.mjs fell back to path.resolve(__dirname, 'config', 'development', 'session.secret.key') -- a file path string, not that file's contents -- used directly as key material whenever cdapConfig['session.secret.key'] isn't configured. That value is fully predictable by anyone who knows the install directory, so a session token can be forged with no access to any real secret. The fallback also doesn't match any real file on disk (the actual file is session_secret.key, with an underscore), which points to this being a missing fs.readFileSync() call rather than intentional behavior.

extractUITheme() in server/uiThemeWrapper.js loaded the theme file with non_webpack_require, which executes .js files as Node modules. POST /updateTheme passes req.body.uiThemePath straight into extractUITheme() with no validation beyond the (forgeable) session-token check above. Together: forge a token, then point uiThemePath at any .js file the server can read, and its code runs with the server process's privileges.

token.mjs now throws instead of falling back to a predictable value when session.secret.key isn't configured, so token generation/validation fails closed. uiThemeWrapper.js reads and JSON.parse()s theme files instead of require()-ing them, since they're data, not code. express.js additionally resolves uiThemePath in /updateTheme to a filename inside config/themes/ only, matching the route's own doc comment describing it as a way to switch between the shipped theme files for testing.

Added a regression test for each half: token_test.mjs confirms a missing secret makes generateToken throw and validateToken reject everything, including a token forged with the old fallback logic; the new uiThemeWrapper.test.js confirms a plain JSON theme file still loads and a .js file passed as the theme path is no longer executed.

yarn install didn't complete in the environment I used for this (large, older dependency tree), so I couldn't run uiThemeWrapper.test.js through the project's actual Jest config. Verified its logic instead by extracting extractUITheme's unmodified post-fix code into a standalone harness (only __dirname/log4js/lodash shimmed for environment reasons) and running it directly under Node -- confirmed the JSON case still loads and the .js case no longer executes. Happy to re-verify against the real Jest run if that's easier for a reviewer with the deps already installed.

herdiyana256 avatar Jul 25 '26 15:07 herdiyana256

All three points addressed in f77678d, threads resolved.

herdiyana256 avatar Aug 03 '26 12:08 herdiyana256

@GnsP All threads addressed, and I also removed an unused fileURLToPath import in server/token.mjs that snuck into f77678d (would've failed no-unused-vars). Ready for re-review whenever you have a moment.

herdiyana256 avatar Aug 03 '26 17:08 herdiyana256

Hi @herdiyana256,

Thank you so much for discovering this vulnerability and taking the time to contribute a solid fix! We deeply appreciate the effort you put into helping keep this project secure.

Also, thank you for your quick turnaround on the changes we discussed during the review. The code looks good to me, and I've just approved the PR so we can get this merged.

We would be absolutely thrilled to see more contributions from you in the future! Whether it's more security improvements, general bug fixes, or new features, your work is very welcome here.

Thanks again!

Best

GnsP avatar Aug 05 '26 04:08 GnsP

Thanks again @GnsP, really appreciate the kind words and the quick reviews throughout.

Since this is approved with all checks green and no conflicts with develop, is there anything left on my end before it can land, or is it just waiting on a committer to merge? Happy to rebase or adjust anything if that helps.

Would love to get this one in, and I've got #1426 and #1427 up as well whenever you have bandwidth for those.

herdiyana256 avatar Aug 08 '26 07:08 herdiyana256

Hi @GnsP gentle bump on this one.

It's been approved for two weeks now with all checks green and no conflicts with develop. Is there a particular committer I should ping to get it merged, or is there a release window it's waiting on?

Same question applies to #1426 and #1427 whenever you have bandwidth. Thanks!

herdiyana256 avatar Aug 18 '26 15:08 herdiyana256