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

"Target" not "URL" #217

Merged
merged 19 commits into from
Jan 20, 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
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ Breaking changes:
* None.

Other notable changes:
* None.
* Fixed handling of non-`origin` request targets. Before v0.6.1, these were
treated as if they were `origin` requests (that is, the usual kind that
specify a resource path), and in v0.6.1 they started causing crashes. Now,
they're properly classified.

### v0.6.1 -- 2024-01-19

Expand Down
2 changes: 1 addition & 1 deletion src/builtin-applications/export/Redirector.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export class Redirector extends BaseApplication {

/** @override */
async _impl_handleRequest(request, dispatch) {
return request.redirect(
return request.sendRedirect(
`${this.#target}${dispatch.extraString}`,
this.#statusCode);
}
Expand Down
4 changes: 2 additions & 2 deletions src/builtin-applications/export/StaticFiles.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,15 @@ export class StaticFiles extends BaseApplication {

if (!resolved) {
if (this.#notFoundType) {
return request.notFound(this.#notFoundType, this.#notFoundContents);
return request.sendNotFound(this.#notFoundType, this.#notFoundContents);
} else {
return false;
}
}

if (resolved.redirect) {
const redirectTo = resolved.redirect;
return request.redirect(redirectTo, 301);
return request.sendRedirect(redirectTo, 301);
} else if (resolved.path) {
return await request.sendFile(resolved.path, StaticFiles.#SEND_OPTIONS);
} else {
Expand Down
26 changes: 9 additions & 17 deletions src/network-protocol/export/ProtocolWrangler.js
Original file line number Diff line number Diff line change
Expand Up @@ -375,21 +375,8 @@ export class ProtocolWrangler {
* to run.
*/
async #handleExpressRequest(req, res, next) {
const context = WranglerContext.getNonNull(req.socket, req.stream?.session);

// TODO: `request.url` is not sanitized by Node, and furthermore it could
// legitimately be the form used when talking to a proxy (see RFC7230,
// section 5.3). `Request` does its own sanity check and will reject those,
// but we should catch it here, in a less ad-hoc way, before trying to
// construct the `Request`.
let request;
try {
request = new Request(context, req, res, this.#requestLogger);
} catch (e) {
res.sendStatus(400); // "Bad Request."
return;
}

const context = WranglerContext.getNonNull(req.socket, req.stream?.session);
const request = new Request(context, req, res, this.#requestLogger);
const reqLogger = request.logger;

const reqCtx = WranglerContext.forRequest(context, request);
Expand All @@ -399,10 +386,15 @@ export class ProtocolWrangler {

res.set('Server', this.#serverHeader);

if (this.#rateLimiter) {
if (!request.pathnameString) {
// It's not an `origin` request. We don't handle any other type of
// target... yet.
await request.sendError(400); // "Bad Request."
return;
} else if (this.#rateLimiter) {
const granted = await this.#rateLimiter.newRequest(reqLogger);
if (!granted) {
res.sendStatus(503);
await request.sendError(503); // "Service Unavailable."

// Wait for the response to have been at least nominally sent before
// closing the socket, in the hope that there is a good chance that it
Expand Down
Loading