-
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
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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(); | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
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