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

Request Handler: Urldecode the requested path #1228

Merged
merged 2 commits into from
Apr 11, 2024
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
43 changes: 43 additions & 0 deletions packages/php-wasm/node/src/test/php-request-handler.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,49 @@ describe.each(SupportedPHPVersions)(
});
});

it('should serve a static file with urlencoded entities in the path', async () => {
php.writeFile(
'/Screenshot 2024-04-05 at 7.13.36 AM.html',
`Hello World`
);
const response = await handler.request({
url: '/Screenshot 2024-04-05 at 7.13.36%E2%80%AFAM.html',
});
expect(response).toEqual({
httpStatusCode: 200,
headers: {
'content-type': ['text/html'],

'accept-ranges': ['bytes'],
'cache-control': ['public, max-age=0'],
'content-length': ['11'],
},
bytes: new TextEncoder().encode('Hello World'),
errors: '',
exitCode: 0,
});
});

it('should serve a PHP file with urlencoded entities in the path', async () => {
php.writeFile(
'/Screenshot 2024-04-05 at 7.13.36 AM.php',
`Hello World`
);
const response = await handler.request({
url: '/Screenshot 2024-04-05 at 7.13.36%E2%80%AFAM.php',
});
expect(response).toEqual({
httpStatusCode: 200,
headers: {
'content-type': ['text/html; charset=UTF-8'],
'x-powered-by': [expect.any(String)],
},
bytes: new TextEncoder().encode('Hello World'),
errors: '',
exitCode: 0,
});
});

it('should yield x-file-type=static when a static file is not found', async () => {
const response = await handler.request({
url: '/index.html',
Expand Down
13 changes: 9 additions & 4 deletions packages/php-wasm/universal/src/lib/php-request-handler.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Semaphore } from '@php-wasm/util';
import { Semaphore, joinPaths } from '@php-wasm/util';
import {
ensurePathPrefix,
toRelativeUrl,
Expand Down Expand Up @@ -125,10 +125,13 @@ export class PHPRequestHandler implements RequestHandler {
);

const normalizedRequestedPath = applyRewriteRules(
removePathPrefix(requestedUrl.pathname, this.#PATHNAME),
removePathPrefix(
decodeURIComponent(requestedUrl.pathname),
this.#PATHNAME
),
this.rewriteRules
);
const fsPath = `${this.#DOCROOT}${normalizedRequestedPath}`;
const fsPath = joinPaths(this.#DOCROOT, normalizedRequestedPath);
if (seemsLikeAPHPRequestHandlerPath(fsPath)) {
return await this.#dispatchToPHP(request, requestedUrl);
}
Expand Down Expand Up @@ -228,7 +231,9 @@ export class PHPRequestHandler implements RequestHandler {

let scriptPath;
try {
scriptPath = this.#resolvePHPFilePath(requestedUrl.pathname);
scriptPath = this.#resolvePHPFilePath(
decodeURIComponent(requestedUrl.pathname)
);
} catch (error) {
return new PHPResponse(
404,
Expand Down
Loading