Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wasm_sapi_send_headers produces broken JSON due to incorrect escaping #1046

Closed
adamziel opened this issue Feb 22, 2024 · 0 comments · Fixed by #1050
Closed

wasm_sapi_send_headers produces broken JSON due to incorrect escaping #1046

adamziel opened this issue Feb 22, 2024 · 0 comments · Fixed by #1050
Assignees
Labels
[Feature] PHP.wasm [Type] Bug An existing feature does not function as intended

Comments

@adamziel
Copy link
Collaborator

When using the new Font Library in WordPress 6.5, I ran into the following error:

register-service-worker.ts:64 Uncaught (in promise) SyntaxError: Bad escaped character in JSON at position 601 (line 9 column 122) (at comlink.js?v=01749073:42:27)
    at JSON.parse (<anonymous>)
    at #getResponseHeaders (base-php.ts:349:28)
    at #handleRequest (base-php.ts:631:44)
    at async WebPHP.run (base-php.ts:267:21)
    at async #dispatchToPHP (php-request-handler.ts:243:11)
    at async PHPRequestHandler.request (php-request-handler.ts:119:11)
    at async PHPBrowser.request (php-browser.ts:61:20)

Turns out, it was in this line in base-php.ts:

const headersData = JSON.parse(this.readFileAsText(headersFilePath));

It tries to parse the JSON-encoded headers emited by PHP.wasm. Here's the output that tripped it up:

{ "status": 201, "headers": ["X-Powered-By: PHP/8.0.30-dev",
"Content-Type: application/json; charset=UTF-8",
"X-Robots-Tag: noindex",
"Link: <http://localhost:5400/scope:0.4235061639379647/index.php?rest_route=/>; rel=\"https://api.w.org/\"",
"X-Content-Type-Options: nosniff",
"Access-Control-Expose-Headers: X-WP-Total, X-WP-TotalPages, Link",
"Access-Control-Allow-Headers: Authorization, X-WP-Nonce, Content-Disposition, Content-MD5, Content-Type",
"X-WP-Nonce: bfcdf46c4b",
"Location: http://localhost:5400/scope:0.4235061639379647/index.php?rest_route=/wp/v2/font-families/(?P<font_family_id>[\d]+)/font-faces/13",
"Allow: GET, POST",
"Expires: Wed, 11 Jan 1984 05:00:00 GMT",
"Cache-Control: no-cache, must-revalidate, max-age=0, no-store, private"  ]}

Turns out, it's about \d. We should probably escape backslashes like we escape double quotes: in the wasm_sapi_send_headers C function:

for (int i = 0, max = sapi_header->header_len; i < max; i++)
{
if (sapi_header->header[i] == '"')
{
fwrite(&"\\", sizeof(char), 1, headers_file);
}
fwrite(&sapi_header->header[i], sizeof(char), 1, headers_file);
}

cc @bgrgicak – would you like to tackle this one?

@adamziel adamziel added [Type] Bug An existing feature does not function as intended [Feature] PHP.wasm labels Feb 22, 2024
@bgrgicak bgrgicak self-assigned this Feb 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Feature] PHP.wasm [Type] Bug An existing feature does not function as intended
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants