// *****************************************************************************
// Copyright (C) 2026 EclipseSource GmbH.
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License v. 2.0 which is available at
// http://www.eclipse.org/legal/epl-2.0.
//
// This Source Code may also be made available under the following Secondary
// Licenses when the conditions for such availability set forth in the Eclipse
// Public License v. 2.0 are satisfied: GNU General Public License, version 2
// with the GNU Classpath Exception which is available at
// https://www.gnu.org/software/classpath/license.html.
//
// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0
// *****************************************************************************
import { enableJSDOM } from '@theia/core/lib/browser/test/jsdom';
let disableJSDOM = enableJSDOM();
import { expect } from 'chai';
import { blockExternalResources, BLOCKED_RESOURCE_CLASS, restoreBlockedResource, setAllowedResourceUrls } from './block-external-resources.js';
disableJSDOM();
describe('blockExternalResources', () => {
before(() => disableJSDOM = enableJSDOM());
after(() => disableJSDOM());
afterEach(() => setAllowedResourceUrls([]));
const createRoot = (html: string): HTMLElement => {
const root = document.createElement('div');
root.innerHTML = html;
blockExternalResources(root);
return root;
};
it('replaces an external image with a placeholder that shows the URL', () => {
const root = createRoot('
');
const placeholder = root.querySelector(`.${BLOCKED_RESOURCE_CLASS}`) as HTMLElement;
expect(placeholder).to.exist;
expect(root.querySelector('img')).to.be.null;
expect(placeholder.textContent).to.contain('https://evil.com/x.gif');
});
it('preserves data images', () => {
const root = createRoot('
');
expect(root.querySelector(`.${BLOCKED_RESOURCE_CLASS}`)).to.be.null;
expect(root.querySelector('img')?.getAttribute('src')).to.equal('data:image/png;base64,AAA');
});
it('preserves images without a source', () => {
const root = createRoot('![]()
');
expect(root.querySelector(`.${BLOCKED_RESOURCE_CLASS}`)).to.be.null;
expect(root.querySelectorAll('img')).to.have.length(2);
});
it('blocks external image srcset entries', () => {
const root = createRoot('
');
expect(root.querySelector(`.${BLOCKED_RESOURCE_CLASS}`)).to.exist;
expect(root.querySelector('img')).to.be.null;
});
it('lists all blocked srcset entries', () => {
const root = createRoot('
');
const placeholder = root.querySelector(`.${BLOCKED_RESOURCE_CLASS}`) as HTMLElement;
expect(placeholder).to.exist;
expect(placeholder.textContent).to.contain('https://a.com/x');
expect(placeholder.textContent).to.contain('https://b.com/y');
expect(placeholder.textContent).to.contain('https://c.com/z');
expect(placeholder.textContent).to.contain('3 resources will be enabled');
});
it('preserves data image srcset entries', () => {
const root = createRoot('
');
expect(root.querySelector(`.${BLOCKED_RESOURCE_CLASS}`)).to.be.null;
expect(root.querySelector('img')?.getAttribute('srcset')).to.equal('data:image/png;base64,AAA 1x');
});
it('lists only external srcset entries when mixed with data entries', () => {
const root = createRoot('
');
const placeholder = root.querySelector(`.${BLOCKED_RESOURCE_CLASS}`) as HTMLElement;
expect(placeholder).to.exist;
expect(placeholder.textContent).to.contain('https://evil.com/x.png');
expect(placeholder.textContent).not.to.contain('data:image/png;base64,AAA');
});
it('blocks external picture sources while preserving safe fallback images', () => {
const root = createRoot('
');
expect(root.querySelector(`.${BLOCKED_RESOURCE_CLASS}`)).to.exist;
expect(root.querySelector('source')).to.be.null;
expect(root.querySelector('img')?.getAttribute('src')).to.equal('data:image/png;base64,AAA');
});
it('blocks external video resources', () => {
const root = createRoot('');
expect(root.querySelector(`.${BLOCKED_RESOURCE_CLASS}`)).to.exist;
expect(root.querySelector('video')).to.be.null;
});
it('blocks external audio resources', () => {
const root = createRoot('');
expect(root.querySelector(`.${BLOCKED_RESOURCE_CLASS}`)).to.exist;
expect(root.querySelector('audio')).to.be.null;
});
it('blocks SVG image resources', () => {
const root = createRoot('');
const placeholder = root.querySelector(`.${BLOCKED_RESOURCE_CLASS}`) as HTMLElement;
expect(placeholder).to.exist;
expect(placeholder.textContent).to.contain('https://evil.com/x.png');
expect(root.querySelector('image')).to.be.null;
});
it('blocks SVG use resources', () => {
const root = createRoot('');
const placeholder = root.querySelector(`.${BLOCKED_RESOURCE_CLASS}`) as HTMLElement;
expect(placeholder).to.exist;
expect(placeholder.textContent).to.contain('https://evil.com/icons.svg#icon');
expect(root.querySelector('use')).to.be.null;
});
it('blocks SVG filter image resources', () => {
const root = createRoot('');
const placeholder = root.querySelector(`.${BLOCKED_RESOURCE_CLASS}`) as HTMLElement;
expect(placeholder).to.exist;
expect(placeholder.textContent).to.contain('https://evil.com/filter.png');
expect(root.querySelector('feImage')).to.be.null;
});
it('blocks SVG filter image resources using xlink:href', () => {
const root = createRoot('');
const placeholder = root.querySelector(`.${BLOCKED_RESOURCE_CLASS}`) as HTMLElement;
expect(placeholder).to.exist;
expect(placeholder.textContent).to.contain('https://evil.com/filter.png');
expect(root.querySelector('feImage')).to.be.null;
});
it('preserves data SVG filter image resources', () => {
const root = createRoot('');
expect(root.querySelector(`.${BLOCKED_RESOURCE_CLASS}`)).to.be.null;
expect(root.querySelector('feImage')?.getAttribute('href')).to.equal('data:image/png;base64,AAA');
});
it('blocks object and embed resources', () => {
const root = createRoot('