@sharadox/protection-web-library 0.1.0-development.6 → 0.1.0-development.9

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/README.md CHANGED
@@ -86,7 +86,7 @@ not for secrets.
86
86
 
87
87
  ### `protection-migration`: `migrateProtection(database)`, `protectionMigrations`, `PROTECTION_MIGRATION_TABLE`
88
88
 
89
- Creates `rate_limit_bucket` and `spending_cap_usage`. Type a Kysely instance with `ProtectionDatabase`
89
+ Creates `rate_limit_bucket`, `spending_cap_usage` and `used_challenge`. Type a Kysely instance with `ProtectionDatabase`
90
90
  (`protection-database/type`), or include it in the application's own schema type.
91
91
 
92
92
  ### `rate-limit`: `limitRate(database, layers, { secret, now? })` → `{ allowed, retryAfterSeconds, blockedBy }`
@@ -109,6 +109,12 @@ const result = await limitRate(
109
109
  );
110
110
  ```
111
111
 
112
+ ### `solution-redemption`: `redeemSolution(database, solution, { secret, now? })`
113
+
114
+ `verifySolution` plus spending it: a valid solution counts once (its challenge id is kept until the challenge
115
+ expires), so a solved challenge can't be replayed. Reasons: `signature`, `expired`, `work`, `used`.
116
+ `pruneUsedChallenges(database, { now? })` (`used-challenge-prune`) forgets expired ones, from a daily job.
117
+
112
118
  ### `rate-limit-prune`: `pruneRateLimits(database, { before })` → deleted count
113
119
 
114
120
  For a daily job. Pick `before` so that every bucket untouched since then would be full again.
@@ -10,9 +10,15 @@ export interface SpendingCapUsageTable {
10
10
  period_start: Date;
11
11
  used: number;
12
12
  }
13
+ /** A proof-of-work challenge already answered, kept until it expires so its solution counts once. */
14
+ export interface UsedChallengeTable {
15
+ id: string;
16
+ expires_at: Date;
17
+ }
13
18
  /** The tables `protectionMigrations` creates, for typing a Kysely instance. */
14
19
  export interface ProtectionDatabase {
15
20
  rate_limit_bucket: RateLimitBucketTable;
16
21
  spending_cap_usage: SpendingCapUsageTable;
22
+ used_challenge: UsedChallengeTable;
17
23
  }
18
24
  //# sourceMappingURL=type.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"type.d.ts","sourceRoot":"","sources":["../../source/protection-database/type.ts"],"names":[],"mappings":"AAAA,qHAAqH;AACrH,MAAM,WAAW,oBAAoB;IACnC,GAAG,EAAE,MAAM,CAAC;IACZ,MAAM,EAAE,MAAM,CAAC;IACf,UAAU,EAAE,IAAI,CAAC;CAClB;AAED,sDAAsD;AACtD,MAAM,WAAW,qBAAqB;IACpC,IAAI,EAAE,MAAM,CAAC;IACb,YAAY,EAAE,IAAI,CAAC;IACnB,IAAI,EAAE,MAAM,CAAC;CACd;AAED,+EAA+E;AAC/E,MAAM,WAAW,kBAAkB;IACjC,iBAAiB,EAAE,oBAAoB,CAAC;IACxC,kBAAkB,EAAE,qBAAqB,CAAC;CAC3C"}
1
+ {"version":3,"file":"type.d.ts","sourceRoot":"","sources":["../../source/protection-database/type.ts"],"names":[],"mappings":"AAAA,qHAAqH;AACrH,MAAM,WAAW,oBAAoB;IACnC,GAAG,EAAE,MAAM,CAAC;IACZ,MAAM,EAAE,MAAM,CAAC;IACf,UAAU,EAAE,IAAI,CAAC;CAClB;AAED,sDAAsD;AACtD,MAAM,WAAW,qBAAqB;IACpC,IAAI,EAAE,MAAM,CAAC;IACb,YAAY,EAAE,IAAI,CAAC;IACnB,IAAI,EAAE,MAAM,CAAC;CACd;AAED,qGAAqG;AACrG,MAAM,WAAW,kBAAkB;IACjC,EAAE,EAAE,MAAM,CAAC;IACX,UAAU,EAAE,IAAI,CAAC;CAClB;AAED,+EAA+E;AAC/E,MAAM,WAAW,kBAAkB;IACjC,iBAAiB,EAAE,oBAAoB,CAAC;IACxC,kBAAkB,EAAE,qBAAqB,CAAC;IAC1C,cAAc,EAAE,kBAAkB,CAAC;CACpC"}
@@ -2,7 +2,7 @@ import type { Kysely } from "kysely";
2
2
  import type { Migration } from "@sharadox/database-web-library/migration/type";
3
3
  /** The table that records which protection migrations a database has applied (apart from the application's own). */
4
4
  export declare const PROTECTION_MIGRATION_TABLE = "protection_migration";
5
- /** The protection tables, forward only: rate-limit buckets and spending-cap usage. */
5
+ /** The protection tables, forward only: rate-limit buckets, spending-cap usage and used challenges. */
6
6
  export declare const protectionMigrations: Migration[];
7
7
  /**
8
8
  * Creates or updates the protection tables, tracked in their own migration table so they never clash with the
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../source/protection-migration/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAErC,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,+CAA+C,CAAC;AAE/E,oHAAoH;AACpH,eAAO,MAAM,0BAA0B,yBAAyB,CAAC;AAEjE,sFAAsF;AACtF,eAAO,MAAM,oBAAoB,EAAE,SAAS,EAkB3C,CAAC;AAEF;;;;;;GAMG;AACH,wBAAgB,iBAAiB,CAAC,MAAM,EAAE,QAAQ,EAAE,MAAM,CAAC,MAAM,CAAC,GAAG,OAAO,CAAC,MAAM,EAAE,CAAC,CAErF"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../source/protection-migration/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAErC,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,+CAA+C,CAAC;AAE/E,oHAAoH;AACpH,eAAO,MAAM,0BAA0B,yBAAyB,CAAC;AAEjE,uGAAuG;AACvG,eAAO,MAAM,oBAAoB,EAAE,SAAS,EA4B3C,CAAC;AAEF;;;;;;GAMG;AACH,wBAAgB,iBAAiB,CAAC,MAAM,EAAE,QAAQ,EAAE,MAAM,CAAC,MAAM,CAAC,GAAG,OAAO,CAAC,MAAM,EAAE,CAAC,CAErF"}
@@ -1,7 +1,7 @@
1
1
  import { migrate } from "@sharadox/database-web-library/migration";
2
2
  /** The table that records which protection migrations a database has applied (apart from the application's own). */
3
3
  export const PROTECTION_MIGRATION_TABLE = "protection_migration";
4
- /** The protection tables, forward only: rate-limit buckets and spending-cap usage. */
4
+ /** The protection tables, forward only: rate-limit buckets, spending-cap usage and used challenges. */
5
5
  export const protectionMigrations = [
6
6
  {
7
7
  name: "0001-rate-limit-and-spending-cap",
@@ -20,6 +20,16 @@ export const protectionMigrations = [
20
20
  );
21
21
  `,
