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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## next

- fix(types): fix Logger type
- fix(error-response-plugin): sanitize input

## [v3.0.5](https://github.com/chimurai/http-proxy-middleware/releases/tag/v3.0.5)

Expand Down
2 changes: 1 addition & 1 deletion cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"path": "CONTRIBUTORS.txt"
}
],
"ignoreRegExpList": ["[a-z]+path", "\\]\\(#[a-z-]+\\)"],
"ignoreRegExpList": ["[a-z]+path", "\\]\\(#[a-z-]+\\)", "%[\\dA-Z]{2}"],
"words": [
"brotli",
"camelcase",
Expand Down
3 changes: 2 additions & 1 deletion src/plugins/default/error-response-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type { Socket } from 'node:net';

import { getStatusCode } from '../../status-code';
import { Plugin } from '../../types';
import { sanitize } from '../../utils/sanitize';

function isResponseLike(obj: any): obj is http.ServerResponse {
return obj && typeof obj.writeHead === 'function';
Expand All @@ -26,7 +27,7 @@ export const errorResponsePlugin: Plugin = (proxyServer, options) => {
}

const host = req.headers && req.headers.host;
res.end(`Error occurred while trying to proxy: ${host}${req.url}`);
res.end(`Error occurred while trying to proxy: ${sanitize(host)}${sanitize(req.url)}`);
} else if (isSocketLike(res)) {
res.destroy();
}
Expand Down
3 changes: 3 additions & 0 deletions src/utils/sanitize.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function sanitize(input: string | undefined): string {
return input?.replace(/[<>]/g, (i) => encodeURIComponent(i)) ?? '';
}
17 changes: 17 additions & 0 deletions test/unit/utils/sanitize.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { sanitize } from '../../../src/utils/sanitize';

describe('sanitize()', () => {
it('should return empty string for undefined input', () => {
expect(sanitize(undefined)).toEqual('');
});

it('should replace special characters with their HTML entity equivalents', () => {
const input = '<>';
expect(sanitize(input)).toMatchInlineSnapshot(`"%3C%3E"`);
});

it('should replace special characters with HTML entities', () => {
const input = '<script>alert("XSS")</script>';
expect(sanitize(input)).toMatchInlineSnapshot(`"%3Cscript%3Ealert("XSS")%3C/script%3E"`);
});
});