Security: Reject cross-origin connections to daemon and stream server (#274)
This commit is contained in:
@@ -196,10 +196,23 @@ export async function startDaemon(options?: { streamPort?: number }): Promise<vo
|
||||
|
||||
const server = net.createServer((socket) => {
|
||||
let buffer = '';
|
||||
let httpChecked = false;
|
||||
|
||||
socket.on('data', async (data) => {
|
||||
buffer += data.toString();
|
||||
|
||||
// Security: Detect and reject HTTP requests to prevent cross-origin attacks.
|
||||
// Browsers using fetch() must send HTTP headers (e.g., "POST / HTTP/1.1"),
|
||||
// while legitimate clients send raw JSON starting with "{".
|
||||
if (!httpChecked) {
|
||||
httpChecked = true;
|
||||
const trimmed = buffer.trimStart();
|
||||
if (/^(GET|POST|PUT|DELETE|HEAD|OPTIONS|PATCH|CONNECT|TRACE)\s/i.test(trimmed)) {
|
||||
socket.destroy();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// Process complete lines
|
||||
while (buffer.includes('\n')) {
|
||||
const newlineIdx = buffer.indexOf('\n');
|
||||
|
||||
Reference in New Issue
Block a user