Mostly so that it's easier to cut the next patch release for Fresh.
This reverts commit e3508c3054.
This commit is contained in:
+6
-27
@@ -114,31 +114,6 @@ export async function getServerContext(state: InternalFreshState) {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
function redirectTo(pathOrUrl: string = "/", status = 302): Response {
|
|
||||||
let location = pathOrUrl;
|
|
||||||
|
|
||||||
// Disallow protocol relative URLs
|
|
||||||
if (pathOrUrl !== "/" && pathOrUrl.startsWith("/")) {
|
|
||||||
let idx = pathOrUrl.indexOf("?");
|
|
||||||
if (idx === -1) {
|
|
||||||
idx = pathOrUrl.indexOf("#");
|
|
||||||
}
|
|
||||||
|
|
||||||
const pathname = idx > -1 ? pathOrUrl.slice(0, idx) : pathOrUrl;
|
|
||||||
const search = idx > -1 ? pathOrUrl.slice(idx) : "";
|
|
||||||
|
|
||||||
// Remove double slashes to prevent open redirect vulnerability.
|
|
||||||
location = `${pathname.replaceAll(/\/+/g, "/")}${search}`;
|
|
||||||
}
|
|
||||||
|
|
||||||
return new Response(null, {
|
|
||||||
status,
|
|
||||||
headers: {
|
|
||||||
location,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
export class ServerContext {
|
export class ServerContext {
|
||||||
#renderFn: RenderFunction;
|
#renderFn: RenderFunction;
|
||||||
#plugins: Plugin[];
|
#plugins: Plugin[];
|
||||||
@@ -308,7 +283,6 @@ export class ServerContext {
|
|||||||
ctx.data = data;
|
ctx.data = data;
|
||||||
return await renderNotFound(req, ctx);
|
return await renderNotFound(req, ctx);
|
||||||
},
|
},
|
||||||
redirect: redirectTo,
|
|
||||||
route: "",
|
route: "",
|
||||||
get pattern() {
|
get pattern() {
|
||||||
return ctx.route;
|
return ctx.route;
|
||||||
@@ -635,7 +609,12 @@ export class ServerContext {
|
|||||||
if (key !== null && BUILD_ID !== key) {
|
if (key !== null && BUILD_ID !== key) {
|
||||||
url.searchParams.delete(ASSET_CACHE_BUST_KEY);
|
url.searchParams.delete(ASSET_CACHE_BUST_KEY);
|
||||||
const location = url.pathname + url.search;
|
const location = url.pathname + url.search;
|
||||||
return redirectTo(location, 307);
|
return new Response(null, {
|
||||||
|
status: 307,
|
||||||
|
headers: {
|
||||||
|
location,
|
||||||
|
},
|
||||||
|
});
|
||||||
}
|
}
|
||||||
const headers = new Headers({
|
const headers = new Headers({
|
||||||
"content-type": contentType,
|
"content-type": contentType,
|
||||||
|
|||||||
+1
-2
@@ -157,7 +157,7 @@ export type PageProps<T = any, S = Record<string, unknown>> = Omit<
|
|||||||
S,
|
S,
|
||||||
T
|
T
|
||||||
>,
|
>,
|
||||||
"render" | "next" | "renderNotFound" | "redirect"
|
"render" | "next" | "renderNotFound"
|
||||||
>;
|
>;
|
||||||
|
|
||||||
export interface StaticFile {
|
export interface StaticFile {
|
||||||
@@ -206,7 +206,6 @@ export interface FreshContext<
|
|||||||
) => Response | Promise<Response>;
|
) => Response | Promise<Response>;
|
||||||
Component: ComponentType<unknown>;
|
Component: ComponentType<unknown>;
|
||||||
next: () => Promise<Response>;
|
next: () => Promise<Response>;
|
||||||
redirect: (path: string, statusCode?: number) => Response;
|
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
* Context passed to async route components.
|
* Context passed to async route components.
|
||||||
|
|||||||
@@ -65,7 +65,6 @@ import * as $not_found from "./routes/not_found.ts";
|
|||||||
import * as $params from "./routes/params.tsx";
|
import * as $params from "./routes/params.tsx";
|
||||||
import * as $preact_boolean_attrs from "./routes/preact/boolean_attrs.tsx";
|
import * as $preact_boolean_attrs from "./routes/preact/boolean_attrs.tsx";
|
||||||
import * as $props_id_ from "./routes/props/[id].tsx";
|
import * as $props_id_ from "./routes/props/[id].tsx";
|
||||||
import * as $redirect from "./routes/redirect.tsx";
|
|
||||||
import * as $route_groups_islands_index from "./routes/route-groups-islands/index.tsx";
|
import * as $route_groups_islands_index from "./routes/route-groups-islands/index.tsx";
|
||||||
import * as $route_groups_bar_baz_layout from "./routes/route-groups/(bar)/(baz)/_layout.tsx";
|
import * as $route_groups_bar_baz_layout from "./routes/route-groups/(bar)/(baz)/_layout.tsx";
|
||||||
import * as $route_groups_bar_baz_baz from "./routes/route-groups/(bar)/(baz)/baz.tsx";
|
import * as $route_groups_bar_baz_baz from "./routes/route-groups/(bar)/(baz)/baz.tsx";
|
||||||
@@ -187,7 +186,6 @@ const manifest = {
|
|||||||
"./routes/params.tsx": $params,
|
"./routes/params.tsx": $params,
|
||||||
"./routes/preact/boolean_attrs.tsx": $preact_boolean_attrs,
|
"./routes/preact/boolean_attrs.tsx": $preact_boolean_attrs,
|
||||||
"./routes/props/[id].tsx": $props_id_,
|
"./routes/props/[id].tsx": $props_id_,
|
||||||
"./routes/redirect.tsx": $redirect,
|
|
||||||
"./routes/route-groups-islands/index.tsx": $route_groups_islands_index,
|
"./routes/route-groups-islands/index.tsx": $route_groups_islands_index,
|
||||||
"./routes/route-groups/(bar)/(baz)/_layout.tsx":
|
"./routes/route-groups/(bar)/(baz)/_layout.tsx":
|
||||||
$route_groups_bar_baz_layout,
|
$route_groups_bar_baz_layout,
|
||||||
|
|||||||
@@ -1,10 +0,0 @@
|
|||||||
import { FreshContext } from "$fresh/server.ts";
|
|
||||||
|
|
||||||
export const handler = {
|
|
||||||
GET(_req: Request, ctx: FreshContext) {
|
|
||||||
const rawStatus = ctx.url.searchParams.get("status");
|
|
||||||
const status = rawStatus !== null ? Number(rawStatus) : undefined;
|
|
||||||
const location = ctx.url.searchParams.get("path") ?? "/";
|
|
||||||
return ctx.redirect(location, status);
|
|
||||||
},
|
|
||||||
};
|
|
||||||
@@ -1,7 +1,13 @@
|
|||||||
import { Handlers } from "$fresh/server.ts";
|
import { Handlers } from "$fresh/server.ts";
|
||||||
|
|
||||||
export const handler: Handlers<unknown, { data: string }> = {
|
export const handler: Handlers<unknown, { data: string }> = {
|
||||||
GET(_req, ctx) {
|
GET(req) {
|
||||||
return ctx.redirect(ctx.url.origin, 302);
|
const url = new URL(req.url);
|
||||||
|
return new Response(null, {
|
||||||
|
status: 302,
|
||||||
|
headers: {
|
||||||
|
"Location": url.origin,
|
||||||
|
},
|
||||||
|
});
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -273,42 +273,6 @@ Deno.test("no open redirect when passing double slashes", async () => {
|
|||||||
assertEquals(resp.headers.get("location"), "/evil.com");
|
assertEquals(resp.headers.get("location"), "/evil.com");
|
||||||
});
|
});
|
||||||
|
|
||||||
Deno.test("ctx.redirect() - relative urls", async () => {
|
|
||||||
let resp = await handler(
|
|
||||||
new Request("https://fresh.deno.dev/redirect?path=//evil.com/"),
|
|
||||||
);
|
|
||||||
assertEquals(resp.status, 302);
|
|
||||||
assertEquals(resp.headers.get("location"), "/evil.com/");
|
|
||||||
|
|
||||||
resp = await handler(
|
|
||||||
new Request(
|
|
||||||
"https://fresh.deno.dev/redirect?path=//evil.com//foo&status=307",
|
|
||||||
),
|
|
||||||
);
|
|
||||||
assertEquals(resp.status, 307);
|
|
||||||
assertEquals(resp.headers.get("location"), "/evil.com/foo");
|
|
||||||
});
|
|
||||||
|
|
||||||
Deno.test("ctx.redirect() - absolute urls", async () => {
|
|
||||||
const resp = await handler(
|
|
||||||
new Request("https://fresh.deno.dev/redirect?path=https://example.com/"),
|
|
||||||
);
|
|
||||||
assertEquals(resp.status, 302);
|
|
||||||
assertEquals(resp.headers.get("location"), "https://example.com/");
|
|
||||||
});
|
|
||||||
|
|
||||||
Deno.test("ctx.redirect() - with search and hash", async () => {
|
|
||||||
const resp = await handler(
|
|
||||||
new Request(
|
|
||||||
`https://fresh.deno.dev/redirect?path=${
|
|
||||||
encodeURIComponent("/foo/bar?baz=123#foo")
|
|
||||||
}`,
|
|
||||||
),
|
|
||||||
);
|
|
||||||
assertEquals(resp.status, 302);
|
|
||||||
assertEquals(resp.headers.get("location"), "/foo/bar?baz=123#foo");
|
|
||||||
});
|
|
||||||
|
|
||||||
Deno.test("/failure", async () => {
|
Deno.test("/failure", async () => {
|
||||||
const resp = await handler(new Request("https://fresh.deno.dev/failure"));
|
const resp = await handler(new Request("https://fresh.deno.dev/failure"));
|
||||||
assert(resp);
|
assert(resp);
|
||||||
@@ -1205,7 +1169,6 @@ Deno.test("Expose config in ctx", async () => {
|
|||||||
next: "Function",
|
next: "Function",
|
||||||
render: "AsyncFunction",
|
render: "AsyncFunction",
|
||||||
renderNotFound: "AsyncFunction",
|
renderNotFound: "AsyncFunction",
|
||||||
redirect: "Function",
|
|
||||||
localAddr: "<undefined>",
|
localAddr: "<undefined>",
|
||||||
pattern: "/ctx_config",
|
pattern: "/ctx_config",
|
||||||
data: "<undefined>",
|
data: "<undefined>",
|
||||||
|
|||||||
@@ -117,7 +117,6 @@ Deno.test("passes context to server component", async () => {
|
|||||||
params: {
|
params: {
|
||||||
id: "foo",
|
id: "foo",
|
||||||
},
|
},
|
||||||
redirect: "Function",
|
|
||||||
state: {},
|
state: {},
|
||||||
isPartial: false,
|
isPartial: false,
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -12,7 +12,11 @@ export async function handler(
|
|||||||
// Redirect from old doc URLs to new ones
|
// Redirect from old doc URLs to new ones
|
||||||
const redirect = REDIRECTS[ctx.url.pathname];
|
const redirect = REDIRECTS[ctx.url.pathname];
|
||||||
if (redirect) {
|
if (redirect) {
|
||||||
return ctx.redirect(redirect, 307);
|
const url = new URL(redirect, ctx.url.origin);
|
||||||
|
return new Response("", {
|
||||||
|
status: 307,
|
||||||
|
headers: { location: url.href },
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
return await ctx.next();
|
return await ctx.next();
|
||||||
|
|||||||
Reference in New Issue
Block a user