This repository was archived by the owner on May 29, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Potential fix for code scanning alert no. 120: Bad HTML filtering regexp #56
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -316,12 +316,29 @@ export class BraveSearchService { | |||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| private extractTextContent(html: string): string { | ||||||||||||||||||||||||||||||
| return html | ||||||||||||||||||||||||||||||
| .replace(/<script[^>]*>.*?<\/script>/gi, '') | ||||||||||||||||||||||||||||||
| .replace(/<style[^>]*>.*?<\/style>/gi, '') | ||||||||||||||||||||||||||||||
| .replace(/<[^>]*>/g, ' ') | ||||||||||||||||||||||||||||||
| .replace(/\s+/g, ' ') | ||||||||||||||||||||||||||||||
| .trim(); | ||||||||||||||||||||||||||||||
| if (typeof window !== 'undefined' && typeof window.DOMParser !== 'undefined') { | ||||||||||||||||||||||||||||||
| const parser = new window.DOMParser(); | ||||||||||||||||||||||||||||||
| const doc = parser.parseFromString(html, 'text/html'); | ||||||||||||||||||||||||||||||
| return doc.body.textContent?.replace(/\s+/g, ' ').trim() || ''; | ||||||||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||||||||
|
Comment on lines
+319
to
+323
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. textContent still includes <script>/<style> contents — remove those nodes before extracting text Using textContent includes the raw contents of <script> and <style> elements. To truly ignore them, strip those nodes before reading text. This also makes behavior deterministic across browsers. Apply this diff: - if (typeof window !== 'undefined' && typeof window.DOMParser !== 'undefined') {
- const parser = new window.DOMParser();
- const doc = parser.parseFromString(html, 'text/html');
- return doc.body.textContent?.replace(/\s+/g, ' ').trim() || '';
+ if (typeof DOMParser !== 'undefined') {
+ const parser = new DOMParser();
+ const doc = parser.parseFromString(html, 'text/html');
+ if (doc.body) {
+ doc.body.querySelectorAll('script,style,noscript').forEach((el) => el.remove());
+ }
+ return doc.body?.textContent?.replace(/\s+/g, ' ').trim() || '';📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||
| // Fallback: improved regex-based approach (repeat until no matches) | ||||||||||||||||||||||||||||||
| let sanitized = html; | ||||||||||||||||||||||||||||||
| let previous; | ||||||||||||||||||||||||||||||
| // Remove <script>...</script> blocks repeatedly | ||||||||||||||||||||||||||||||
| do { | ||||||||||||||||||||||||||||||
| previous = sanitized; | ||||||||||||||||||||||||||||||
| sanitized = sanitized.replace(/<script[\s\S]*?>[\s\S]*?<\/script[\s\S]*?>/gi, ''); | ||||||||||||||||||||||||||||||
| } while (sanitized !== previous); | ||||||||||||||||||||||||||||||
| // Remove <style>...</style> blocks repeatedly | ||||||||||||||||||||||||||||||
| do { | ||||||||||||||||||||||||||||||
| previous = sanitized; | ||||||||||||||||||||||||||||||
| sanitized = sanitized.replace(/<style[\s\S]*?>[\s\S]*?<\/style[\s\S]*?>/gi, ''); | ||||||||||||||||||||||||||||||
| } while (sanitized !== previous); | ||||||||||||||||||||||||||||||
| return sanitized | ||||||||||||||||||||||||||||||
| .replace(/<[^>]*>/g, ' ') | ||||||||||||||||||||||||||||||
| .replace(/\s+/g, ' ') | ||||||||||||||||||||||||||||||
| .trim(); | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
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.
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.
🛠️ Refactor suggestion
Broaden environment detection — prefer DOMParser presence over window checks
Checking window can fail in workers/iframes/SSR shims. Gate purely on DOMParser existence; instantiate via new DOMParser().
Apply this diff:
📝 Committable suggestion
🤖 Prompt for AI Agents