Conversation
There was a problem hiding this comment.
Pull request overview
This PR hardens extension library URL path handling to mitigate CWE-22 (path traversal) and CWE-20 (query injection) by switching from encodeURI() on whole paths to per-segment encodeURIComponent() with explicit validation.
Changes:
- Added
encodeExtPath(rawPath, trailingSlash)to canonicalize and encode extension library paths while rejecting./..segments. - Updated
read(),write(),remove(), andlist()to use the helper;write()now builds and encodes the permissions query string separately. - Added security-focused tests covering traversal rejection, space encoding, and accepted input forms.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| lib/extlibs.js | Introduces encodeExtPath and routes all extlib operations through it; refactors write() to build query string safely. |
| test-basic/extlibs.js | Adds new tests for traversal blocking and encoding/canonicalization behavior. |
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
rjdew-progress
approved these changes
Jul 8, 2026
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.
Fix CWE-22/CWE-20: Replace
encodeURIwith per-segmentencodeURIComponentin extlibs.jsJira: MLE-30257
Severity: Medium | CVSS: 3.5 Low | CWE: CWE-22, CWE-20
Problem
All four path-handling methods in extlibs.js (
read,write,remove,list) calledencodeURI()on the constructed URL path before sending the request.encodeURI()intentionally leaves/,?,#, and other reserved characters unencoded, creating two vulnerabilities:../../../v1/databasespassed through unchanged, producing/v1/ext/../../../v1/databases. HTTP clients that normalize the path resolve this to/v1/databases— an unintended MarkLogic REST endpoint.?could inject arbitrary query parameters into the request.Solution
Added a private
encodeExtPath(rawPath, trailingSlash)helper that:/v1/ext/,/ext/, or bare/) while requiring a leading/, preserving backward compatibility for existing call sites./and rejects any.or..segment with a synchronousError.encodeURIComponent()./v1/ext/base.All four methods now delegate to this helper. The
write()method is additionally refactored to encode the path first, then assemble the?perm:role=capabilityquery string separately (role names and capability values are also now encoded withencodeURIComponent()).A pre-existing bug in
list()is also corrected as a side effect: the/ext/-prefixed branch was omitting the/v1prefix from the request path.Changes
encodeExtPathhelper; updatedread(),write(),remove(),list()'when handling path encoding security'blockTests
All 16 pre-existing
extlibstests continue to pass. New tests added:read('../../../v1/databases')throws with message matching/relative path components/write('../../../v1/databases', ...)throws (positional-args form)write({ path: '../../../v1/databases', ... })throws (object-params form)remove('../../secret')throwslist('../../admin')throwsread('my lib/module.xqy')sends a percent-encoded URL the server acceptsbare,/path,/ext/path) complete without a client-side URL construction errorTesting instructions
# Requires a running MarkLogic instance with the test app deployed npx mocha test-basic/extlibs.js --timeout 10000