|
| 1 | +/** |
| 2 | + * Copyright (c) Microsoft Corporation. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +import { Client } from '@modelcontextprotocol/sdk/client/index.js'; |
| 18 | +import { test, expect } from './fixtures'; |
| 19 | + |
| 20 | +const BLOCK_MESSAGE = /Blocked by Web Inspector|NS_ERROR_FAILURE|net::ERR_BLOCKED_BY_CLIENT/g; |
| 21 | + |
| 22 | +const fetchPage = async (client: Client, url: string) => { |
| 23 | + const result = await client.callTool({ |
| 24 | + name: 'browser_navigate', |
| 25 | + arguments: { |
| 26 | + url, |
| 27 | + }, |
| 28 | + }); |
| 29 | + |
| 30 | + return JSON.stringify(result, null, 2); |
| 31 | +}; |
| 32 | + |
| 33 | +test('default to allow all', async ({ server, client }) => { |
| 34 | + server.setContent('/ppp', 'content:PPP', 'text/html'); |
| 35 | + const result = await fetchPage(client, server.PREFIX + '/ppp'); |
| 36 | + expect(result).toContain('content:PPP'); |
| 37 | +}); |
| 38 | + |
| 39 | +test('blocked works (hostname)', async ({ startClient }) => { |
| 40 | + const { client } = await startClient({ |
| 41 | + args: ['--blocked-origins', 'microsoft.com;example.com;playwright.dev'] |
| 42 | + }); |
| 43 | + const result = await fetchPage(client, 'https://example.com/'); |
| 44 | + expect(result).toMatch(BLOCK_MESSAGE); |
| 45 | +}); |
| 46 | + |
| 47 | +test('blocked works (origin)', async ({ startClient }) => { |
| 48 | + const { client } = await startClient({ |
| 49 | + args: ['--blocked-origins', 'https://microsoft.com;https://example.com;https://playwright.dev'] |
| 50 | + }); |
| 51 | + const result = await fetchPage(client, 'https://example.com/'); |
| 52 | + expect(result).toMatch(BLOCK_MESSAGE); |
| 53 | +}); |
| 54 | + |
| 55 | +test('allowed works (hostname)', async ({ server, startClient }) => { |
| 56 | + server.setContent('/ppp', 'content:PPP', 'text/html'); |
| 57 | + const { client } = await startClient({ |
| 58 | + args: ['--allowed-origins', `microsoft.com;${new URL(server.PREFIX).host};playwright.dev`] |
| 59 | + }); |
| 60 | + const result = await fetchPage(client, server.PREFIX + '/ppp'); |
| 61 | + expect(result).toContain('content:PPP'); |
| 62 | +}); |
| 63 | + |
| 64 | +test('allowed works (origin)', async ({ server, startClient }) => { |
| 65 | + server.setContent('/ppp', 'content:PPP', 'text/html'); |
| 66 | + const { client } = await startClient({ |
| 67 | + args: ['--allowed-origins', `https://microsoft.com;${new URL(server.PREFIX).origin};https://playwright.dev`] |
| 68 | + }); |
| 69 | + const result = await fetchPage(client, server.PREFIX + '/ppp'); |
| 70 | + expect(result).toContain('content:PPP'); |
| 71 | +}); |
| 72 | + |
| 73 | +test('blocked takes precedence (hostname)', async ({ startClient }) => { |
| 74 | + const { client } = await startClient({ |
| 75 | + args: [ |
| 76 | + '--blocked-origins', 'example.com', |
| 77 | + '--allowed-origins', 'example.com', |
| 78 | + ], |
| 79 | + }); |
| 80 | + const result = await fetchPage(client, 'https://example.com/'); |
| 81 | + expect(result).toMatch(BLOCK_MESSAGE); |
| 82 | +}); |
| 83 | + |
| 84 | +test('blocked takes precedence (origin)', async ({ startClient }) => { |
| 85 | + const { client } = await startClient({ |
| 86 | + args: [ |
| 87 | + '--blocked-origins', 'https://example.com', |
| 88 | + '--allowed-origins', 'https://example.com', |
| 89 | + ], |
| 90 | + }); |
| 91 | + const result = await fetchPage(client, 'https://example.com/'); |
| 92 | + expect(result).toMatch(BLOCK_MESSAGE); |
| 93 | +}); |
| 94 | + |
| 95 | +test('allowed without blocked blocks all non-explicitly specified origins (hostname)', async ({ startClient }) => { |
| 96 | + const { client } = await startClient({ |
| 97 | + args: ['--allowed-origins', 'playwright.dev'], |
| 98 | + }); |
| 99 | + const result = await fetchPage(client, 'https://example.com/'); |
| 100 | + expect(result).toMatch(BLOCK_MESSAGE); |
| 101 | +}); |
| 102 | + |
| 103 | +test('allowed without blocked blocks all non-explicitly specified origins (origin)', async ({ startClient }) => { |
| 104 | + const { client } = await startClient({ |
| 105 | + args: ['--allowed-origins', 'https://playwright.dev'], |
| 106 | + }); |
| 107 | + const result = await fetchPage(client, 'https://example.com/'); |
| 108 | + expect(result).toMatch(BLOCK_MESSAGE); |
| 109 | +}); |
| 110 | + |
| 111 | +test('blocked without allowed allows non-explicitly specified origins (hostname)', async ({ server, startClient }) => { |
| 112 | + server.setContent('/ppp', 'content:PPP', 'text/html'); |
| 113 | + const { client } = await startClient({ |
| 114 | + args: ['--blocked-origins', 'example.com'], |
| 115 | + }); |
| 116 | + const result = await fetchPage(client, server.PREFIX + '/ppp'); |
| 117 | + expect(result).toContain('content:PPP'); |
| 118 | +}); |
| 119 | + |
| 120 | +test('blocked without allowed allows non-explicitly specified origins (origin)', async ({ server, startClient }) => { |
| 121 | + server.setContent('/ppp', 'content:PPP', 'text/html'); |
| 122 | + const { client } = await startClient({ |
| 123 | + args: ['--blocked-origins', 'https://example.com'], |
| 124 | + }); |
| 125 | + const result = await fetchPage(client, server.PREFIX + '/ppp'); |
| 126 | + expect(result).toContain('content:PPP'); |
| 127 | +}); |
0 commit comments