Skip to content

Commit fd5d690

Browse files
re-fcursoragent
andcommitted
fix: restrict /api/markdown to markdown files
Keep serving hidden-directory markdown, but reject non-md paths so .git/config and .env stay unreadable through the widened static mount. Co-authored-by: Cursor <[email protected]>
1 parent e290764 commit fd5d690

4 files changed

Lines changed: 38 additions & 5 deletions

File tree

‎src/server/server.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,12 @@ export const createApp = (
106106
return res.status(403).send('Forbidden');
107107
}
108108

109+
// /api/markdown is only used to fetch markdown. Keep that contract so
110+
// enabling dotfiles below cannot expose .env, .git/config, and similar.
111+
if (!/\.(md|markdown)$/i.test(normalizedPath)) {
112+
return res.status(404).send('File not found');
113+
}
114+
109115
if (!fs.existsSync(filePath)) {
110116
logger.error(`🚫 File not found: ${filePath}`);
111117
return res.status(404).send('File not found');

‎test/e2e/server.test.ts‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,17 @@ describe('Server E2E Tests', () => {
5151
expect(res.text).not.toContain('<title>mdts - Markdown file viewer</title>');
5252
});
5353

54+
it('GET /api/markdown/.git/config should not be served', async () => {
55+
const res = await request(app).get('/api/markdown/.git/config');
56+
expect(res.statusCode).toEqual(404);
57+
});
58+
59+
it('GET /api/markdown/.env should not be served even when the file exists', async () => {
60+
const res = await request(app).get('/api/markdown/.env');
61+
expect(res.statusCode).toEqual(404);
62+
expect(res.text).toEqual('File not found');
63+
});
64+
5465
it('GET /api/outline should return the outline for a markdown file', async () => {
5566
const res = await request(app).get('/api/outline?filePath=test.md');
5667
expect(res.statusCode).toEqual(200);
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
SECRET=should-not-be-served

‎test/unit/server/server.test.ts‎

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -79,11 +79,6 @@ describe('server.ts unit tests', () => {
7979
expect(app.use).toHaveBeenCalledWith(expect.any(Function)); // for express.static
8080
});
8181

82-
it('should allow serving markdown files from dot-directories', () => {
83-
const express = jest.requireMock('express');
84-
expect(express.static).toHaveBeenCalledWith('/mock/directory', { dotfiles: 'allow' });
85-
});
86-
8782
it('should define /api/filetree and /api/outline routes', () => {
8883
expect(app.use).toHaveBeenCalledWith('/api/filetree', expect.objectContaining({ get: expect.any(Function) }));
8984
expect(app.use).toHaveBeenCalledWith('/api/outline', expect.objectContaining({ get: expect.any(Function) }));
@@ -188,6 +183,26 @@ describe('server.ts unit tests', () => {
188183
expect(mockNext).not.toHaveBeenCalled();
189184
});
190185

186+
it('should return 404 for non-markdown files in /api/markdown', async () => {
187+
(fs.existsSync as jest.Mock).mockReturnValue(true);
188+
const mockStatus = jest.fn().mockReturnThis();
189+
const mockSend = jest.fn();
190+
const mockNext = jest.fn();
191+
const req = { path: '.git/config' } as Request;
192+
const res = { status: mockStatus, send: mockSend } as unknown as Response;
193+
194+
const markdownMiddleware = (app.use as jest.Mock).mock.calls.find(
195+
(call: [string, (req: Request, res: Response, next: NextFunction) => void]) =>
196+
call[0] === '/api/markdown' && call.length === 2,
197+
)[1];
198+
await markdownMiddleware(req, res, mockNext);
199+
200+
expect(mockStatus).toHaveBeenCalledWith(404);
201+
expect(mockSend).toHaveBeenCalledWith('File not found');
202+
expect(mockNext).not.toHaveBeenCalled();
203+
expect(fs.existsSync).not.toHaveBeenCalled();
204+
});
205+
191206
it('should call next for existing files in /api/markdown', async () => {
192207
(fs.existsSync as jest.Mock).mockReturnValue(true);
193208
const mockStatus = jest.fn().mockReturnThis();

0 commit comments

Comments
 (0)