ovr 2.0.4 → 2.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.ts CHANGED
@@ -5,4 +5,5 @@ export { Context } from "./app/context.js";
5
5
  export { Page } from "./app/page.js";
6
6
  export { Action } from "./app/action.js";
7
7
  export { escape } from "./escape/index.js";
8
+ export { csrf } from "./mw/csrf.js";
8
9
  export type * from "./types/index.js";
package/dist/index.js CHANGED
@@ -5,3 +5,4 @@ export { Context } from "./app/context.js";
5
5
  export { Page } from "./app/page.js";
6
6
  export { Action } from "./app/action.js";
7
7
  export { escape } from "./escape/index.js";
8
+ export { csrf } from "./mw/csrf.js";
@@ -0,0 +1,18 @@
1
+ import type { Middleware } from "../app/index.js";
2
+ /**
3
+ * Basic CSRF protection that checks the request's `method` and `Origin` header.
4
+ *
5
+ * @param options
6
+ * @returns CSRF middleware
7
+ *
8
+ * @example
9
+ *
10
+ * ```ts
11
+ * import { csrf } from "ovr";
12
+ *
13
+ * app.use(csrf({ origin: "https://example.com" }));
14
+ * ```
15
+ */
16
+ export declare const csrf: (options: {
17
+ origin: string | string[];
18
+ }) => Middleware;
@@ -0,0 +1,26 @@
1
+ /**
2
+ * Basic CSRF protection that checks the request's `method` and `Origin` header.
3
+ *
4
+ * @param options
5
+ * @returns CSRF middleware
6
+ *
7
+ * @example
8
+ *
9
+ * ```ts
10
+ * import { csrf } from "ovr";
11
+ *
12
+ * app.use(csrf({ origin: "https://example.com" }));
13
+ * ```
14
+ */
15
+ export const csrf = (options) => {
16
+ if (typeof options.origin === "string")
17
+ options.origin = [options.origin];
18
+ return (c, next) => {
19
+ if (["GET", "HEAD"].includes(c.req.method))
20
+ return next();
21
+ const origin = c.req.headers.get("Origin");
22
+ if (origin && options.origin.includes(origin))
23
+ return next();
24
+ return c.text("Forbidden", 403);
25
+ };
26
+ };
@@ -3,20 +3,22 @@
3
3
  * @param params Parameters to insert
4
4
  * @returns Resolved pathname with params
5
5
  */
6
- export const insertParams = (parts, params) => parts
7
- .map((part) => {
8
- if (part.startsWith(":")) {
9
- const param = part.slice(1);
10
- if (!(param in params))
11
- throw new Error(`Parameter "${param}" did not match pattern..`);
12
- return params[param];
13
- }
6
+ export const insertParams = (parts, params) => {
14
7
  const wild = "*";
15
- if (part === wild) {
16
- if (!(wild in params))
17
- throw new Error("No wildcard parameter found.");
18
- return params[wild];
19
- }
20
- return part;
21
- })
22
- .join("/");
8
+ return parts
9
+ .map((part) => {
10
+ if (part.startsWith(":")) {
11
+ const param = part.slice(1);
12
+ if (!(param in params))
13
+ throw new Error(`Parameter "${param}" did not match pattern.`);
14
+ return params[param];
15
+ }
16
+ if (part === wild) {
17
+ if (!(wild in params))
18
+ throw new Error("No wildcard parameter found.");
19
+ return params[wild];
20
+ }
21
+ return part;
22
+ })
23
+ .join("/");
24
+ };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "ovr",
3
3
  "description": "Progressive HTML Rendering",
4
- "version": "2.0.4",
4
+ "version": "2.1.0",
5
5
  "license": "MIT",
6
6
  "repository": {
7
7
  "type": "git",