Skip to content

Commit 0aaeeb2

Browse files
fix CodeQL warnings
1 parent 588f91e commit 0aaeeb2

File tree

3 files changed

+54
-21
lines changed

3 files changed

+54
-21
lines changed

modules/help_tools/resources/help_viewer.html

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@
189189
}
190190

191191
// Public actions
192-
function searchHelp(term) {
192+
function searchHelp(term = '') {
193193
const normalizedTerm = (term || '').trim();
194194
if (!normalizedTerm) {
195195
alert(messages[lang].enter_search);
@@ -273,8 +273,22 @@
273273
if (searchTerm) {
274274
searchInput.value = searchTerm.trim();
275275
}
276-
if (openPage) {
276+
// Only allow openPage to be a safe relative path (no absolute URLs or protocol handlers)
277+
function isSafeRelativePath(p) {
278+
// No protocol, not starting with //, only allowed chars, no javascript: etc.
279+
if (!p || typeof p !== "string") return false;
280+
if (/^\s*$/.test(p)) return false;
281+
if (/^[a-z][a-z0-9+\-.]*:/i.test(p)) return false; // Disallow any protocol like http: or javascript:
282+
if (/^\/\//.test(p)) return false; // Disallow protocol-relative URLs
283+
if (/[<>:"|?*\\]/.test(p)) return false; // Disallow special chars
284+
// Allow only paths like foo/bar.html, /foo/bar or ./foo
285+
return /^[\w\-\/\.\@]+$/.test(p);
286+
}
287+
if (openPage && isSafeRelativePath(openPage)) {
277288
navigateToPage(openPage);
289+
} else if (openPage) {
290+
// Optionally log/ignore invalid path
291+
console.warn("Blocked potentially unsafe openPage value:", openPage);
278292
} else if (searchTerm) {
279293
searchHelp(searchTerm);
280294
}

modules/help_tools/resources/nelson_help.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@
8585

8686
prepareMathJaxV3Config();
8787
var local = "../tex-mml-chtml.js";
88+
8889
var cdn = "https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js";
8990

9091
function onLoad() {

modules/help_tools/resources/search_results.html

Lines changed: 37 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,20 @@ <h1 class="search-results-title"></h1>
3535

3636
<!-- localized behavior: detect lang, apply messages, append lang to links -->
3737
<script>
38+
function escapeHTML(str) {
39+
if (!str) return '';
40+
return str.replace(/[&<>"']/g, function (m) {
41+
switch (m) {
42+
case '&': return '&amp;';
43+
case '<': return '&lt;';
44+
case '>': return '&gt;';
45+
case '"': return '&quot;';
46+
case "'": return '&#39;';
47+
default: return m;
48+
}
49+
});
50+
}
51+
3852
function getQueryParam(name) {
3953
const params = new URLSearchParams(window.location.search);
4054
return params.get(name);
@@ -56,8 +70,8 @@ <h1 class="search-results-title"></h1>
5670
loading: 'Loading search index...',
5771
index_empty: 'Warning: Search index is empty or could not be loaded.',
5872
no_query: 'No search term provided.',
59-
found_results: (n, q) => `Found ${n} results for <strong>${q}</strong>:`,
60-
no_results_for: q => `No results found for <strong>${q}</strong>.`,
73+
found_results: (n, q) => `Found ${n} results for <strong>${escapeHTML(q)}</strong>:`,
74+
no_results_for: q => `No results found for <strong>${escapeHTML(q)}</strong>.`,
6175
popular_topics: 'Popular topics:',
6276
home_aria: 'Back to help homepage'
6377
},
@@ -66,8 +80,8 @@ <h1 class="search-results-title"></h1>
6680
loading: "Chargement de l'index de recherche...",
6781
index_empty: "Attention : l'index de recherche est vide ou n'a pas pu être chargé.",
6882
no_query: "Aucun terme de recherche fourni.",
69-
found_results: (n, q) => `Trouvé ${n} résultat(s) pour <strong>${q}</strong> :`,
70-
no_results_for: q => `Aucun résultat pour <strong>${q}</strong>.`,
83+
found_results: (n, q) => `Trouvé ${n} résultat(s) pour <strong>${escapeHTML(q)}</strong> :`,
84+
no_results_for: q => `Aucun résultat pour <strong>${escapeHTML(q)}</strong>.`,
7185
popular_topics: 'Sujets populaires :',
7286
home_aria: "Retour à la page d'accueil de l'aide"
7387
}
@@ -211,7 +225,7 @@ <h1 class="search-results-title"></h1>
211225
if (helpIndex.length === 0) {
212226
resultsDiv.innerHTML = `<p>${msg.index_empty}</p>`;
213227
if (query) {
214-
resultsDiv.innerHTML += `<p>${msg.no_results_for(query)}</p>`;
228+
resultsDiv.innerHTML += `<p>${msg.no_results_for(escapeHTML(query))}</p>`;
215229
}
216230
return;
217231
}
@@ -226,13 +240,16 @@ <h1 class="search-results-title"></h1>
226240

227241
results.forEach(result => {
228242
const href = appendLangToUrl(result.url || result.path || './homepage.html');
229-
html += `
230-
<div class="result-item">
231-
<h3><a href="${href}" target="contentFrame">${result.title}</a></h3>
232-
<p>${result.snippet}</p>
233-
<small>${result.path || ''}</small>
234-
</div>
235-
`;
243+
const safeTitle = escapeHTML(result.title || '');
244+
const safeSnippet = escapeHTML(result.snippet || '');
245+
const safePath = escapeHTML(result.path || '');
246+
html += `
247+
<div class="result-item">
248+
<h3><a href="${href}" target="contentFrame">${safeTitle}</a></h3>
249+
<p>${safeSnippet}</p>
250+
<small>${safePath}</small>
251+
</div>
252+
`;
236253
});
237254

238255
resultsDiv.innerHTML = html;
@@ -245,17 +262,18 @@ <h3><a href="${href}" target="contentFrame">${result.title}</a></h3>
245262
let html = `<p>${msg.popular_topics}</p>`;
246263
topResults.forEach(result => {
247264
const href = appendLangToUrl(result.url || result.path || './homepage.html');
248-
html += `
249-
<div class="result-item">
250-
<h3><a href="${href}" target="contentFrame">${result.title}</a></h3>
251-
</div>
252-
`;
253-
});
265+
const safeTitle = escapeHTML(result.title || '');
266+
html += `
267+
<div class="result-item">
268+
<h3><a href="${href}" target="contentFrame">${safeTitle}</a></h3>
269+
</div>
270+
`;
271+
});
254272
resultsDiv.innerHTML += html;
255273
}
256274
}
257275
} catch (error) {
258-
resultsDiv.innerHTML = `<p>Error loading search index: ${error && error.message ? error.message : error}</p>`;
276+
resultsDiv.innerHTML = `<p>Error loading search index: ${escapeHTML(error && error.message ? error.message : error)}</p>`;
259277
}
260278
}
261279

0 commit comments

Comments
 (0)