@rehearsal-db/core 0.1.0-beta.1 → 0.1.0-beta.11

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.
Files changed (72) hide show
  1. package/CHANGELOG.md +271 -1
  2. package/COMPATIBILITY.md +26 -0
  3. package/README.md +150 -346
  4. package/SECURITY.md +2 -1
  5. package/SUPPORT.md +5 -0
  6. package/docs/README.md +38 -0
  7. package/docs/adapters.md +20 -6
  8. package/docs/architecture.md +58 -0
  9. package/docs/baselines.md +102 -22
  10. package/docs/commands.md +202 -33
  11. package/docs/configuration.md +288 -13
  12. package/docs/getting-started.md +114 -181
  13. package/docs/glossary.md +11 -8
  14. package/docs/index.md +47 -0
  15. package/docs/production-source.md +176 -33
  16. package/docs/releasing.md +28 -38
  17. package/docs/roadmap.md +41 -0
  18. package/docs/runtime-policies.md +253 -0
  19. package/docs/sanitization.md +144 -8
  20. package/docs/security-model.md +34 -9
  21. package/docs/standalone-workflow.md +109 -0
  22. package/docs/troubleshooting.md +127 -2
  23. package/docs/tutorial.md +70 -59
  24. package/package.json +39 -23
  25. package/scripts/install/create_root_config.mjs +36 -0
  26. package/scripts/runtime/manage_database.mjs +18 -0
  27. package/src/README.md +17 -0
  28. package/src/application/session.mjs +435 -0
  29. package/{scripts/lib/rehearsal/baseline_artifact.mjs → src/baseline/artifact.mjs} +142 -17
  30. package/{scripts/lib/rehearsal/baseline_builder.mjs → src/baseline/builder.mjs} +19 -10
  31. package/src/baseline/input_discovery.mjs +155 -0
  32. package/src/baseline/policy_review.mjs +92 -0
  33. package/src/baseline/preparation.mjs +323 -0
  34. package/src/baseline/privacy_engine.mjs +741 -0
  35. package/{scripts/lib/rehearsal → src/baseline}/sanitization_policy.mjs +133 -3
  36. package/{scripts/lib/rehearsal → src/baseline}/schema_snapshot.mjs +1 -1
  37. package/src/cli/arguments.mjs +123 -0
  38. package/src/cli/guided.mjs +812 -0
  39. package/src/cli/init_command.mjs +77 -0
  40. package/src/cli/rehearsal.mjs +952 -0
  41. package/src/cli/renderers.mjs +589 -0
  42. package/src/cli/runtime_commands.mjs +624 -0
  43. package/src/cli/source_commands.mjs +326 -0
  44. package/src/cli/terminal.mjs +275 -0
  45. package/src/identity/claim.mjs +1163 -0
  46. package/src/identity/storage.mjs +165 -0
  47. package/{scripts/lib/rehearsal → src/project}/configuration.d.mts +72 -4
  48. package/src/project/configuration.mjs +1201 -0
  49. package/src/project/install_scaffold.mjs +291 -0
  50. package/src/project/setup.mjs +484 -0
  51. package/src/project/support_report.mjs +105 -0
  52. package/src/runtime/cleanup.mjs +381 -0
  53. package/{scripts/lib/rehearsal → src/runtime}/plan.mjs +118 -20
  54. package/src/runtime/policy.mjs +438 -0
  55. package/{scripts/lib/rehearsal/runtime_restore.mjs → src/runtime/restore.mjs} +46 -27
  56. package/src/runtime/topology.mjs +177 -0
  57. package/{scripts/lib/rehearsal → src/shared}/diagnostics.mjs +10 -3
  58. package/src/shared/human_output.mjs +4 -0
  59. package/src/shared/operation_guard.mjs +162 -0
  60. package/{scripts/lib/rehearsal → src/shared}/process_environment.mjs +5 -1
  61. package/src/source/access.mjs +578 -0
  62. package/src/source/asset_transfer.mjs +177 -0
  63. package/src/source/baseline.mjs +446 -0
  64. package/src/source/postgresql_access.mjs +480 -0
  65. package/src/targets/postgresql.mjs +808 -0
  66. package/{scripts/operations/database/manage_rehearsal_database.mjs → src/targets/supabase.mjs} +66 -20
  67. package/{scripts/lib/environment/local_supabase.mjs → src/targets/supabase_environment.mjs} +60 -28
  68. package/src/targets/target.mjs +66 -0
  69. package/scripts/lib/rehearsal/configuration.mjs +0 -559
  70. package/scripts/operations/rehearsal/rehearsal_cli.mjs +0 -565
  71. /package/{scripts/lib/rehearsal → src/runtime}/migration_history.mjs +0 -0
  72. /package/{scripts/lib/rehearsal → src/runtime}/service_environment.mjs +0 -0
