Skip to content

armgit / moli

Public Imported from GitHub
0

Best headless browser for AI agents. Lite, Fast, High-Compatibility. Built in Rust

4.0 KB
Raw Blame
<!DOCTYPE html>
<html>
<head>
<title>Permission policy enforcement</title>
<link rel="author" href="mailto:[email protected]">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/common/get-host-info.sub.js"></script>
</head>
<body>
<script>
const host_info = get_host_info();

promise_test(async t => {
  const iframe = document.createElement('iframe');

  const origin_b = host_info.HTTPS_REMOTE_ORIGIN;
  const origin_c = host_info.HTTPS_OTHER_NOTSAMESITE_ORIGIN;

  iframe.src = origin_b + '/webmcp/imperative/resources/iframe-register-tool.html';
  iframe.allow = `tools ${origin_b}`;

  const load_promise = new Promise(resolve => iframe.onload = resolve);
  document.body.appendChild(iframe);
  t.add_cleanup(() => iframe.remove());
  await load_promise;

  const toolchange_promise = new Promise((resolve) => {
    document.modelContext.addEventListener('toolchange', resolve, {once: true});
  });

  iframe.contentWindow.postMessage({
    action: 'register',
    tool: { name: 'iframe_tool', description: 'Origin B tool' },
    options: { exposedTo: [host_info.HTTPS_ORIGIN] }
  }, '*');
  await toolchange_promise;

  const navigate_promise = new Promise(resolve => iframe.onload = resolve);
  iframe.src = origin_c + '/webmcp/imperative/resources/iframe-register-tool.html';
  await navigate_promise;

  const failure_promise = new Promise(resolve => {
    window.addEventListener('message', e => {
      if (e.data.startsWith('tool registration failed:')) {
        resolve(e.data);
      }
    }, {once: true});
  });

  iframe.contentWindow.postMessage({
    action: 'register',
    tool: { name: 'iframe_tool', description: 'Origin C tool' }
  }, '*');
  const failure_msg = await failure_promise;

  assert_true(failure_msg.includes('NotAllowedError'), `Expected NotAllowedError in navigated iframe, got: ${failure_msg}`);
}, 'registerTool() rejects in iframe, after navigation to an origin not covered by permissions policy');

promise_test(async t => {
  const iframe = document.createElement('iframe');
  const origin_b = host_info.HTTPS_REMOTE_ORIGIN;

  iframe.src = origin_b + '/webmcp/imperative/resources/iframe-register-tool.html';
  // No `allow` attribute.

  const load_promise = new Promise(resolve => iframe.onload = resolve);
  document.body.appendChild(iframe);
  t.add_cleanup(() => iframe.remove());
  await load_promise;

  const failure_promise = new Promise(resolve => {
    window.addEventListener('message', e => {
      if (typeof e.data === 'string' && e.data.startsWith('getTools promise rejected:')) {
        resolve(e.data);
      }
    }, {once: true});
  });

  iframe.contentWindow.postMessage('getTools', '*');
  const failure_msg = await failure_promise;

  assert_true(failure_msg.includes('NotAllowedError'), `Expected NotAllowedError in iframe, got: ${failure_msg}`);
}, 'getTools() throws NotAllowedError in iframe when permissions policy is disabled');

promise_test(async t => {
  const iframe = document.createElement('iframe');
  const origin_b = host_info.HTTPS_REMOTE_ORIGIN;

  iframe.src = origin_b + '/webmcp/imperative/resources/iframe-register-tool.html';
  // No `allow` attribute.

  const load_promise = new Promise(resolve => iframe.onload = resolve);
  document.body.appendChild(iframe);
  t.add_cleanup(() => iframe.remove());
  await load_promise;

  const failure_promise = new Promise(resolve => {
    window.addEventListener('message', e => {
      if (e.data.action === 'executeFakeToolResponse') {
        resolve(e.data);
      }
    }, {once: true});
  });

  // Direct the iframe to construct a conceivable `RegisteredTool` and call
  // `executeTool()` on it.
  iframe.contentWindow.postMessage({ action: 'execute_fake_tool', name: 'dummy_tool' }, '*');
  const response = await failure_promise;

  assert_equals(response.error_name, 'NotAllowedError', 'Should reject with NotAllowedError');
}, 'executeTool() throws NotAllowedError in iframe when permissions policy is disabled');
</script>
</body>
</html>