Skip to content

Commit

Permalink
Escape backslash in response headers
Browse files Browse the repository at this point in the history
  • Loading branch information
bgrgicak committed Feb 23, 2024
1 parent 51d0589 commit 36e7fcb
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
5 changes: 3 additions & 2 deletions packages/php-wasm/compile/php/php_wasm.c
Original file line number Diff line number Diff line change
Expand Up @@ -1259,7 +1259,7 @@ int EMSCRIPTEN_KEEPALIVE wasm_sapi_handle_request()
run_php(wasm_server_context->php_code);
}
result = EG(exit_status);

wasm_request_done:
wasm_sapi_request_shutdown();
return result;
Expand Down Expand Up @@ -1422,7 +1422,8 @@ static void wasm_sapi_send_header(sapi_header_struct *sapi_header, void *server_
_fwrite(headers_file, "\"");
for (int i = 0, max = sapi_header->header_len; i < max; i++)
{
if (sapi_header->header[i] == '"')
// Escape quotes and backslashes
if (sapi_header->header[i] == '"' || sapi_header->header[i] == '\\')
{
fwrite(&"\\", sizeof(char), 1, headers_file);
}
Expand Down
9 changes: 9 additions & 0 deletions packages/php-wasm/node/src/test/php.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1340,4 +1340,13 @@ bar1
expect(consoleErrorMock).not.toHaveBeenCalled();
});
});

describe('Response parsing', () => {
it('should encode response headers', async () => {
const out = await php.run({
code: `<?php header('Location: /(?P<id>[\\d]+)');`,
});
expect(out.headers['location'][0]).toEqual('/(?P<id>[\\d]+)');
});
});
});

0 comments on commit 36e7fcb

Please sign in to comment.