Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .changeset/rotten-wolves-pull.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
'@modern-js/server-core': patch
---

fix: escape special characters in static file routes to prevent regex syntax errors

fix: 转义静态文件路由中的特殊字符,防止正则表达式语法错误

26 changes: 23 additions & 3 deletions packages/server/core/src/plugins/render/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,18 @@ import { requestLatencyMiddleware } from '../monitors';

export * from './inject';

const DYNAMIC_ROUTE_REG = /\/:./;

/**
* Escape special regex characters in a path string.
* This is needed because Hono's router converts paths to regex patterns,
* and special characters like parentheses need to be escaped.
*/
function escapeRegexSpecialChars(path: string): string {
// Escape special regex characters: ( ) [ ] { } * + ? . ^ $ | \
return path.replace(/[()[\]{}*+?.^$|\\]/g, '\\$&');
}

export const renderPlugin = (): ServerPluginLegacy => ({
name: '@modern-js/plugin-render',

Expand Down Expand Up @@ -47,9 +59,17 @@ export const renderPlugin = (): ServerPluginLegacy => ({

for (const route of pageRoutes) {
const { urlPath: originUrlPath, entryName = MAIN_ENTRY_NAME } = route;
const urlPath = originUrlPath.endsWith('/')
? `${originUrlPath}*`
: `${originUrlPath}/*`;
const isDynamic = DYNAMIC_ROUTE_REG.test(originUrlPath);

// For static routes, escape special regex characters to prevent regex syntax errors
// For dynamic routes, keep as-is since they contain route parameters
const escapedPath = isDynamic
? originUrlPath
: escapeRegexSpecialChars(originUrlPath);

const urlPath = escapedPath.endsWith('/')
? `${escapedPath}*`
: `${escapedPath}/*`;

// Hook middleware will handle stream as string and then handle it as stream, which will cause the performance problem
// TODO: Hook middleware will be deprecated in next version
Expand Down
32 changes: 16 additions & 16 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions tests/integration/server-prod/config/public/test(bug.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
test
8 changes: 8 additions & 0 deletions tests/integration/server-prod/tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,12 @@ describe('test basic usage', () => {
expect(status).toBe(successStatus);
expect(headers['content-type']).toBe('image/png');
});

test(`should serve static file with special characters in filename`, async () => {
const { status, data } = await axios.get(
`http://localhost:${appPort}/test(bug.txt`,
);
expect(status).toBe(successStatus);
expect(data.trim()).toBe('test');
});
});