@@ -0,0 +1,165 @@
1
+ /** Copy and verify local Supabase Storage objects during an identity claim. */
2
+
3
+ import { createHash } from "node:crypto";
4
+ import { assertLoopbackUrl } from "../shared/process_environment.mjs";
5
+
6
+ const BUCKET = /^[a-z0-9][a-z0-9.-]{0,99}$/u;
7
+ const MAXIMUM_VERIFICATION_BYTES = 100 * 1024 * 1024;
8
+ const REQUEST_TIMEOUT_MS = 30_000;
9
+
10
+ const objectPath = (value) => {
11
+ if (
12
+ typeof value !== "string" ||
13
+ !value ||
14
+ value.startsWith("/") ||
15
+ value.split("/").some((part) => !part || part === "." || part === "..")
16
+ ) {
17
+ throw new Error("A Storage identity-claim path is unsafe.");
18
+ }
19
+ return value.split("/").map(encodeURIComponent).join("/");
20
+ };
21
+
22
+ const readVerifiedObject = async ({
23
+ fetchImpl,
24
+ baseUrl,
25
+ headers,
26
+ bucket,
27
+ path,
28
+ }) => {
29
+ const response = await fetchImpl(
30
+ `${baseUrl}/storage/v1/object/${encodeURIComponent(bucket)}/${objectPath(path)}?rehearsal=${Date.now()}`,
31
+ {
32
+ headers,
33
+ cache: "no-store",
34
+ signal: AbortSignal.timeout(REQUEST_TIMEOUT_MS),
35
+ },
36
+ );
37
+ if (!response.ok) {
38
+ throw new Error(
39
+ `Supabase Storage could not read an identity-claim object (HTTP ${response.status}).`,
40
+ );
41
+ }
42
+ const declaredBytes = Number(response.headers.get("content-length"));
43
+ if (
44
+ Number.isFinite(declaredBytes) &&
45
+ declaredBytes > MAXIMUM_VERIFICATION_BYTES
46
+ ) {
47
+ throw new Error(
48
+ "A Storage identity-claim object exceeds the 100 MiB verification limit.",
49
+ );
50
+ }
51
+ const bytes = Buffer.from(await response.arrayBuffer());
52
+ if (bytes.length > MAXIMUM_VERIFICATION_BYTES) {
53
+ throw new Error(
54
+ "A Storage identity-claim object exceeds the 100 MiB verification limit.",
55
+ );
56
+ }
57
+ return Object.freeze({
58
+ bytes: bytes.length,
59
+ sha256: createHash("sha256").update(bytes).digest("hex"),
60
+ });
61
+ };
62
+
63
+ export const createSupabaseStorageTransfer = ({
64
+ url,
65
+ serviceRoleKey,
66
+ fetchImpl = globalThis.fetch,
67
+ }) => {
68
+ assertLoopbackUrl("Rehearsal Supabase Storage", url);
69
+ if (typeof serviceRoleKey !== "string" || !serviceRoleKey) {
70
+ throw new Error(
71
+ "The local runtime environment has no Storage service key.",
72
+ );
73
+ }
74
+ if (typeof fetchImpl !== "function") {
75
+ throw new Error("A Fetch implementation is required for Storage transfer.");
76
+ }
77
+ const baseUrl = url.replace(/\/$/u, "");
78
+ const headers = Object.freeze({
79
+ apikey: serviceRoleKey,
80
+ authorization: `Bearer ${serviceRoleKey}`,
81
+ });
82
+ const removePaths = async ({ bucket, paths }) => {
83
+ if (!BUCKET.test(bucket ?? "") || !Array.isArray(paths) || !paths.length) {
84
+ throw new Error("A Storage identity-claim removal is invalid.");
85
+ }
86
+ paths.forEach(objectPath);
87
+ const response = await fetchImpl(
88
+ `${baseUrl}/storage/v1/object/${encodeURIComponent(bucket)}`,
89
+ {
90
+ method: "DELETE",
91
+ headers: { ...headers, "content-type": "application/json" },
92
+ body: JSON.stringify({ prefixes: paths }),
93
+ signal: AbortSignal.timeout(REQUEST_TIMEOUT_MS),
94
+ },
95
+ );
96
+ if (!response.ok) {
97
+ throw new Error(
98
+ `Supabase Storage could not remove identity-claim objects (HTTP ${response.status}).`,
99
+ );
100
+ }
101
+ };
102
+ return Object.freeze({
103
+ async copyAndVerify({ bucket, source, destination }) {
104
+ if (!BUCKET.test(bucket ?? "")) {
105
+ throw new Error("A Storage identity-claim bucket is invalid.");
106
+ }
107
+ objectPath(source);
108
+ objectPath(destination);
109
+ if (source === destination) {
110
+ throw new Error("A Storage identity-claim copy must change the path.");
111
+ }
112
+ const sourceReceipt = await readVerifiedObject({
113
+ fetchImpl,
114
+ baseUrl,
115
+ headers,
116
+ bucket,
117
+ path: source,
118
+ });
119
+ try {
120
+ const response = await fetchImpl(`${baseUrl}/storage/v1/object/copy`, {
121
+ method: "POST",
122
+ headers: { ...headers, "content-type": "application/json" },
123
+ body: JSON.stringify({
124
+ bucketId: bucket,
125
+ sourceKey: source,
126
+ destinationKey: destination,
127
+ }),
128
+ signal: AbortSignal.timeout(REQUEST_TIMEOUT_MS),
129
+ });
130
+ if (!response.ok) {
131
+ throw new Error(
132
+ `Supabase Storage could not copy an identity-claim object (HTTP ${response.status}).`,
133
+ );
134
+ }
135
+ const destinationReceipt = await readVerifiedObject({
136
+ fetchImpl,
137
+ baseUrl,
138
+ headers,
139
+ bucket,
140
+ path: destination,
141
+ });
142
+ if (
143
+ sourceReceipt.bytes !== destinationReceipt.bytes ||
144
+ sourceReceipt.sha256 !== destinationReceipt.sha256
145
+ ) {
146
+ throw new Error(
147
+ "Supabase Storage copied an identity object with a different byte digest.",
148
+ );
149
+ }
150
+ return destinationReceipt;
151
+ } catch (error) {
152
+ try {
153
+ await removePaths({ bucket, paths: [destination] });
154
+ } catch (cleanupError) {
155
+ throw new AggregateError(
156
+ [error, cleanupError],
157
+ "Storage identity-copy verification failed and destination cleanup needs attention.",
158
+ );
159
+ }
160
+ throw error;
161
+ }
162
+ },
163
+ remove: removePaths,
164
+ });
165
+ };
@@ -3,7 +3,7 @@ export type RehearsalConfigVersion = 1;
3
3
  export interface RehearsalConfig {
4
4
  schemaVersion: RehearsalConfigVersion;
5
5
  project: { name: string };
6
- supabase: {
6
+ supabase?: {
7
7
  workdir: string;
8
8
  migrationDirectory: string;
9
9
  rehearsalConfig: string;
@@ -11,22 +11,70 @@ export interface RehearsalConfig {
11
11
  serviceEnvironmentFile?: string;
12
12
  serviceEnvironmentVariables?: string[];
13
13
  };
14
+ postgresql?: {
15
+ migrationDirectory: string;
16
+ runtimeWorkdir?: string;
17
+ image?: string;
18
+ database?: string;
19
+ user?: string;
20
+ };
14
21
  baseline: {
15
22
  artifactDirectory?: string;
16
23
  sanitizationPolicy: string;
17
24
  };
25
+ containerRuntime?: {
26
+ autoStartColima?: boolean;
27
+ };
28
+ cleanup?: {
29
+ retainBaselineGenerations?: number;
30
+ };
31
+ preparation?: {
32
+ sourcePolicy: string;
33
+ privacyKey: string;
34
+ batchRows?: number;
35
+ maximumRows?: number;
36
+ maximumBytes?: number;
37
+ diskHeadroomBytes?: number;
38
+ };
39
+ runtimePolicy?: string;
40
+ identityPolicy?: string;
41
+ dependentTargets?: Array<{
42
+ name: string;
43
+ configPath: string;
44
+ prepareCommand?: string;
45
+ }>;
18
46
  application: {
19
47
  startCommand: string;
20
48
  proofCommand: string;
21
49
  environmentFile?: string;
50
+ environmentVariables?: Record<string, string>;
51
+ readiness?: {
52
+ url: string;
53
+ expectedStatus: number;
54
+ timeoutSeconds?: number;
55
+ };
56
+ httpProofs?: Array<{
57
+ name: string;
58
+ kind: "positive" | "negative";
59
+ url: string;
60
+ method?: string;
61
+ expectedStatus: number;
62
+ json?: {
63
+ path: string[];
64
+ equals?: unknown;
65
+ notEquals?: unknown;
66
+ minimumItems?: number;
67
+ };
68
+ }>;
22
69
  runtimeAdapter?: string;
23
70
  };
24
71
  runtime: {
72
+ target?: "supabase" | "postgresql";
25
73
  applicationUrl?: string;
26
74
  projectId?: string;
27
- apiPort: number;
75
+ apiPort?: number;
28
76
  databasePort: number;
29
- studioPort: number;
77
+ studioPort?: number;
30
78
  };
31
79
  safety?: {
32
80
  allowedHosts?: string[];
@@ -51,7 +99,20 @@ export declare const loadRehearsalConfig: (options?: {
51
99
  export declare const inspectDetectedProject: (options?: {
52
100
  projectRoot?: string;
53
101
  }) => Promise<unknown>;
54
- export declare const renderDetectedConfig: (detected: unknown) => string;
102
+ export declare const renderDetectedConfig: (
103
+ detected: unknown,
104
+ options?: {
105
+ applicationUrl?: string;
106
+ ports?: { api: number; database: number; studio: number };
107
+ },
108
+ ) => string;
109
+ export declare const renderDetectedPostgresqlConfig: (
110
+ detected: unknown,
111
+ options?: {
112
+ applicationUrl?: string;
113
+ databasePort?: number;
114
+ },
115
+ ) => string;
55
116
  export declare const SANITIZATION_ACTIONS: Readonly<{
56
117
  KEEP: "KEEP";
57
118
  PSEUDONYMIZE: "PSEUDONYMIZE";
@@ -75,6 +136,13 @@ export declare const validateSanitizationCoverage: (options: {
75
136
  columnCount: number;
76
137
  tables: ReadonlyArray<Record<string, unknown>>;
77
138
  }>;
139
+ export declare const validateRuntimeSanitizationPolicy: (
140
+ policy: unknown,
141
+ ) => unknown;
142
+ export declare const readBoundRuntimeSanitizationPolicy: (options: {
143
+ bytes: Uint8Array | string;
144
+ expectedSha256: string;
145
+ }) => unknown;
78
146
  export declare const applySanitizationAction: (options: {
79
147
  action: string;
80
148
  value: unknown;