Add rel="noopener noreferrer" to external links for security#9
Add rel="noopener noreferrer" to external links for security#9JosunLP merged 7 commits intodevelopmentfrom
Conversation
Security and Playground Fixes: * Replaced direct assignment of HTML via `innerHTML` with safer DOM manipulation in the `logEvent` function within `playground/src/demos/core.ts` to prevent possible HTML injection vulnerabilities. * Updated collection handling in the same file to use the raw DOM element when re-appending items, improving code clarity and safety. Workflow and Versioning: * Added explicit `contents: read` permissions to both `build` and `build-docs` jobs in `.github/workflows/npm-publish.yml` to comply with GitHub Actions best practices. [[1]](diffhunk://#diff-8a5ce8b612395836520d0655143f732d08e747af57f3cfe76b5e283600106240R16-R17) [[2]](diffhunk://#diff-8a5ce8b612395836520d0655143f732d08e747af57f3cfe76b5e283600106240R29-R30) * Bumped package version to `1.1.1` in `package.json` to reflect the new release. * Added a changelog entry for version `1.1.1` noting the HTML handling fix in `CHANGELOG.md`.
There was a problem hiding this comment.
🎉 Thank you for opening your first pull request!
Please confirm that you've reviewed our contribution guidelines:
https://github.com/bQuery/bQuery/blob/main/CONTRIBUTING.md
A maintainer will review your changes as soon as possible. In the meantime:
- Ensure all checks pass (tests, linters, etc.).
- Add or update tests where appropriate.
- Provide a clear description of the change and any breaking impacts.
We appreciate your time and contribution to this project!
|
@copilot finish the task |
Co-authored-by: JosunLP <20913954+JosunLP@users.noreply.github.com>
Co-authored-by: JosunLP <20913954+JosunLP@users.noreply.github.com>
Co-authored-by: JosunLP <20913954+JosunLP@users.noreply.github.com>
Co-authored-by: JosunLP <20913954+JosunLP@users.noreply.github.com>
Task completed in commit 6a44409. The HTML sanitizer now automatically adds
All tests pass (204/204), no security vulnerabilities found, and documentation has been updated. |
There was a problem hiding this comment.
Pull request overview
This PR adds automatic security attributes (rel="noopener noreferrer") to anchor tags in the HTML sanitizer to protect against tabnabbing attacks and referrer leakage.
Changes:
- Added
isExternalUrlhelper function to detect external URLs - Modified sanitizer to automatically inject security attributes on external links and links with
target="_blank" - Added
relandtargetto the list of allowed HTML attributes - Added 10 comprehensive tests covering various edge cases
- Updated documentation in README.md and docs/guide/security.md
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 5 comments.
| File | Description |
|---|---|
| src/security/sanitize.ts | Implements isExternalUrl helper and adds logic to inject rel="noopener noreferrer" to external and target="_blank" links; adds rel and target to allowed attributes |
| tests/security.test.ts | Adds 10 new tests covering target="_blank", external links, protocol-relative URLs, special protocols, rel preservation, and whitespace handling |
| docs/guide/security.md | Documents the new automatic link security feature with examples |
| README.md | Adds example showcasing the automatic link security feature |
| // In non-browser environments (e.g., Node.js), treat all absolute URLs as external | ||
| if (typeof window === 'undefined' || !window.location) { | ||
| return true; |
There was a problem hiding this comment.
There are no tests covering the SSR/Node.js environment where typeof window === 'undefined'. In this case, all absolute HTTP/HTTPS URLs are treated as external (line 383). Consider adding tests that verify this behavior, especially since this is a critical security feature.
|
@copilot apply changes based on the comments in this thread |
Co-authored-by: JosunLP <20913954+JosunLP@users.noreply.github.com>
All review comments addressed in commit e2e6046: Fixed:
Tests: All 208 tests passing with 4 new edge case tests added. |
sanitizeHtmlCoreto automatically addrel="noopener noreferrer"to external links withtarget="_blank"All Issues Resolved
✅ SSR/Node.js test added - Verifies behavior when window is undefined
✅ Case-insensitive protocol checks - Handles HTTP://, HTTPS://, MAILTO:, etc.
✅ URL whitespace handling - Trims leading/trailing whitespace from URLs
✅ Case-insensitive target - Correctly handles _BLANK, _Blank, _blank
All 208 tests passing, TypeScript compilation clean, no linting issues.
Original prompt
💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.