Restrict UI theme file loader to JSON files under the theme config dir#1391
Open
TristanInSec wants to merge 1 commit into
Open
Restrict UI theme file loader to JSON files under the theme config dir#1391TristanInSec wants to merge 1 commit into
TristanInSec wants to merge 1 commit into
Conversation
extractUITheme accepts a path value that originates from the POST /updateTheme HTTP handler, and currently passes it straight to __non_webpack_require__. Because require() can load both .json and .js modules from anywhere on the filesystem, the handler is effectively a general-purpose file loader whose output is exposed back to the UI via window.CDAP_UI_THEME. Replace the require-based loader with a narrower implementation: - resolveAllowedThemePath() canonicalises the supplied path (handling the existing server/ and config/ relative-path shortcuts) and rejects anything whose resolved location is not inside one of the UI theme config directories. - The resolver enforces a .json extension, so the loader can no longer be pointed at a .js module. - Loading switches from __non_webpack_require__ to fs.readFileSync + JSON.parse, so non-JSON contents cannot be executed even if the path check were ever weakened. The public extractUITheme signature and return shape are unchanged, so the two callers (extractUIThemeWrapper and the /updateTheme handler) keep working with the legitimate theme paths (config/themes/light.json, server/config/themes/default.json, etc.).
Author
|
Hi @chtyim @ajainarayanan, friendly ping on this security fix and the related #1392, #1393, #1394. CLA is passing, happy to make any changes needed. Thanks! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
server/uiThemeWrapper.jscurrently loads the UI theme file by passing the caller-supplied path straight to__non_webpack_require__. ThePOST /updateThemehandler inserver/express.jsfeeds this function a path taken from the request body, so the effective contract is "load any file reachable byrequire()and expose its contents to the UI throughwindow.CDAP_UI_THEME".This PR narrows that contract:
resolveAllowedThemePath()helper canonicalises the supplied path (keeping the existingserver/andconfig/relative-path shortcuts) and refuses to return anything that does not resolve to a location inside one of the UI theme config directories..jsonextension. The resolver rejects paths with any other extension, so the loader can no longer be pointed at a.jsmodule.require()tofs.readFileSync+JSON.parse. Even if the allowlist check were ever weakened, the loaded file is no longer executed as a Node module.The exported
extractUITheme(cdapConfig, uiThemePath)signature and return shape are unchanged; the two existing callers (extractUIThemeWrapperand the/updateThemeexpress handler) continue to work with the legitimate theme paths shipped inserver/config/themes/(default.json,light.json).Testing
I exercised the new resolver in isolation against the server's
config/themesdirectory and confirmed:config/themes/light.jsonserver/config/themes/../../../etc/passwd/etc/cdap/conf/cdap-site.jsonconfig/themes/evil.jsconfig/themes/../../etc/passwd.json''default.jsonandlight.jsoncontinue to load identically to the previous implementation.