mcp-image-resources.test.mjs
sha256:b5f647cb9c409f563d4671fe3fc05ddea01fabfed9b41fc11cb923588e1c1baf
mirror: GitHub Phase A durable MCP OAuth (#270)
Human
minor
⚠ breaking
11 days ago
| 1 | /** |
| 2 | * Tests for Phase 18A: MCP Image Resources — image-fetch.mjs security + resource registration. |
| 3 | */ |
| 4 | import { describe, it, mock, beforeEach, afterEach } from 'node:test'; |
| 5 | import assert from 'node:assert'; |
| 6 | |
| 7 | describe('fetchImageAsBase64', () => { |
| 8 | it('rejects non-https URLs', async () => { |
| 9 | const { fetchImageAsBase64 } = await import('../mcp/resources/image-fetch.mjs'); |
| 10 | await assert.rejects( |
| 11 | () => fetchImageAsBase64('http://example.com/img.png'), |
| 12 | /https:\/\//, |
| 13 | ); |
| 14 | }); |
| 15 | |
| 16 | it('rejects data: URIs', async () => { |
| 17 | const { fetchImageAsBase64 } = await import('../mcp/resources/image-fetch.mjs'); |
| 18 | await assert.rejects( |
| 19 | () => fetchImageAsBase64('data:image/png;base64,abc'), |
| 20 | /https:\/\//, |
| 21 | ); |
| 22 | }); |
| 23 | |
| 24 | it('rejects javascript: URIs', async () => { |
| 25 | const { fetchImageAsBase64 } = await import('../mcp/resources/image-fetch.mjs'); |
| 26 | await assert.rejects( |
| 27 | () => fetchImageAsBase64('javascript:alert(1)'), |
| 28 | /https:\/\//, |
| 29 | ); |
| 30 | }); |
| 31 | |
| 32 | it('rejects file: URIs', async () => { |
| 33 | const { fetchImageAsBase64 } = await import('../mcp/resources/image-fetch.mjs'); |
| 34 | await assert.rejects( |
| 35 | () => fetchImageAsBase64('file:///etc/passwd'), |
| 36 | /https:\/\//, |
| 37 | ); |
| 38 | }); |
| 39 | |
| 40 | it('rejects localhost', async () => { |
| 41 | const { fetchImageAsBase64 } = await import('../mcp/resources/image-fetch.mjs'); |
| 42 | await assert.rejects( |
| 43 | () => fetchImageAsBase64('https://localhost/img.png'), |
| 44 | /localhost.*blocked/i, |
| 45 | ); |
| 46 | }); |
| 47 | |
| 48 | it('rejects URLs with empty string', async () => { |
| 49 | const { fetchImageAsBase64 } = await import('../mcp/resources/image-fetch.mjs'); |
| 50 | await assert.rejects( |
| 51 | () => fetchImageAsBase64(''), |
| 52 | /https:\/\//, |
| 53 | ); |
| 54 | }); |
| 55 | |
| 56 | it('rejects null/undefined', async () => { |
| 57 | const { fetchImageAsBase64 } = await import('../mcp/resources/image-fetch.mjs'); |
| 58 | await assert.rejects(() => fetchImageAsBase64(null)); |
| 59 | await assert.rejects(() => fetchImageAsBase64(undefined)); |
| 60 | }); |
| 61 | }); |
| 62 | |
| 63 | describe('extractImageUrls used in resource context', () => { |
| 64 | it('extracts correct structure for resource listing', async () => { |
| 65 | const { extractImageUrls } = await import('../lib/media-url-extract.mjs'); |
| 66 | const body = '\nSome text\n'; |
| 67 | const images = extractImageUrls(body); |
| 68 | assert.strictEqual(images.length, 2); |
| 69 | assert.strictEqual(images[0].alt, 'Screenshot'); |
| 70 | assert.strictEqual(images[0].mimeType, 'image/png'); |
| 71 | assert.strictEqual(images[1].alt, 'Logo'); |
| 72 | assert.strictEqual(images[1].mimeType, 'image/jpeg'); |
| 73 | |
| 74 | for (const img of images) { |
| 75 | assert.ok(img.url.startsWith('https://')); |
| 76 | assert.ok(typeof img.mimeType === 'string'); |
| 77 | assert.ok(typeof img.alt === 'string'); |
| 78 | } |
| 79 | }); |
| 80 | |
| 81 | it('resource URI format is correct', () => { |
| 82 | const notePath = 'inbox/my-note.md'; |
| 83 | const index = 0; |
| 84 | const uri = `knowtation://vault/${notePath}/image/${index}`; |
| 85 | assert.strictEqual(uri, 'knowtation://vault/inbox/my-note.md/image/0'); |
| 86 | }); |
| 87 | |
| 88 | it('index out of range is detectable', async () => { |
| 89 | const { extractImageUrls } = await import('../lib/media-url-extract.mjs'); |
| 90 | const body = ''; |
| 91 | const images = extractImageUrls(body); |
| 92 | assert.strictEqual(images.length, 1); |
| 93 | const oobIndex = 5; |
| 94 | assert.ok(oobIndex >= images.length, 'Index should be out of range'); |
| 95 | }); |
| 96 | }); |
| 97 | |
| 98 | describe('SSRF protection constants', () => { |
| 99 | it('private IP patterns reject common ranges', async () => { |
| 100 | const mod = await import('../mcp/resources/image-fetch.mjs'); |
| 101 | assert.ok(mod.DEFAULT_MAX_BYTES > 0); |
| 102 | assert.ok(mod.DEFAULT_TIMEOUT_MS > 0); |
| 103 | }); |
| 104 | }); |
File History
4 commits
sha256:b5f647cb9c409f563d4671fe3fc05ddea01fabfed9b41fc11cb923588e1c1baf
mirror: GitHub Phase A durable MCP OAuth (#270)
Human
minor
⚠
11 days ago
sha256:d8c648b20a4d53b2673c5c082ee7edfa7b2fc9b11080832da1f38807b6bf940b
fix(7C-L1b): route hosted delegation proposals through cani…
Human
minor
⚠
31 days ago
sha256:2827ba9e7632a4b141c50caf1e8f7d77abbc3515be20e7465f2bccb0ac4edf91
fix: repair endpoint now sets has_active_subscription when …
Human
minor
⚠
51 days ago
sha256:6a102aafafdfe7e70a24f4e59740200f0ee713ce7915f1b53e9d4ba5ee8b4410
Initial Muse snapshot
Human
83 days ago