Skip to content

Commit

Permalink
Add comment and test for trusted domains
Browse files Browse the repository at this point in the history
  • Loading branch information
roblourens committed May 22, 2024
1 parent 2ab53e7 commit 4c82e07
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/vs/workbench/contrib/chat/browser/chatMarkdownRenderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ export class ChatMarkdownRenderer extends MarkdownRenderer {
const mdWithBody: IMarkdownString | undefined = (markdown && markdown.supportHtml) ?
{
...markdown,

// dompurify uses DOMParser, which strips leading comments. Wrapping it all in 'body' prevents this.
value: `<body>${markdown.value}</body>`,
}
: markdown;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<div class="rendered-markdown"><img src="http://allowed.com/image.jpg"> &lt;div&gt;&lt;img src="http://disallowed.com/image.jpg"&gt;&lt;/div&gt;</div>
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import { MarkdownString } from 'vs/base/common/htmlContent';
import { assertSnapshot } from 'vs/base/test/common/snapshot';
import { ensureNoDisposablesAreLeakedInTestSuite } from 'vs/base/test/common/utils';
import { ChatMarkdownRenderer } from 'vs/workbench/contrib/chat/browser/chatMarkdownRenderer';
import { ITrustedDomainService } from 'vs/workbench/contrib/url/browser/trustedDomainService';
import { MockTrustedDomainService } from 'vs/workbench/contrib/url/test/browser/mockTrustedDomainService';
import { workbenchInstantiationService } from 'vs/workbench/test/browser/workbenchTestServices';

suite('ChatMarkdownRenderer', () => {
Expand All @@ -15,6 +17,7 @@ suite('ChatMarkdownRenderer', () => {
let testRenderer: ChatMarkdownRenderer;
setup(() => {
const instantiationService = store.add(workbenchInstantiationService(undefined, store));
instantiationService.stub(ITrustedDomainService, new MockTrustedDomainService(['http://allowed.com']));
testRenderer = instantiationService.createInstance(ChatMarkdownRenderer, {});
});

Expand Down Expand Up @@ -86,4 +89,11 @@ suite('ChatMarkdownRenderer', () => {
const result = store.add(testRenderer.render(md));
await assertSnapshot(result.element.outerHTML);
});

test('remote images', async () => {
const md = new MarkdownString('<img src="http://allowed.com/image.jpg"> <img src="http://disallowed.com/image.jpg">');
md.supportHtml = true;
const result = store.add(testRenderer.render(md));
await assertSnapshot(result.element.outerHTML);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/

import { URI } from 'vs/base/common/uri';
import { isURLDomainTrusted, ITrustedDomainService } from 'vs/workbench/contrib/url/browser/trustedDomainService';

export class MockTrustedDomainService implements ITrustedDomainService {
_serviceBrand: undefined;

constructor(private readonly _trustedDomains: string[] = []) {
}

isValid(resource: URI): boolean {
return isURLDomainTrusted(resource, this._trustedDomains);
}
}

0 comments on commit 4c82e07

Please sign in to comment.