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

[FEAT]: create server only access control for loaders and actions #884

Merged
merged 3 commits into from
Jan 16, 2025
Merged
Show file tree
Hide file tree
Changes from 2 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
13 changes: 9 additions & 4 deletions blocks/action.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
// deno-lint-ignore-file no-explicit-any
import { applyProps, type FnProps } from "../blocks/utils.tsx";
import {
applyProps,
type FnProps,
type GateKeeperAccess,
} from "../blocks/utils.tsx";
import JsonViewer from "../components/JsonViewer.tsx";
import type { Block, BlockModule, InstanceOf } from "../engine/block.ts";
import { gateKeeper } from "./utils.tsx";

export type Action = InstanceOf<typeof actionBlock, "#/root/actions">;

export type ActionModule<
export interface ActionModule<
TProps = any,
TResp = any,
> = BlockModule<FnProps<TProps, TResp>>;
> extends BlockModule<FnProps<TProps, TResp>>, GateKeeperAccess {}

const actionBlock: Block<ActionModule> = {
type: "actions",
Expand All @@ -17,7 +22,7 @@ const actionBlock: Block<ActionModule> = {
>(
mod: ActionModule<TProps>,
) => [
applyProps(mod),
applyProps(gateKeeper(mod)),
],
defaultPreview: (result) => {
return {
Expand Down
10 changes: 8 additions & 2 deletions blocks/loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import {
applyProps,
type FnContext,
type FnProps,
gateKeeper,
type GateKeeperAccess,
type RequestState,
type SingleFlightKeyFunc,
} from "./utils.tsx";
Expand All @@ -28,7 +30,7 @@ export type Loader = InstanceOf<typeof loaderBlock, "#/root/loaders">;
export interface LoaderModule<
TProps = any,
TState = any,
> extends BlockModule<FnProps<TProps>> {
> extends BlockModule<FnProps<TProps>>, GateKeeperAccess {
/**
* Specifies caching behavior for the loader and its dependencies.
*
Expand Down Expand Up @@ -335,7 +337,11 @@ const loaderBlock: Block<LoaderModule> = {
adapt: <TProps = any>(mod: LoaderModule<TProps>) => [
wrapCaughtErrors,
(props: TProps, ctx: HttpContext<{ global: any } & RequestState>) =>
applyProps(wrapLoader(mod, ctx.resolveChain, ctx.context.state.release))(
applyProps(
gateKeeper(
wrapLoader(mod, ctx.resolveChain, ctx.context.state.release),
),
)(
props,
ctx,
),
Expand Down
31 changes: 30 additions & 1 deletion blocks/utils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import type { StatusCode as Status } from "@std/http/status";
import type { JSX } from "preact";
import type { AppManifest, ImportMap } from "../blocks/app.ts";
import { isInvokeCtx } from "../blocks/loader.ts";
import { isInvokeCtx, LoaderModule } from "../blocks/loader.ts";
import type { InvocationFunc } from "../clients/withManifest.ts";
import { withSection } from "../components/section.tsx";
import type {
Expand All @@ -28,6 +28,10 @@ import { type Device, deviceOf, isBot as isUABot } from "../utils/userAgent.ts";
import type { HttpContext } from "./handler.ts";
import type { Vary } from "../utils/vary.ts";

export interface GateKeeperAccess {
defaultVisibility?: "private" | "public";
}

export type SingleFlightKeyFunc<TConfig = any, TCtx = any> = (
args: TConfig,
ctx: TCtx,
Expand Down Expand Up @@ -138,6 +142,7 @@ export const fnContextFromHttpContext = <TState = {}>(
},
};
};

/**
* Applies the given props to the target block function.
*
Expand Down Expand Up @@ -248,3 +253,27 @@ export const buildImportMap = (manifest: AppManifest): ImportMap => {
);
return buildImportMapWith(manifest, builder);
};

export const gateKeeper = (
{
default: handler,
defaultVisibility = "public",
...rest
}: BlockModule & GateKeeperAccess,
) => {
return {
...rest,
default: async (
props: Parameters<typeof handler>[0],
req: Request,
ctx: FnContext<unknown, any>,
): Promise<ReturnType<typeof handler>> => {
if (defaultVisibility === "private" && !ctx.isInvoke) {
return new Response(null, {
status: 403,
});
}
return await handler(props, req, ctx);
},
};
};
Loading