22
22
  },
23
+ {
24
+ name: "0002-used-challenge",
25
+ sql: `
26
+ create table used_challenge (
27
+ id text primary key check (length(id) <= 100),
28
+ expires_at timestamptz not null
29
+ );
30
+ create index used_challenge_expires_at on used_challenge (expires_at);
31
+ `,
32
+ },
23
33
  ];
24
34
  /**
25
35
  * Creates or updates the protection tables, tracked in their own migration table so they never clash with the
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../source/protection-migration/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,OAAO,EAAE,MAAM,0CAA0C,CAAC;AAGnE,oHAAoH;AACpH,MAAM,CAAC,MAAM,0BAA0B,GAAG,sBAAsB,CAAC;AAEjE,sFAAsF;AACtF,MAAM,CAAC,MAAM,oBAAoB,GAAgB;IAC/C;QACE,IAAI,EAAE,kCAAkC;QACxC,GAAG,EAAE;;;;;;;;;;;;;KAaJ;KACF;CACF,CAAC;AAEF;;;;;;GAMG;AACH,MAAM,UAAU,iBAAiB,CAAS,QAAwB;IAChE,OAAO,OAAO,CAAC,QAAQ,EAAE,oBAAoB,EAAE,EAAE,KAAK,EAAE,0BAA0B,EAAE,CAAC,CAAC;AACxF,CAAC"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../source/protection-migration/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,OAAO,EAAE,MAAM,0CAA0C,CAAC;AAGnE,oHAAoH;AACpH,MAAM,CAAC,MAAM,0BAA0B,GAAG,sBAAsB,CAAC;AAEjE,uGAAuG;AACvG,MAAM,CAAC,MAAM,oBAAoB,GAAgB;IAC/C;QACE,IAAI,EAAE,kCAAkC;QACxC,GAAG,EAAE;;;;;;;;;;;;;KAaJ;KACF;IACD;QACE,IAAI,EAAE,qBAAqB;QAC3B,GAAG,EAAE;;;;;;KAMJ;KACF;CACF,CAAC;AAEF;;;;;;GAMG;AACH,MAAM,UAAU,iBAAiB,CAAS,QAAwB;IAChE,OAAO,OAAO,CAAC,QAAQ,EAAE,oBAAoB,EAAE,EAAE,KAAK,EAAE,0BAA0B,EAAE,CAAC,CAAC;AACxF,CAAC"}
@@ -0,0 +1,19 @@
1
+ import type { Kysely } from "kysely";
2
+ import type { Solution, VerifySolutionOptions } from "@sharadox/protection-web-library/proof-of-work/type";
3
+ import type { ProtectionDatabase } from "@sharadox/protection-web-library/protection-database/type";
4
+ /**
5
+ * Checks a proof-of-work solution and spends it: a valid one counts once (its challenge id is kept until the
6
+ * challenge expires), so one solved challenge can't be replayed. Nothing is stored for invalid solutions.
7
+ *
8
+ * @example
9
+ * const verification = await redeemSolution(database, solution, { secret: process.env.PROOF_OF_WORK_SECRET! });
10
+ * if (!verification.valid) return refuse(verification.reason);
11
+ */
12
+ export declare function redeemSolution(database: Kysely<ProtectionDatabase>, solution: Solution, options: VerifySolutionOptions): Promise<{
13
+ valid: true;
14
+ id: string;
15
+ } | {
16
+ valid: false;
17
+ reason: "signature" | "expired" | "work" | "used";
18
+ }>;
19
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../source/solution-redemption/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AACrC,OAAO,KAAK,EAAE,QAAQ,EAAE,qBAAqB,EAAE,MAAM,qDAAqD,CAAC;AAE3G,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,2DAA2D,CAAC;AAEpG;;;;;;;GAOG;AACH,wBAAsB,cAAc,CAClC,QAAQ,EAAE,MAAM,CAAC,kBAAkB,CAAC,EACpC,QAAQ,EAAE,QAAQ,EAClB,OAAO,EAAE,qBAAqB,GAC7B,OAAO,CAAC;IAAE,KAAK,EAAE,IAAI,CAAC;IAAC,EAAE,EAAE,MAAM,CAAA;CAAE,GAAG;IAAE,KAAK,EAAE,KAAK,CAAC;IAAC,MAAM,EAAE,WAAW,GAAG,SAAS,GAAG,MAAM,GAAG,MAAM,CAAA;CAAE,CAAC,CAU5G"}
@@ -0,0 +1,22 @@
1
+ import { verifySolution } from "@sharadox/protection-web-library/proof-of-work";
2
+ /**
3
+ * Checks a proof-of-work solution and spends it: a valid one counts once (its challenge id is kept until the
4
+ * challenge expires), so one solved challenge can't be replayed. Nothing is stored for invalid solutions.
5
+ *
6
+ * @example
7
+ * const verification = await redeemSolution(database, solution, { secret: process.env.PROOF_OF_WORK_SECRET! });
8
+ * if (!verification.valid) return refuse(verification.reason);
9
+ */
10
+ export async function redeemSolution(database, solution, options) {
11
+ const verification = verifySolution(solution, options);
12
+ if (!verification.valid)
13
+ return verification;
14
+ const stored = await database
15
+ .insertInto("used_challenge")
16
+ .values({ id: verification.id, expires_at: new Date(solution.challenge.expiresAt) })
17
+ .onConflict((conflict) => conflict.column("id").doNothing())
18
+ .returning("id")
19
+ .executeTakeFirst();
20
+ return stored ? verification : { valid: false, reason: "used" };
21
+ }
22
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../source/solution-redemption/index.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,cAAc,EAAE,MAAM,gDAAgD,CAAC;AAGhF;;;;;;;GAOG;AACH,MAAM,CAAC,KAAK,UAAU,cAAc,CAClC,QAAoC,EACpC,QAAkB,EAClB,OAA8B;IAE9B,MAAM,YAAY,GAAG,cAAc,CAAC,QAAQ,EAAE,OAAO,CAAC,CAAC;IACvD,IAAI,CAAC,YAAY,CAAC,KAAK;QAAE,OAAO,YAAY,CAAC;IAC7C,MAAM,MAAM,GAAG,MAAM,QAAQ;SAC1B,UAAU,CAAC,gBAAgB,CAAC;SAC5B,MAAM,CAAC,EAAE,EAAE,EAAE,YAAY,CAAC,EAAE,EAAE,UAAU,EAAE,IAAI,IAAI,CAAC,QAAQ,CAAC,SAAS,CAAC,SAAS,CAAC,EAAE,CAAC;SACnF,UAAU,CAAC,CAAC,QAAQ,EAAE,EAAE,CAAC,QAAQ,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,SAAS,EAAE,CAAC;SAC3D,SAAS,CAAC,IAAI,CAAC;SACf,gBAAgB,EAAE,CAAC;IACtB,OAAO,MAAM,CAAC,CAAC,CAAC,YAAY,CAAC,CAAC,CAAC,EAAE,KAAK,EAAE,KAAK,EAAE,MAAM,EAAE,MAAM,EAAE,CAAC;AAClE,CAAC"}
@@ -0,0 +1,13 @@
1
+ import type { Kysely } from "kysely";
2
+ import type { ProtectionDatabase } from "@sharadox/protection-web-library/protection-database/type";
3
+ /**
4
+ * Forgets used challenges that have expired (their solutions are refused as expired anyway), from a daily job.
5
+ * Returns how many were deleted.
6
+ *
7
+ * @example
8
+ * await pruneUsedChallenges(database);
9
+ */
10
+ export declare function pruneUsedChallenges(database: Kysely<ProtectionDatabase>, { now }?: {
11
+ now?: number;
12
+ }): Promise<number>;
13
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../source/used-challenge-prune/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AACrC,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,2DAA2D,CAAC;AAEpG;;;;;;GAMG;AACH,wBAAsB,mBAAmB,CACvC,QAAQ,EAAE,MAAM,CAAC,kBAAkB,CAAC,EACpC,EAAE,GAAgB,EAAE,GAAE;IAAE,GAAG,CAAC,EAAE,MAAM,CAAA;CAAO,GAC1C,OAAO,CAAC,MAAM,CAAC,CAGjB"}
@@ -0,0 +1,12 @@
1
+ /**
2
+ * Forgets used challenges that have expired (their solutions are refused as expired anyway), from a daily job.
3
+ * Returns how many were deleted.
4
+ *
5
+ * @example
6
+ * await pruneUsedChallenges(database);
7
+ */
8
+ export async function pruneUsedChallenges(database, { now = Date.now() } = {}) {
9
+ const result = await database.deleteFrom("used_challenge").where("expires_at", "<", new Date(now)).executeTakeFirst();
10
+ return Number(result.numDeletedRows);
11
+ }
12
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../source/used-challenge-prune/index.ts"],"names":[],"mappings":"AAGA;;;;;;GAMG;AACH,MAAM,CAAC,KAAK,UAAU,mBAAmB,CACvC,QAAoC,EACpC,EAAE,GAAG,GAAG,IAAI,CAAC,GAAG,EAAE,KAAuB,EAAE;IAE3C,MAAM,MAAM,GAAG,MAAM,QAAQ,CAAC,UAAU,CAAC,gBAAgB,CAAC,CAAC,KAAK,CAAC,YAAY,EAAE,GAAG,EAAE,IAAI,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC,gBAAgB,EAAE,CAAC;IACtH,OAAO,MAAM,CAAC,MAAM,CAAC,cAAc,CAAC,CAAC;AACvC,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sharadox/protection-web-library",
3
- "version": "0.1.0-development.6",
3
+ "version": "0.1.0-development.9",
4
4
  "description": "Protection for web forms and APIs: layered rate limits, proof-of-work challenges and spending caps.",
5
5
  "private": false,
6
6
  "license": "SEE LICENSE IN LICENCA.md",