Skip to content

Commit

Permalink
Remove image HTML elements from non whitelisted sources in Obsidian chat
Browse files Browse the repository at this point in the history
Given img src enforcement via CSP required loosening. Soft enforce it
via a regex replace of img HTML elements if the src isn't from the
whitelisted set of source prefixes.

Currently allowed source prefixes are
- app: for local images
- data: for inline generated images
- https://generated.khoj.dev: for cloud generated images
  • Loading branch information
debanjum committed Jun 18, 2024
1 parent c7d825b commit 86a3505
Showing 1 changed file with 6 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/interface/obsidian/src/chat_view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,12 @@ export class KhojChatView extends KhojPaneView {
// @ts-ignore
MarkdownRenderer.renderMarkdown(markdownText, virtualChatMessageBodyTextEl, '', null);

// Remove image HTML elements with any non whitelisted src prefix
virtualChatMessageBodyTextEl.innerHTML = virtualChatMessageBodyTextEl.innerHTML.replace(
/<img(?:(?!src=["'](app:|data:|https:\/\/generated\.khoj\.dev)).)*?>/gis,
''
);

// Sanitize the markdown text rendered as HTML
return DOMPurify.sanitize(virtualChatMessageBodyTextEl.innerHTML);
}
Expand Down

0 comments on commit 86a3505

Please sign in to comment.