Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion src/doc/files/js/chmRelative.js
Original file line number Diff line number Diff line change
@@ -1 +1,12 @@
function relativelnk(a){var b,c;b=location.href.search(/:/)==2?14:7;c=location.href.lastIndexOf("\\")+1;a="file:///"+location.href.substring(b,c)+a;location.href=a};
function relativelnk(a){
// Simple path validation: allow only relative filenames (no slashes, no colon, no protocol)
if (typeof a !== 'string' || a.match(/^[a-zA-Z0-9_.-]+$/) === null) {
console.error('Invalid path for redirect');
return;
}
var b, c;
b = location.href.search(/:/)==2 ? 14 : 7;
c = location.href.lastIndexOf("\\")+1;
a = "file:///" + location.href.substring(b, c) + a;
location.href = a;

Check warning

Code scanning / CodeQL

Client-side URL redirect Medium documentation

Untrusted URL redirection depends on a
user-provided value
.

Copilot Autofix

AI 5 months ago

To fix the client-side URL redirect issue, we should sanitize and strictly validate all parts that contribute to the redirect URL. In particular, we should avoid using unchecked data from location.href to construct file paths, and instead use a safer mechanism. Since the function intends to redirect to a locally available help file referenced by a filename, it should select from a hardcoded list of allowed filenames or simply use the provided parameter a as a relative filename, not concatenated with user-controlled path data. If directory navigation is needed, define an allowed set of directories (preferably hardcoded), and ignore any values derived from the current location. This prevents attackers from manipulating the redirect path via location.

Therefore, the best way to fix the problem is to keep the allowed filenames in a white-list array, verify a is in that array, and construct the redirect path only from trusted base directory + safe filename, avoiding the use of location.href for redirect destination construction.

Edits needed: In src/doc/files/js/chmRelative.js, update the implementation of relativelnk such that:

  • A hardcoded base directory is used (for local help files, e.g., file:///C:/Program Files/HelpFiles/).
  • The function checks that a is in a whitelist of allowed filenames.
  • Only construct the redirect URL from trusted parts, completely avoiding use of location.href for redirect destination.
    The whitelist should be declared in the same file.

Suggested changeset 1
src/doc/files/js/chmRelative.js

Autofix patch

Autofix patch
Run the following command in your local git repository to apply this patch
cat << 'EOF' | git apply
diff --git a/src/doc/files/js/chmRelative.js b/src/doc/files/js/chmRelative.js
--- a/src/doc/files/js/chmRelative.js
+++ b/src/doc/files/js/chmRelative.js
@@ -1,12 +1,20 @@
+// Whitelist of allowed help file names (add entries as needed)
+const allowedHelpFiles = [
+    "index.html",
+    "getting_started.html",
+    "faq.html",
+    "installation.html",
+    "advanced_topics.html"
+];
+
 function relativelnk(a){
-    // Simple path validation: allow only relative filenames (no slashes, no colon, no protocol)
-    if (typeof a !== 'string' || a.match(/^[a-zA-Z0-9_.-]+$/) === null) {
-        console.error('Invalid path for redirect');
+    // Only allow a to be from the whitelist of help files
+    if (typeof a !== 'string' || !allowedHelpFiles.includes(a)) {
+        console.error('Invalid or unauthorized help file requested; aborting redirect.');
         return;
     }
-    var b, c;
-    b = location.href.search(/:/)==2 ? 14 : 7;
-    c = location.href.lastIndexOf("\\")+1;
-   a = "file:///" + location.href.substring(b, c) + a;
-   location.href = a;
+    // Use a fixed trusted help files directory
+    var baseHelpPath = "file:///C:/Program Files/HelpFiles/";
+    var targetURL = baseHelpPath + a;
+    location.href = targetURL;
 };
\ No newline at end of file
EOF
@@ -1,12 +1,20 @@
// Whitelist of allowed help file names (add entries as needed)
const allowedHelpFiles = [
"index.html",
"getting_started.html",
"faq.html",
"installation.html",
"advanced_topics.html"
];

function relativelnk(a){
// Simple path validation: allow only relative filenames (no slashes, no colon, no protocol)
if (typeof a !== 'string' || a.match(/^[a-zA-Z0-9_.-]+$/) === null) {
console.error('Invalid path for redirect');
// Only allow a to be from the whitelist of help files
if (typeof a !== 'string' || !allowedHelpFiles.includes(a)) {
console.error('Invalid or unauthorized help file requested; aborting redirect.');
return;
}
var b, c;
b = location.href.search(/:/)==2 ? 14 : 7;
c = location.href.lastIndexOf("\\")+1;
a = "file:///" + location.href.substring(b, c) + a;
location.href = a;
// Use a fixed trusted help files directory
var baseHelpPath = "file:///C:/Program Files/HelpFiles/";
var targetURL = baseHelpPath + a;
location.href = targetURL;
};
Copilot is powered by AI and may make mistakes. Always verify output.
};
Loading