-
Notifications
You must be signed in to change notification settings - Fork 10
Fix/sanitize img src exfiltration #54
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
Merged
whhe
merged 4 commits into
oceanbase:main
from
Zhangg7723:fix/sanitize-img-src-exfiltration
Feb 25, 2026
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
76d0a3e
fix(security): prevent data exfiltration via img src in chat output
Zhangg7723 edda100
chore: ignore web/.env for local debugging config
Zhangg7723 a9815aa
Use relative path so frontend treats it as same-origin
Zhangg7723 e83f36f
solve copilot reviews
Zhangg7723 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
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
This file was deleted.
Oops, something went wrong.
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 |
|---|---|---|
| @@ -1,4 +1,5 @@ | ||
| /node_modules | ||
| /.env | ||
| /.env.local | ||
| /.umirc.local.ts | ||
| /config/config.local.ts | ||
|
|
||
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
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
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
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
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
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
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
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
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
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 |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| /** | ||
| * Secure sanitization for chat/AI-generated content. | ||
| * Prevents data exfiltration via img src (e.g. prompt injection that embeds | ||
| * conversation history in external image URLs). | ||
| */ | ||
| import DOMPurify, { Config } from 'dompurify'; | ||
|
|
||
| // Single / only (not //), ./ , ../ , or data:image/ | ||
| const ALLOWED_IMAGE_URL_PATTERN = /^(?:\/(?!\/)|data:image\/|\.\/|\.\.\/)/i; | ||
|
|
||
| function isAllowedImageUrl(url: string): boolean { | ||
| if (!url || typeof url !== 'string') return false; | ||
| const trimmed = url.trim(); | ||
| if (!trimmed) return false; | ||
| // Block protocol-relative URLs (//evil.com) and backslash variants (\\...) | ||
| if (trimmed.startsWith('//') || trimmed.startsWith('\\\\')) return false; | ||
| // Allow relative URLs: /path (single slash), ./path, ../path | ||
| if (ALLOWED_IMAGE_URL_PATTERN.test(trimmed)) return true; | ||
| // Allow data:image/* for inline images (no network request) | ||
| if (trimmed.toLowerCase().startsWith('data:image/')) return true; | ||
| // Allow same-origin only | ||
| if (typeof window !== 'undefined') { | ||
| try { | ||
| const parsed = new URL(trimmed, window.location.origin); | ||
| return parsed.origin === window.location.origin; | ||
| } catch { | ||
| return false; | ||
| } | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| /** Parse srcset attribute into list of URLs (descriptors like 1x, 100w are stripped). */ | ||
| function getUrlsFromSrcset(srcset: string): string[] { | ||
| return srcset | ||
| .split(',') | ||
| .map((s) => s.trim().split(/\s+/)[0]) | ||
| .filter(Boolean); | ||
| } | ||
|
|
||
| function isSrcsetAllowed(srcset: string): boolean { | ||
| const urls = getUrlsFromSrcset(srcset); | ||
| return urls.length > 0 && urls.every((url) => isAllowedImageUrl(url)); | ||
| } | ||
|
|
||
| function sanitizeImageUrlAttributes(node: Element): void { | ||
| const tag = node.tagName; | ||
| if (tag !== 'IMG' && tag !== 'SOURCE') return; | ||
|
|
||
| const src = node.getAttribute('src'); | ||
| if (src && !isAllowedImageUrl(src)) { | ||
| node.removeAttribute('src'); | ||
| } | ||
|
|
||
| const srcset = node.getAttribute('srcset'); | ||
| if (srcset && !isSrcsetAllowed(srcset)) { | ||
| node.removeAttribute('srcset'); | ||
| } | ||
| } | ||
|
|
||
| let secureImageHookAdded = false; | ||
|
|
||
| function ensureSecureImageHook(): void { | ||
| if (secureImageHookAdded) return; | ||
| DOMPurify.addHook('afterSanitizeAttributes', (node) => { | ||
| if (node.nodeType === 1) { | ||
| sanitizeImageUrlAttributes(node as Element); | ||
| } | ||
| }); | ||
| secureImageHookAdded = true; | ||
| } | ||
|
|
||
| /** | ||
| * Sanitize content for safe rendering. Restricts img/srcset (and source src/srcset) | ||
| * to same-origin, relative, or data: URLs only. Use for all chat/AI output and user content. | ||
| */ | ||
| export function sanitizeChatContent(dirty: string, config?: Config): string { | ||
| ensureSecureImageHook(); | ||
| const result = DOMPurify.sanitize(dirty, { | ||
| ADD_TAGS: ['think', 'section'], | ||
| ADD_ATTR: ['class'], | ||
| ...config, | ||
| }); | ||
| return typeof result === 'string' ? result : ''; | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.