Markdown-syntax and raw-HTML file: destinations are converted to an absolute local path before rendering or sanitization, then served by the same session media route as relative images. POSIX, Windows drive, localhost, UNC and percent-encoded URLs are covered; the file: scheme is never handed to the renderer or DOMPurify. Also prepares v0.31.1.
384 lines
18 KiB
TypeScript
384 lines
18 KiB
TypeScript
/**
|
|
* Markdown-image vocabulary spec: the local destination → absolute target
|
|
* resolution, the page-base media URL (browser AND desktop shell), and the
|
|
* `pathImages` capability the preview surfaces hand the host renderer. The
|
|
* desktop-shell case is the regression: an absolute http URL on the transport
|
|
* origin is cross-site and the Connection fence answers 403, so only a
|
|
* page-base `dsh-app://app/api/file` URL renders there (see markdown-images.ts).
|
|
*/
|
|
import { describe, expect, it } from 'vitest'
|
|
import {
|
|
createMarkdownPathImages,
|
|
desktopFileUrl,
|
|
fileUrlToLocalPath,
|
|
markdownImageTarget,
|
|
markdownMediaUrl,
|
|
resolveLocalMediaDest,
|
|
rewriteLocalImageUrls,
|
|
rewriteProseLocalImageUrls,
|
|
} from '../src/client/markdown-images.ts'
|
|
import { rewriteLocalMarkdownLinks } from '../src/client/markdown-navigation.ts'
|
|
import type { SessionScope } from '../src/client/api.ts'
|
|
|
|
const ORIGIN = 'http://127.0.0.1:3080'
|
|
/** The desktop shell's page origin; the Host itself listens elsewhere. */
|
|
const APP = 'dsh-app://app/'
|
|
const scope: SessionScope = { sessionId: 'abc', cwd: '/repo' }
|
|
|
|
describe('markdownImageTarget', () => {
|
|
it('resolves relative destinations against the opened file directory', () => {
|
|
expect(markdownImageTarget('./img.png', '/repo/docs/readme.md')).toBe('/repo/docs/img.png')
|
|
expect(markdownImageTarget('images/b.jpg', '/repo/docs/readme.md')).toBe('/repo/docs/images/b.jpg')
|
|
})
|
|
|
|
it('normalizes `.`/`..` segments', () => {
|
|
expect(markdownImageTarget('./img.png', '/repo/docs/sub/readme.md')).toBe('/repo/docs/sub/img.png')
|
|
expect(markdownImageTarget('../assets/b.png', '/repo/docs/sub/readme.md')).toBe('/repo/docs/assets/b.png')
|
|
})
|
|
|
|
it('passes absolute local paths through', () => {
|
|
expect(markdownImageTarget('/repo/assets/b.png', '/repo/docs/readme.md')).toBe('/repo/assets/b.png')
|
|
expect(markdownImageTarget('~/pics/a.png', '/repo/docs/readme.md')).toBe('~/pics/a.png')
|
|
})
|
|
|
|
it('handles windows absolute and UNC paths as local', () => {
|
|
expect(markdownImageTarget('C:\\repo\\img.png', 'C:\\repo\\docs\\readme.md')).toBe('C:\\repo\\img.png')
|
|
expect(markdownImageTarget('//server/share/a.png', '/repo/docs/readme.md')).toBeUndefined()
|
|
})
|
|
|
|
it('refuses everything the host renderer would not pass to the delegate', () => {
|
|
expect(markdownImageTarget('', '/repo/readme.md')).toBeUndefined()
|
|
expect(markdownImageTarget('#section', '/repo/readme.md')).toBeUndefined()
|
|
expect(markdownImageTarget('https://example.com/x.png', '/repo/readme.md')).toBeUndefined()
|
|
expect(markdownImageTarget('data:image/png;base64,AAA', '/repo/readme.md')).toBeUndefined()
|
|
expect(markdownImageTarget('./img.png?v=1', '/repo/readme.md')).toBeUndefined()
|
|
})
|
|
})
|
|
|
|
describe('fileUrlToLocalPath', () => {
|
|
it('turns POSIX, Windows and localhost file URLs into local paths', () => {
|
|
expect(fileUrlToLocalPath('file:///repo/docs/my%20image.png')).toBe('/repo/docs/my image.png')
|
|
expect(fileUrlToLocalPath('file://localhost/repo/docs/a.png')).toBe('/repo/docs/a.png')
|
|
expect(fileUrlToLocalPath('FILE:///C:/repo/docs/a.png')).toBe('C:\\repo\\docs\\a.png')
|
|
expect(fileUrlToLocalPath('file://C:/repo/docs/a.png')).toBe('C:\\repo\\docs\\a.png')
|
|
})
|
|
|
|
it('keeps a UNC file URL in the host resolver\'s forward-slash spelling', () => {
|
|
expect(fileUrlToLocalPath('file://server/share/a.png')).toBe('//server/share/a.png')
|
|
})
|
|
|
|
it('refuses malformed file URLs and every non-file scheme', () => {
|
|
expect(fileUrlToLocalPath('file:///bad%E0%A4%A.png')).toBeUndefined()
|
|
expect(fileUrlToLocalPath('file://')).toBeUndefined()
|
|
expect(fileUrlToLocalPath('https://example.com/x.png')).toBeUndefined()
|
|
expect(fileUrlToLocalPath('./x.png')).toBeUndefined()
|
|
})
|
|
})
|
|
|
|
describe('markdownMediaUrl', () => {
|
|
it('builds a same-origin media URL on the page base (browser)', () => {
|
|
expect(markdownMediaUrl('/repo/docs/img.png', scope, ORIGIN)).toBe(
|
|
'http://127.0.0.1:3080/sidebar/file?sessionId=abc&path=%2Frepo%2Fdocs%2Fimg.png&cwd=%2Frepo',
|
|
)
|
|
})
|
|
|
|
it('keeps a reverse-proxy directory prefix', () => {
|
|
expect(markdownMediaUrl('/repo/img.png', scope, 'https://host.example.test/proxy/3080/')).toBe(
|
|
'https://host.example.test/proxy/3080/sidebar/file?sessionId=abc&path=%2Frepo%2Fimg.png&cwd=%2Frepo',
|
|
)
|
|
})
|
|
|
|
it('stays on the desktop shell page origin instead of the transport origin', () => {
|
|
// Regression: resolving against `__DSH_TRANSPORT__.streamBaseUrl`
|
|
// (`http://127.0.0.1:<port>`) produced a cross-site request from this page
|
|
// and the Connection fence answered 403, so every local image fell back to
|
|
// its alt text.
|
|
expect(markdownMediaUrl('/repo/docs/img.png', scope, APP)).toBe(
|
|
'dsh-app://app/sidebar/file?sessionId=abc&path=%2Frepo%2Fdocs%2Fimg.png&cwd=%2Frepo',
|
|
)
|
|
})
|
|
|
|
it('drops the cwd param when the scope has no cwd', () => {
|
|
const url = markdownMediaUrl('/repo/img.png', { sessionId: 'abc' }, ORIGIN)
|
|
expect(url).toBe('http://127.0.0.1:3080/sidebar/file?sessionId=abc&path=%2Frepo%2Fimg.png')
|
|
expect(url).not.toContain('cwd=')
|
|
})
|
|
})
|
|
|
|
describe('desktopFileUrl', () => {
|
|
it('builds the host file URL on the dsh-app page origin', () => {
|
|
expect(desktopFileUrl('/repo/a.png', APP)).toBe('dsh-app://app/api/file?path=%2Frepo%2Fa.png')
|
|
})
|
|
|
|
it('keeps a reverse-proxy prefix under an http(s) page base', () => {
|
|
expect(desktopFileUrl('/repo/a.png', 'https://host.example.test/proxy/3080/')).toBe(
|
|
'https://host.example.test/proxy/3080/api/file?path=%2Frepo%2Fa.png',
|
|
)
|
|
})
|
|
})
|
|
|
|
describe('createMarkdownPathImages', () => {
|
|
it('maps local destinations to the plugin media route on the browser page base', () => {
|
|
const images = createMarkdownPathImages(scope, '/repo/docs/readme.md', ORIGIN)
|
|
expect(images.resolve('./img.png')).toBe(
|
|
'http://127.0.0.1:3080/sidebar/file?sessionId=abc&path=%2Frepo%2Fdocs%2Fimg.png&cwd=%2Frepo',
|
|
)
|
|
expect(images.resolve('../x.png')).toBe(
|
|
'http://127.0.0.1:3080/sidebar/file?sessionId=abc&path=%2Frepo%2Fx.png&cwd=%2Frepo',
|
|
)
|
|
})
|
|
|
|
it('maps local destinations to the authenticated host file route on the desktop page base', () => {
|
|
// The renderer's image vocabulary accepts exactly one local-file shape from
|
|
// the dsh-app:// page (`dsh-app://app/api/file?...`); the shell forwards it
|
|
// to the Host with the launch cookie.
|
|
const images = createMarkdownPathImages(scope, '/repo/docs/readme.md', APP)
|
|
expect(images.resolve('./img.png')).toBe(
|
|
'dsh-app://app/api/file?path=%2Frepo%2Fdocs%2Fimg.png',
|
|
)
|
|
expect(images.resolve('./img.png')).not.toContain('/sidebar/file')
|
|
})
|
|
|
|
it('makes a relative tab path absolute for the desktop host file route', () => {
|
|
// An editor tab opened from a chat address can carry a workspace-RELATIVE
|
|
// path (`openwrt-proxy-primer/PRINCIPLES.md`). The plugin media route
|
|
// resolves that together with `cwd`, but the host `api/file` route answers
|
|
// 400 "absolute path required" — so the desktop branch joins the session
|
|
// workspace root first.
|
|
const images = createMarkdownPathImages(scope, 'docs/readme.md', APP)
|
|
expect(images.resolve('./img.png')).toBe(
|
|
'dsh-app://app/api/file?path=%2Frepo%2Fdocs%2Fimg.png',
|
|
)
|
|
expect(images.resolve('images/b.png')).toBe(
|
|
'dsh-app://app/api/file?path=%2Frepo%2Fdocs%2Fimages%2Fb.png',
|
|
)
|
|
})
|
|
|
|
it('falls back without a workspace root when the tab path is relative (desktop)', () => {
|
|
const images = createMarkdownPathImages({ sessionId: 'abc' }, 'docs/readme.md', APP)
|
|
expect(images.resolve('./img.png')).toBeUndefined()
|
|
})
|
|
|
|
it('keeps the relative spelling with a cwd on the browser page base', () => {
|
|
// The plugin media route resolves a relative target against `cwd`, so the
|
|
// browser branch does not have to normalize it (the desktop host route
|
|
// does — see the case above).
|
|
const images = createMarkdownPathImages(scope, 'docs/readme.md', ORIGIN)
|
|
expect(images.resolve('./img.png')).toBe(
|
|
'http://127.0.0.1:3080/sidebar/file?sessionId=abc&path=docs%2Fimg.png&cwd=%2Frepo',
|
|
)
|
|
})
|
|
|
|
it('decodes one authored percent-escape before resolving', () => {
|
|
const images = createMarkdownPathImages(scope, '/repo/docs/readme.md', ORIGIN)
|
|
expect(images.resolve('./my%20image.png')).toBe(
|
|
'http://127.0.0.1:3080/sidebar/file?sessionId=abc&path=%2Frepo%2Fdocs%2Fmy+image.png&cwd=%2Frepo',
|
|
)
|
|
expect(images.resolve('./bad%E0%A4%A.png')).toBeUndefined()
|
|
})
|
|
|
|
it('passes an image data URI through on both page bases (chat/pasted screenshots)', () => {
|
|
const dataUrl = 'data:image/png;base64,iVBORw0KGgo='
|
|
for (const baseUrl of [ORIGIN, APP]) {
|
|
const images = createMarkdownPathImages(scope, '/repo/docs/readme.md', baseUrl)
|
|
expect(images.resolve(dataUrl)).toBe(dataUrl)
|
|
expect(images.resolve(` ${dataUrl} `)).toBe(dataUrl)
|
|
expect(images.resolve('DATA:IMAGE/PNG;BASE64,iVBORw0KGgo=')).toBe('DATA:IMAGE/PNG;BASE64,iVBORw0KGgo=')
|
|
}
|
|
})
|
|
|
|
it('still refuses non-image data payloads (only data:image is an image)', () => {
|
|
const images = createMarkdownPathImages(scope, '/repo/docs/readme.md', ORIGIN)
|
|
expect(images.resolve('data:text/html,<h1>x</h1>')).toBeUndefined()
|
|
expect(images.resolve('data:application/pdf;base64,AAA')).toBeUndefined()
|
|
})
|
|
|
|
it('maps an authored file: URL through the same media routes as a local path', () => {
|
|
const browser = createMarkdownPathImages(scope, '/repo/docs/readme.md', ORIGIN)
|
|
expect(browser.resolve('file:///repo/docs/img.png')).toBe(
|
|
'http://127.0.0.1:3080/sidebar/file?sessionId=abc&path=%2Frepo%2Fdocs%2Fimg.png&cwd=%2Frepo',
|
|
)
|
|
const desktop = createMarkdownPathImages(scope, '/repo/docs/readme.md', APP)
|
|
expect(desktop.resolve('file:///repo/docs/img.png')).toBe(
|
|
'dsh-app://app/api/file?path=%2Frepo%2Fdocs%2Fimg.png',
|
|
)
|
|
})
|
|
|
|
it('maps a Windows file: URL with the drive and backslashes intact', () => {
|
|
const images = createMarkdownPathImages(
|
|
{ sessionId: 'abc', cwd: 'C:\\repo' },
|
|
'C:\\repo\\t\\doc.md',
|
|
APP,
|
|
)
|
|
expect(images.resolve('file:///C:/pics/a%20b.png')).toBe(
|
|
'dsh-app://app/api/file?path=C%3A%5Cpics%5Ca+b.png',
|
|
)
|
|
})
|
|
|
|
it('maps a UNC file: URL through the media route', () => {
|
|
const images = createMarkdownPathImages(scope, '/repo/docs/readme.md', ORIGIN)
|
|
expect(images.resolve('file://server/share/a.png')).toBe(
|
|
'http://127.0.0.1:3080/sidebar/file?sessionId=abc&path=%2F%2Fserver%2Fshare%2Fa.png&cwd=%2Frepo',
|
|
)
|
|
})
|
|
|
|
it('maps a relative image from a Windows absolute tab path on the browser base', () => {
|
|
// The reporter's platform: an absolute Windows path joined with the image's
|
|
// relative destination, then percent-encoded for /sidebar/file.
|
|
const images = createMarkdownPathImages(
|
|
{ sessionId: 'abc', cwd: 'C:\\repo' },
|
|
'C:\\repo\\t\\doc.md',
|
|
ORIGIN,
|
|
)
|
|
expect(images.resolve('./pic.png')).toBe(
|
|
'http://127.0.0.1:3080/sidebar/file?sessionId=abc&path=C%3A%5Crepo%5Ct%5Cpic.png&cwd=C%3A%5Crepo',
|
|
)
|
|
})
|
|
|
|
it('answers undefined for destinations it cannot serve (alt-text fallback)', () => {
|
|
const images = createMarkdownPathImages(scope, '/repo/docs/readme.md', ORIGIN)
|
|
expect(images.resolve('https://example.com/x.png')).toBeUndefined()
|
|
expect(images.resolve('')).toBeUndefined()
|
|
expect(images.resolve('#anchor')).toBeUndefined()
|
|
})
|
|
})
|
|
|
|
describe('resolveLocalMediaDest (raw HTML leaves)', () => {
|
|
it('rewrites local sources on the page base', () => {
|
|
expect(resolveLocalMediaDest('./shot.png', scope, '/repo/docs/readme.md', ORIGIN)).toBe(
|
|
'http://127.0.0.1:3080/sidebar/file?sessionId=abc&path=%2Frepo%2Fdocs%2Fshot.png&cwd=%2Frepo',
|
|
)
|
|
})
|
|
|
|
it('rewrites local sources on the desktop shell page origin', () => {
|
|
// Raw HTML <img> is plain DOM, so the same-origin plugin media route works
|
|
// (the markdown-syntax vocabulary above needs the host file route instead).
|
|
expect(resolveLocalMediaDest('./shot.png', scope, '/repo/docs/readme.md', APP)).toBe(
|
|
'dsh-app://app/sidebar/file?sessionId=abc&path=%2Frepo%2Fdocs%2Fshot.png&cwd=%2Frepo',
|
|
)
|
|
})
|
|
|
|
it('rewrites a raw HTML file: URL through the media route', () => {
|
|
expect(resolveLocalMediaDest('file:///repo/docs/shot.png', scope, '/repo/docs/readme.md', ORIGIN)).toBe(
|
|
'http://127.0.0.1:3080/sidebar/file?sessionId=abc&path=%2Frepo%2Fdocs%2Fshot.png&cwd=%2Frepo',
|
|
)
|
|
})
|
|
|
|
it('decodes one percent-escape from the authored src', () => {
|
|
expect(resolveLocalMediaDest('./my%20shot.png', scope, '/repo/docs/readme.md', ORIGIN)).toBe(
|
|
'http://127.0.0.1:3080/sidebar/file?sessionId=abc&path=%2Frepo%2Fdocs%2Fmy+shot.png&cwd=%2Frepo',
|
|
)
|
|
})
|
|
|
|
it('leaves remote, anchor and empty sources untouched', () => {
|
|
expect(resolveLocalMediaDest('https://example.com/x.png', scope, '/repo/readme.md', ORIGIN))
|
|
.toBe('https://example.com/x.png')
|
|
expect(resolveLocalMediaDest('#anchor', scope, '/repo/readme.md', ORIGIN)).toBe('#anchor')
|
|
expect(resolveLocalMediaDest('', scope, '/repo/readme.md', ORIGIN)).toBe('')
|
|
})
|
|
})
|
|
|
|
describe('rewriteLocalImageUrls: CommonMark angle destinations and titles (N7)', () => {
|
|
const MEDIA = (path: string): string => `${ORIGIN}/sidebar/file?sessionId=abc&path=${encodeURIComponent(path)}&cwd=%2Frepo`
|
|
|
|
it('rewrites an angle-delimited destination, including its space', () => {
|
|
const out = rewriteLocalImageUrls('', scope, '/repo/readme.md', ORIGIN)
|
|
// The delimiters are preserved; the URL-encoded path carries the space.
|
|
expect(out).toBe(``)
|
|
})
|
|
|
|
it('preserves an optional double-quoted title while rewriting the destination', () => {
|
|
const out = rewriteLocalImageUrls('', scope, '/repo/readme.md', ORIGIN)
|
|
expect(out).toBe(`} "the title")`)
|
|
})
|
|
|
|
it('preserves a multi-line double-quoted title', () => {
|
|
const md = ''
|
|
const out = rewriteLocalImageUrls(md, scope, '/repo/readme.md', ORIGIN)
|
|
expect(out).toBe(`} "line one\nline two")`)
|
|
})
|
|
|
|
it('preserves a single-quoted title (it would otherwise not be rewritten at all)', () => {
|
|
const out = rewriteLocalImageUrls("", scope, '/repo/readme.md', ORIGIN)
|
|
expect(out).toBe(`} 'the title')`)
|
|
})
|
|
|
|
it('combines an angle destination with a title', () => {
|
|
const out = rewriteLocalImageUrls('', scope, '/repo/readme.md', ORIGIN)
|
|
expect(out).toBe(``)
|
|
})
|
|
|
|
it('leaves a remote angle destination and its title untouched', () => {
|
|
const md = ''
|
|
expect(rewriteLocalImageUrls(md, scope, '/repo/readme.md', ORIGIN)).toBe(md)
|
|
})
|
|
|
|
it('rewrites a reference definition with an angle destination, keeping its delimiters and title', () => {
|
|
const md = '![logo][logo]\n\n[logo]: <./logo mark.svg> "the logo"'
|
|
const out = rewriteLocalImageUrls(md, scope, '/repo/readme.md', ORIGIN)
|
|
expect(out).toContain(`[logo]: <${ORIGIN}/sidebar/file?sessionId=abc&path=%2Frepo%2Flogo+mark.svg&cwd=%2Frepo> "the logo"`)
|
|
})
|
|
})
|
|
|
|
describe('rewriteProseLocalImageUrls', () => {
|
|
const proseScope: SessionScope = { sessionId: 'abc', cwd: '/repo' }
|
|
const FILE_URL = (path: string, cwd = '/repo'): string =>
|
|
`${ORIGIN}/sidebar/file?sessionId=abc&path=${encodeURIComponent(path)}&cwd=${encodeURIComponent(cwd)}`
|
|
|
|
it('resolves a relative destination against the prose cwd (the row is not a file)', () => {
|
|
const out = rewriteProseLocalImageUrls('', proseScope, '/repo', ORIGIN)
|
|
expect(out).toBe(`})`)
|
|
})
|
|
|
|
it('rewrites a file: URL to the media route (prose and legacy file surfaces)', () => {
|
|
const out = rewriteProseLocalImageUrls('', proseScope, '/repo', ORIGIN)
|
|
expect(out).toBe(`})`)
|
|
expect(rewriteLocalImageUrls('', proseScope, '/repo/readme.md', ORIGIN))
|
|
.toBe(`})`)
|
|
})
|
|
|
|
it('leaves relative destinations untouched when the cwd is unknown', () => {
|
|
const md = '\n'
|
|
expect(rewriteProseLocalImageUrls(md, { sessionId: 'abc' }, undefined, ORIGIN)).toBe(md)
|
|
})
|
|
|
|
it('still serves an absolute local image when the cwd is unknown (no cwd param)', () => {
|
|
const out = rewriteProseLocalImageUrls('', { sessionId: 'abc' }, undefined, ORIGIN)
|
|
expect(out).toBe(``)
|
|
})
|
|
|
|
it('tolerates a trailing separator on the cwd', () => {
|
|
const out = rewriteProseLocalImageUrls('', proseScope, '/repo/', ORIGIN)
|
|
expect(out).toBe(`})`)
|
|
// Repeated separators collapse the same way.
|
|
const repeated = rewriteProseLocalImageUrls('', proseScope, '/repo///', ORIGIN)
|
|
expect(repeated).toBe(`})`)
|
|
})
|
|
|
|
it('resolves a relative destination against a Windows cwd', () => {
|
|
const out = rewriteProseLocalImageUrls('', { sessionId: 'abc', cwd: 'C:\\repo' }, 'C:\\repo\\', ORIGIN)
|
|
// A Windows root keeps its drive and backslash spelling after normalization.
|
|
expect(decodeURIComponent(out.replace(/\+/g, ' '))).toContain('C:\\repo\\cat.png')
|
|
})
|
|
|
|
it('rewrites a prose reference-style image against the cwd', () => {
|
|
const md = '![logo][logo]\n\n[logo]: ./logo.svg'
|
|
const out = rewriteProseLocalImageUrls(md, proseScope, '/repo', ORIGIN)
|
|
expect(out).toContain(`[logo]: ${FILE_URL('/repo/logo.svg')}`)
|
|
expect(out).toContain('![logo][logo]')
|
|
})
|
|
|
|
it('composes with the local-link rewrite: image first, then the link inside its label', () => {
|
|
const md = '[](./doc.md#intro)\n\n'
|
|
const out = rewriteLocalMarkdownLinks(
|
|
rewriteProseLocalImageUrls(md, proseScope, '/repo', ORIGIN),
|
|
undefined,
|
|
'/repo',
|
|
)
|
|
// The image destination became a media URL and the enclosing `.md` link is
|
|
// still claimed (its heading fragment survives as %23).
|
|
expect(out).toContain(`[})](./doc.md%23intro)`)
|
|
expect(out).toContain(`})`)
|
|
})
|
|
})
|