-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
CDPT-2079 AJAX search fix & refactor #738
Conversation
const resultsHtml = posts.map((props) => t.renderHtml(props)); | ||
|
||
// Append the html to the content section. | ||
$("#content").append(resultsHtml); |
Check warning
Code scanning / CodeQL
DOM text reinterpreted as HTML Medium
DOM text
Show autofix suggestion
Hide autofix suggestion
Copilot Autofix AI 2 months ago
To fix the problem, we need to ensure that any HTML content generated from the template is properly sanitized before being inserted into the DOM. We can use a library like DOMPurify
to sanitize the HTML content. This will remove any potentially malicious scripts or tags from the HTML content.
- Install the
DOMPurify
library. - Import
DOMPurify
in the relevant files. - Use
DOMPurify.sanitize
to sanitize the HTML content before appending it to the DOM.
-
Copy modified line R2 -
Copy modified lines R91-R93 -
Copy modified line R95
@@ -1,2 +1,3 @@ | ||
import AjaxTemplating from "./ajax-templating.js"; | ||
import DOMPurify from "dompurify"; | ||
|
||
@@ -89,4 +90,7 @@ | ||
|
||
// Sanitize the html content. | ||
const sanitizedHtml = resultsHtml.map(html => DOMPurify.sanitize(html)); | ||
|
||
// Append the html to the content section. | ||
$("#content").append(resultsHtml); | ||
$("#content").append(sanitizedHtml); | ||
|
-
Copy modified lines R31-R32
@@ -30,3 +30,4 @@ | ||
"jquery": "^3.7.1", | ||
"node-notifier": "^10.0.1" | ||
"node-notifier": "^10.0.1", | ||
"dompurify": "^3.1.7" | ||
}, |
Package | Version | Security advisories |
dompurify (npm) | 3.1.7 | None |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is OK as I do see a way that an attacker could inject malicious code into resultsHtml
.
Would you mind offering a second opinion please @wilson1000 ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we test this? It looks good 😄
totalPages: !totalResults ? 1 : Math.ceil(totalResults / resultsPerPage), | ||
}); | ||
|
||
$(".c-pagination").html(paginationHtml); |
Check warning
Code scanning / CodeQL
DOM text reinterpreted as HTML Medium
DOM text
Show autofix suggestion
Hide autofix suggestion
Copilot Autofix AI 2 months ago
To fix the problem, we need to ensure that any dynamic content inserted into the DOM is properly sanitized or escaped to prevent XSS attacks. The best way to fix this is to use a library like DOMPurify
to sanitize the HTML content before inserting it into the DOM.
- Install the
DOMPurify
library. - Import
DOMPurify
in the relevant files. - Use
DOMPurify.sanitize
to sanitize the HTML content before inserting it into the DOM.
-
Copy modified line R2 -
Copy modified line R139
@@ -1,2 +1,3 @@ | ||
import AjaxTemplating from "./ajax-templating.js"; | ||
import DOMPurify from 'dompurify'; | ||
|
||
@@ -137,3 +138,3 @@ | ||
|
||
$(".c-pagination").html(paginationHtml); | ||
$(".c-pagination").html(DOMPurify.sanitize(paginationHtml)); | ||
|
-
Copy modified line R1 -
Copy modified line R87
@@ -1 +1,2 @@ | ||
import DOMPurify from 'dompurify'; | ||
/** | ||
@@ -85,3 +86,3 @@ | ||
|
||
return parts.join(""); | ||
return DOMPurify.sanitize(parts.join("")); | ||
} |
-
Copy modified lines R31-R32
@@ -30,3 +30,4 @@ | ||
"jquery": "^3.7.1", | ||
"node-notifier": "^10.0.1" | ||
"node-notifier": "^10.0.1", | ||
"dompurify": "^3.1.7" | ||
}, |
Package | Version | Security advisories |
dompurify (npm) | 3.1.7 | None |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @EarthlingDavey , it looks like you ventured down a rabbit hole here 😄
Can you walk me through the update tomorrow? It would be good to get your view of the improvements you've made. Thank you!
@@ -120,6 +119,9 @@ | |||
new MOJ\Intranet\WPOffloadMedia(); | |||
new MOJ\Intranet\WPElasticPress(); | |||
|
|||
$search = new MOJ\Intranet\Search(); | |||
$search->hooks(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we have to notate it like this? Can we follow the pattern of include and or do we need other $search variables in the functions.php
script?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
This PR includes a refactor of archive pages.
AJAX filter/search results are pulled in via ajax.
The underlying WP_Query for the AJAX results is the same as the WP_Query used for loading the first page of results (without AJAX).
Some bugs have been ironed out and unnecessary code has been removed.
There are still a lot of potential improvements to be made, e.g.
get-news-rest-api.php
and re-write functions that rely on it.pagination.php
and modify templates that use it, as a 2nd WP_Query should not be run just for pagination.^ having removed the file
get-blog-rest-api.php
and refactoring pagination onpage_news.php
andpage_blog.php
shows that the above improvements are feasible.