Merge #869: feat(uploads): prompt uploads accept .html and .htm as documents

This commit is contained in:
Codeman maintainer committed 2026-12-03 10:15:11 +01:00
commit 7df0f11892
14 files changed
+81 -25

No files matched your search

+1 -1
View File
@@ -612,7 +612,7 @@ describe('mobile prompt composer', () => {
bar.composePrompt();
const fileInput = document.querySelector('.paste-file-input') as HTMLInputElement;
expect(fileInput.getAttribute('accept')).toBe(
'image/*,video/*,.pdf,.docx,.pptx,.xlsx,.zip,.txt,.md,.csv,.tsv,.json,.log,.yaml,.yml,.xml'
'image/*,video/*,.pdf,.docx,.pptx,.xlsx,.zip,.txt,.md,.csv,.tsv,.json,.log,.yaml,.yml,.xml,.html,.htm'
);
expect((document.querySelector('.paste-image') as HTMLButtonElement).textContent).toBe('📎 Attach');
const video = new document.defaultView!.File(['clip'], 'clip.mov', { type: 'video/quicktime' });
+21
View File
@@ -963,6 +963,27 @@ describe('file-routes', () => {
expect(res.headers['x-content-type-options']).toBe('nosniff');
});
it('serves an uploaded HTML page as inert octet-stream with nosniff, never as text/html', async () => {
// Prompt uploads admit .html/.htm as documents, so a page a user attached sits
// in the workspace. Reading it back must never run it on this origin: html has
// no entry in file-raw's MIME table, so it falls through to octet-stream, which
// a browser downloads, and nosniff stops it guessing text/html from the bytes.
for (const name of ['spec.html', 'report.htm']) {
const content = Buffer.from('<script>fetch("/api/sessions")</script>');
mockedCreateReadStream.mockReturnValue(Readable.from([content]) as never);
mockedStat.mockResolvedValue({ size: content.length } as never);
const res = await harness.app.inject({
method: 'GET',
url: `/api/sessions/${harness.ctx._sessionId}/file-raw?path=.codeman-uploads/paste-1-ab-${name}`,
});
expect(res.statusCode, name).toBe(200);
expect(res.headers['content-type'], name).toBe('application/octet-stream');
expect(res.headers['x-content-type-options'], name).toBe('nosniff');
}
});
it('rejects path traversal in raw file serving', async () => {
mockedRealpathSync.mockImplementation(
(p: string) => (p === harness.ctx._session.workingDir ? p : '/etc/shadow') as never
+4
View File
@@ -336,6 +336,10 @@ describe('html-view routes', () => {
writeFileSync(join(workspace, '.cache', 'r.html'), '<p>r</p>');
writeFileSync(join(workspace, '.cache', 'token.json'), '{}');
expect((await open(join(workspace, '.cache', 'r.html'))).statusCode).toBe(403);
// A page attached through a prompt upload lands here, and is never rendered.
mkdirSync(join(workspace, '.codeman-uploads'));
writeFileSync(join(workspace, '.codeman-uploads', 'paste-1-ab-spec.html'), '<p>spec</p>');
expect((await open(join(workspace, '.codeman-uploads', 'paste-1-ab-spec.html'))).statusCode).toBe(403);
mkdirSync(join(root, '.hidden'));
writeFileSync(join(root, '.hidden', 'r.html'), '<p>r</p>');
expect((await open(join(root, '.hidden', 'r.html'))).statusCode).toBe(403);
+5 -2
View File
@@ -807,6 +807,9 @@ describe('session-routes', () => {
['todo.md', 'text/markdown', Buffer.from('# héllo\n- [ ] one\n'), /-todo\.md$/],
['data.csv', 'application/octet-stream', Buffer.from('a,b\n1,2\n'), /-data\.csv$/],
['archive.zip', 'application/zip', zip, /-archive\.zip$/],
// An AI-written spec or report exported as HTML is context, like a .md.
['spec.html', 'text/html', Buffer.from('<!doctype html><h1>Spéc</h1>\n'), /-spec\.html$/],
['report.HTM', 'application/octet-stream', Buffer.from('<p>report</p>\n'), /-report\.htm$/],
] as const) {
const res = await upload(name, mime, bytes);
expect(res.statusCode, name).toBe(200);
@@ -830,12 +833,12 @@ describe('session-routes', () => {
await rm(workDir, { recursive: true });
});
it('refuses a script or an HTML page by name, with a message that says so', async () => {
it('refuses a script by name, with a message that says so', async () => {
const workDir = await mkdtemp(join(tmpdir(), 'codeman-doc-'));
harness.ctx._session.workingDir = workDir;
for (const [name, mime] of [
['run.sh', 'text/x-shellscript'],
['page.html', 'text/html'],
['app.js', 'text/javascript'],
['app.py', 'text/x-python'],
] as const) {
const res = await upload(name, mime, Buffer.from('#!/bin/sh\necho hi\n'));
+5 -4
View File
@@ -19,7 +19,7 @@ function loadToolbar(sessionId: string | null = 'session-1', localEcho = true, k
const dom = new JSDOM(
'<!DOCTYPE html><html><body><span class="btn-attach-mobile-wrap">' +
'<button type="button" id="attachBtnMobile"></button>' +
'<input type="file" id="attachFileInput" accept="image/*,video/*,.pdf,.docx,.pptx,.xlsx,.zip,.txt,.md,.csv,.tsv,.json,.log,.yaml,.yml,.xml" multiple>' +
'<input type="file" id="attachFileInput" accept="image/*,video/*,.pdf,.docx,.pptx,.xlsx,.zip,.txt,.md,.csv,.tsv,.json,.log,.yaml,.yml,.xml,.html,.htm" multiple>' +
'</span><div id="terminalContainer"></div></body></html>'
);
const { window } = dom;
@@ -361,8 +361,8 @@ describe('phone toolbar Attach button', () => {
};
const photo = new window.File(['p'], 'photo.jpg', { type: 'image/jpeg' });
const script = new window.File(['#!/bin/sh'], 'run.sh', { type: 'text/x-shellscript' });
const page = new window.File(['<p>'], 'page.html', { type: 'text/html' });
drop([photo, script, page]);
const program = new window.File(['print(1)'], 'app.py', { type: 'text/x-python' });
drop([photo, script, program]);
expect(app._uploadAndInsertImages).toHaveBeenCalledWith([photo]);
expect(app.showToast).toHaveBeenCalledWith('2 files skipped: unsupported type', 'info');
app.showToast.mockClear();
@@ -397,7 +397,8 @@ describe('phone toolbar Attach button', () => {
expect(kind('todo.md', 'text/plain')).toBe('document');
expect(kind('data.csv', 'application/octet-stream')).toBe('document');
expect(kind('run.sh', 'text/x-shellscript')).toBeNull();
expect(kind('page.html', 'text/html')).toBeNull();
expect(kind('spec.html', 'text/html')).toBe('document');
expect(kind('report.HTM', '')).toBe('document');
expect(kind('noext', '')).toBeNull();
});