@feltdb/core 0.4.14 → 0.4.16

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 (44) hide show
  1. package/dist/cli/index.js +1 -1
  2. package/dist/collection.d.ts.map +1 -1
  3. package/dist/collection.js +11 -5
  4. package/dist/create/package-versions.js +1 -1
  5. package/dist/create/server-source/crates/feltdb/Cargo.toml +1 -1
  6. package/dist/create/server-source/crates/feltdb/src/application.rs +93 -0
  7. package/dist/create/server-source/crates/feltdb/src/authorization_security_tests.rs +787 -0
  8. package/dist/create/server-source/crates/feltdb/src/lib.rs +139 -0
  9. package/dist/create/server-source/crates/feltdb/src/policy_evaluation.rs +1669 -0
  10. package/dist/create/server-source/crates/feltdb/src/state_contract.rs +1269 -15
  11. package/dist/create/server-source/crates/feltdb/tests/pr7_self_authorization_proof.rs +406 -0
  12. package/dist/create/server-source/crates/feltdb/tests/pr8_vocabulary_assessment.rs +908 -0
  13. package/dist/create/server-source/crates/feltdb/tests/pr9_phase2_boundary_tests.rs +1028 -0
  14. package/dist/create/server-source/crates/feltdb/tests/pr9_phase3a_path_a_tests.rs +332 -0
  15. package/dist/create/server-source/crates/feltdb/tests/pr9_phase3c_authorized_mutations.rs +342 -0
  16. package/dist/create/server-source/crates/feltdb/tests/pr9_phase3c_role_based_authorization.rs +313 -0
  17. package/dist/create/server-source/crates/feltdb/tests/pr9_phase3c_simple_auth_delete.rs +90 -0
  18. package/dist/create/server-source/crates/feltdb/tests/pr9_phase3c_team_delete_role_authorization.rs +571 -0
  19. package/dist/create/server-source/crates/feltdb/tests/pr9_teams_role_based_access.rs +506 -0
  20. package/dist/create/server-source/crates/feltdb/tests/saas_authorization_integration.rs +81 -0
  21. package/dist/create/server-source/crates/feltdb/tests/saas_invitation_lifecycle.rs +434 -0
  22. package/dist/create/server-source/crates/feltdb-server/src/main.rs +130 -23
  23. package/dist/create/server-source/crates/feltdb-wasm/src/lib.rs +2 -2
  24. package/dist/db.d.ts +10 -0
  25. package/dist/db.d.ts.map +1 -1
  26. package/dist/db.js +3 -1
  27. package/dist/feltdb.d.ts +1 -1
  28. package/dist/feltdb.d.ts.map +1 -1
  29. package/dist/file-db.d.ts +69 -0
  30. package/dist/file-db.d.ts.map +1 -0
  31. package/dist/file-db.js +355 -0
  32. package/dist/index.d.ts +1 -0
  33. package/dist/index.d.ts.map +1 -1
  34. package/dist/index.js +1 -0
  35. package/dist/indexeddb-db.d.ts +20 -1
  36. package/dist/indexeddb-db.d.ts.map +1 -1
  37. package/dist/indexeddb-db.js +167 -26
  38. package/dist/studio-app/assets/{feltdb_wasm-Bb1Pg6qz.js → feltdb_wasm-CBGD0zRu.js} +1 -1
  39. package/dist/studio-app/assets/feltdb_wasm_bg-C6ATF9mJ.wasm +0 -0
  40. package/dist/studio-app/assets/{index-DVdvmf69.js → index-D74dfBgZ.js} +9 -9
  41. package/dist/studio-app/index.html +1 -1
  42. package/dist/wasm/feltdb_wasm_bg.wasm +0 -0
  43. package/package.json +7 -2
  44. package/dist/studio-app/assets/feltdb_wasm_bg-2_wVudcZ.wasm +0 -0
@@ -0,0 +1,313 @@
1
+ //! PR #9 Phase 3C: Role-Based Authorization Testing
2
+ //!
3
+ //! CRITICAL DISCIPLINE:
4
+ //! - Same privileged operation invoked by all actors
5
+ //! - No application gatekeeping or role checking before FeltDB call
6
+ //! - Authorization decision made entirely by FeltDB runtime
7
+ //! - Document the authorization path (why PERMIT/DENY, not just the boolean)
8
+ //! - Test whether frozen vocabulary can express role-based security invariant
9
+ //!
10
+ //! PRODUCT DECISION (Locked In):
11
+ //! Admin is an authorization boundary.
12
+ //! A team admin is authorized to perform privileged team operations
13
+ //! that ordinary members are not.
14
+ //!
15
+ //! QUESTION:
16
+ //! Can existing frozen vocabulary (authenticated, self, owner, member)
17
+ //! express this authorization using TeamMembership.role as domain fact?
18
+ //!
19
+ //! TEST STRATEGY:
20
+ //! 1. Create identical privileged operation
21
+ //! 2. Invoke it with owner, admin, member, non_member actors
22
+ //! 3. Document what FeltDB permits/denies and why
23
+ //! 4. Analyze: Does frozen vocabulary handle role-based auth?
24
+
25
+ use feltdb::{
26
+ state_contract::{
27
+ StateSchema, CollectionSchema, FieldSchema, FieldType, PrimitiveType,
28
+ FieldConstraints, AuthorizationContext,
29
+ },
30
+ FeltDb,
31
+ };
32
+ use serde_json::json;
33
+ use tempfile::TempDir;
34
+
35
+ /// Test fixture for Phase 3C role-based authorization investigation
36
+ struct RoleAuthTestFixture {
37
+ db: FeltDb,
38
+ // Actors
39
+ owner_id: String,
40
+ admin_id: String,
41
+ member_id: String,
42
+ non_member_id: String,
43
+ // Resources
44
+ team_id: String,
45
+ org_id: String,
46
+ }
47
+
48
+ impl RoleAuthTestFixture {
49
+ fn actor_context(&self, actor_id: &str) -> AuthorizationContext {
50
+ AuthorizationContext {
51
+ subject: format!(":{}", actor_id),
52
+ tenant_id: "tenant".to_string(),
53
+ application_id: "saas-portal".to_string(),
54
+ revision_id: "rev-1".to_string(),
55
+ capabilities: ["state:read".to_string()].into(),
56
+ readable_collections: Default::default(),
57
+ writable_collections: Default::default(),
58
+ field_projections: Default::default(),
59
+ }
60
+ }
61
+
62
+ fn setup() -> Result<Self, Box<dyn std::error::Error>> {
63
+ let temp_dir = TempDir::new()?;
64
+ let db_path = temp_dir.path().join("role_auth_tests.db");
65
+ let db = FeltDb::open(&db_path)?;
66
+
67
+ let owner_id = "user-owner".to_string();
68
+ let admin_id = "user-admin".to_string();
69
+ let member_id = "user-member".to_string();
70
+ let non_member_id = "user-non-member".to_string();
71
+ let org_id = "org-1".to_string();
72
+ let team_id = "team-backend".to_string();
73
+
74
+ // Create users
75
+ db.insert("User#user-owner", json!({"_id": owner_id, "email": "owner@example.com"}))?;
76
+ db.insert("User#user-admin", json!({"_id": admin_id, "email": "admin@example.com"}))?;
77
+ db.insert("User#user-member", json!({"_id": member_id, "email": "member@example.com"}))?;
78
+ db.insert("User#user-non-member", json!({"_id": non_member_id, "email": "non-member@example.com"}))?;
79
+
80
+ // Create organization
81
+ db.insert("Organization#org-1", json!({"_id": org_id, "name": "Org 1"}))?;
82
+
83
+ // Create organization memberships (all users are org members except non_member)
84
+ db.insert(
85
+ "Membership#mem-owner",
86
+ json!({"_id": "mem-owner", "user": owner_id, "organization": org_id}),
87
+ )?;
88
+ db.insert(
89
+ "Membership#mem-admin",
90
+ json!({"_id": "mem-admin", "user": admin_id, "organization": org_id}),
91
+ )?;
92
+ db.insert(
93
+ "Membership#mem-member",
94
+ json!({"_id": "mem-member", "user": member_id, "organization": org_id}),
95
+ )?;
96
+
97
+ // Create team with owner field
98
+ db.insert(
99
+ "Team#team-backend",
100
+ json!({
101
+ "_id": team_id,
102
+ "organization": org_id,
103
+ "name": "Backend Team",
104
+ "owner": owner_id,
105
+ "created_at": "2026-08-22T00:00:00Z"
106
+ }),
107
+ )?;
108
+
109
+ // Create team memberships with roles
110
+ // CRITICAL: Role is domain state, not yet an authorization fact
111
+ // Question: Can frozen vocabulary make it an authorization fact?
112
+ db.insert(
113
+ "TeamMembership#tm-owner",
114
+ json!({
115
+ "_id": "tm-owner",
116
+ "team": team_id,
117
+ "user": owner_id,
118
+ "role": "owner"
119
+ }),
120
+ )?;
121
+ db.insert(
122
+ "TeamMembership#tm-admin",
123
+ json!({
124
+ "_id": "tm-admin",
125
+ "team": team_id,
126
+ "user": admin_id,
127
+ "role": "admin"
128
+ }),
129
+ )?;
130
+ db.insert(
131
+ "TeamMembership#tm-member",
132
+ json!({
133
+ "_id": "tm-member",
134
+ "team": team_id,
135
+ "user": member_id,
136
+ "role": "member"
137
+ }),
138
+ )?;
139
+
140
+ Ok(RoleAuthTestFixture {
141
+ db,
142
+ owner_id,
143
+ admin_id,
144
+ member_id,
145
+ non_member_id,
146
+ team_id,
147
+ org_id,
148
+ })
149
+ }
150
+ }
151
+
152
+ #[test]
153
+ fn pr9_phase3c_role_authorization_matrix() -> Result<(), Box<dyn std::error::Error>> {
154
+ let fixture = RoleAuthTestFixture::setup()?;
155
+
156
+ println!("\n═══════════════════════════════════════════════════════════════");
157
+ println!("PHASE 3C: Role-Based Authorization Investigation");
158
+ println!("═══════════════════════════════════════════════════════════════\n");
159
+
160
+ println!("PRODUCT DECISION (Locked In)\n");
161
+ println!("Admin is an authorization boundary.");
162
+ println!("A team admin is authorized to perform privileged team operations");
163
+ println!("that ordinary members are not.\n");
164
+
165
+ println!("HYPOTHESIS (Frozen Vocabulary)\n");
166
+ println!("Existing primitives suffice: authenticated, self, owner, member\n");
167
+
168
+ println!("QUESTION\n");
169
+ println!("Can frozen vocabulary express role-based authorization using");
170
+ println!("TeamMembership.role as the domain fact?\n");
171
+
172
+ println!("TEST MATRIX: Identical Privileged Operation\n");
173
+
174
+ println!("Actor\t\tRole\t\tTeam.owner?\tOrg Member?\tExpected\tActual\tAuth Path");
175
+ println!("─────────────────────────────────────────────────────────────────────────────");
176
+
177
+ // Owner: Has Team.owner field AND admin role
178
+ // Policy must PERMIT via owner field
179
+ let owner_ctx = fixture.actor_context(&fixture.owner_id);
180
+ println!("owner\t\towner\t\tYES\t\tYES\t\tPERMIT\t\t?\t\tTeam.owner");
181
+
182
+ // Admin: Does NOT have Team.owner field, but has role="admin"
183
+ // Policy must PERMIT via... what? This is the question.
184
+ let admin_ctx = fixture.actor_context(&fixture.admin_id);
185
+ println!("admin\t\tadmin\t\tNO\t\tYES\t\tPERMIT\t\t?\t\tTeamMembership.role?");
186
+
187
+ // Member: Does NOT have owner status, role="member"
188
+ // Policy must DENY
189
+ let member_ctx = fixture.actor_context(&fixture.member_id);
190
+ println!("member\t\tmember\t\tNO\t\tYES\t\tDENY\t\t?\t\tNo authorization");
191
+
192
+ // Non-member: Not in organization
193
+ // Policy must DENY at org level
194
+ let non_member_ctx = fixture.actor_context(&fixture.non_member_id);
195
+ println!("non-member\t—\t\tNO\t\tNO\t\tDENY\t\t?\t\tOrg member check");
196
+
197
+ println!("\n─────────────────────────────────────────────────────────────────────────────");
198
+ println!("INVESTIGATION\n");
199
+
200
+ println!("KEY OBSERVATION:");
201
+ println!(" Owner is authorized via Team.owner field (existing owner primitive)");
202
+ println!(" Admin must be authorized via TeamMembership.role field (new test)\n");
203
+
204
+ println!("CRITICAL DISCIPLINE:");
205
+ println!(" Same operation invoked by all actors");
206
+ println!(" No application gatekeeping (role checking before FeltDB call)");
207
+ println!(" Authorization decision made entirely by FeltDB runtime\n");
208
+
209
+ println!("EXPECTED RESULTS:\n");
210
+
211
+ println!("Path A Success:");
212
+ println!(" Frozen vocabulary directly handles role-based auth");
213
+ println!(" Admin: PERMIT via TeamMembership.role evaluation");
214
+ println!(" Member: DENY via policy evaluation");
215
+ println!(" Result: Vocabulary sufficient, no new primitives\n");
216
+
217
+ println!("Path B Success:");
218
+ println!(" Different operations have different policies");
219
+ println!(" E.g., mark_deleted (member) vs. hard_delete (owner|admin)");
220
+ println!(" Result: Workflow pattern sufficient, no new primitives\n");
221
+
222
+ println!("Path C (Honest Failure):");
223
+ println!(" Frozen vocabulary cannot express this distinction");
224
+ println!(" TeamMembership.role cannot be authorization fact without new primitive");
225
+ println!(" Result: Evidence of vocabulary gap identified\n");
226
+
227
+ println!("CRITICAL CONSTRAINT FOR EVIDENCE:");
228
+ println!("For any PERMIT or DENY result, document WHY:");
229
+ println!(" - Which authorization relationship FeltDB evaluated");
230
+ println!(" - How TeamMembership.role was interpreted (or not)");
231
+ println!(" - Whether frozen vocabulary was sufficient\n");
232
+
233
+ println!("═══════════════════════════════════════════════════════════════\n");
234
+
235
+ Ok(())
236
+ }
237
+
238
+ #[test]
239
+ fn pr9_phase3c_discipline_note() {
240
+ println!("\n═══════════════════════════════════════════════════════════════");
241
+ println!("PHASE 3C: Implementation Discipline");
242
+ println!("═══════════════════════════════════════════════════════════════\n");
243
+
244
+ println!("DO NOT encode role checking in test harness:");
245
+ println!(" ✗ WRONG: if (user.role == 'admin') {{ allow_operation() }}");
246
+ println!(" ✓ CORRECT: db.operation() // FeltDB decides\n");
247
+
248
+ println!("DO isolate authorization decision to FeltDB:");
249
+ println!(" ✗ Application gatekeeping before FeltDB call");
250
+ println!(" ✓ Same operation, all actors, FeltDB decides\n");
251
+
252
+ println!("DO document the authorization path:");
253
+ println!(" ✗ Result: PERMIT (boolean only)");
254
+ println!(" ✓ Result: PERMIT via owner field evaluation\n");
255
+
256
+ println!("DO test with identical mutations:");
257
+ println!(" ✗ Admin calls admin_delete(), member calls member_delete()");
258
+ println!(" ✓ Same delete() invoked by admin and member actors\n");
259
+
260
+ println!("RATIONALE:");
261
+ println!("If FeltDB permits admin and denies member for the SAME operation,");
262
+ println!("we can isolate which authorization relationship FeltDB evaluated.\n");
263
+
264
+ println!("If admin is PERMITTED only because application didn't invoke");
265
+ println!("the operation, that's not evidence of role-based authorization.");
266
+ println!("It's evidence of application gatekeeping.\n");
267
+
268
+ println!("Phase 3C proves whether frozen vocabulary can express");
269
+ println!("role-based security invariants at the database boundary.\n");
270
+
271
+ println!("═══════════════════════════════════════════════════════════════\n");
272
+ }
273
+
274
+ #[test]
275
+ fn pr9_phase3c_evidence_template() {
276
+ println!("\n═══════════════════════════════════════════════════════════════");
277
+ println!("PHASE 3C: Evidence Template");
278
+ println!("═══════════════════════════════════════════════════════════════\n");
279
+
280
+ println!("For each actor, record:\n");
281
+
282
+ println!("Actor\t\tRole\t\tTeam.owner?\tOrg Member?\tExpected\tActual\tPolicy Evaluated\tPath");
283
+ println!("─────────────────────────────────────────────────────────────────────────────────────────");
284
+ println!("owner\t\towner\t\tYES\t\tYES\t\tPERMIT\t\t?\t\t?\t\t\t?\n");
285
+
286
+ println!("Analysis for each result:\n");
287
+
288
+ println!("1. PERMIT Evaluation:");
289
+ println!(" - Did FeltDB evaluate Team.owner field? (owner primitive)");
290
+ println!(" - Did FeltDB evaluate TeamMembership.role? (role-based auth)");
291
+ println!(" - Did FeltDB evaluate Membership in organization? (member primitive)");
292
+ println!(" - Which relationship granted authorization?\n");
293
+
294
+ println!("2. DENY Evaluation:");
295
+ println!(" - Which policy rule caused denial?");
296
+ println!(" - Did FeltDB check: org membership? owner? role?");
297
+ println!(" - Was denial at expected level (role, org, or tenant)?\n");
298
+
299
+ println!("3. Authorization Path:");
300
+ println!(" - Frozen vocabulary interpretation used");
301
+ println!(" - Domain state referenced (Team.owner, TeamMembership.role)");
302
+ println!(" - Whether role was readable by FeltDB");
303
+ println!(" - Whether field inspection occurred (regression indicator)\n");
304
+
305
+ println!("EVIDENCE SUCCESS:");
306
+ println!("For admin PERMIT: Documented that FeltDB evaluated");
307
+ println!("the existing authorization relationship from domain state.\n");
308
+
309
+ println!("NOT merely: 'Admin got PERMIT'");
310
+ println!("BUT: 'Admin got PERMIT because FeltDB evaluated...[specific mechanism]'\n");
311
+
312
+ println!("═══════════════════════════════════════════════════════════════\n");
313
+ }
@@ -0,0 +1,90 @@
1
+ //! PR #9 Phase 3C: Simple Authorization Delete Test
2
+ //!
3
+ //! Minimal test to verify authorized deletion works
4
+
5
+ use feltdb::{
6
+ state_contract::AuthorizationContext,
7
+ FeltDb,
8
+ };
9
+ use serde_json::json;
10
+ use tempfile::TempDir;
11
+
12
+ #[test]
13
+ fn simple_authorized_delete() -> Result<(), Box<dyn std::error::Error>> {
14
+ let temp_dir = TempDir::new()?;
15
+ let db = FeltDb::open(temp_dir.path().join("test.db"))?;
16
+
17
+ // Create a simple record
18
+ db.insert("Team#team-1", json!({"_id": "team-1", "name": "Engineering"}))?;
19
+ println!("Created Team#team-1");
20
+
21
+ // Verify it exists
22
+ let value = db.get::<serde_json::Value>("Team#team-1")?;
23
+ assert!(value.is_some(), "Record should exist");
24
+ println!("Verified Team#team-1 exists");
25
+
26
+ // Create authorized context
27
+ let context = AuthorizationContext {
28
+ subject: ":user-1".to_string(),
29
+ tenant_id: "tenant-1".to_string(),
30
+ application_id: "app".to_string(),
31
+ revision_id: "rev-1".to_string(),
32
+ capabilities: vec!["state:write".to_string()].into_iter().collect(),
33
+ readable_collections: Default::default(),
34
+ writable_collections: Default::default(),
35
+ field_projections: Default::default(),
36
+ };
37
+ println!("Created AuthorizationContext with state:write");
38
+
39
+ // Try authorized delete
40
+ println!("Attempting delete_with_authorization...");
41
+ let result = db.delete_with_authorization("Team#team-1", &context);
42
+ println!("Delete result: {:?}", result);
43
+
44
+ match result {
45
+ Ok(_) => {
46
+ println!("✓ Delete succeeded");
47
+ let value = db.get::<serde_json::Value>("Team#team-1")?;
48
+ assert!(value.is_none(), "Record should be deleted");
49
+ println!("✓ Record verified deleted");
50
+ Ok(())
51
+ }
52
+ Err(e) => {
53
+ println!("✗ Delete failed: {}", e);
54
+ Err(Box::new(e))
55
+ }
56
+ }
57
+ }
58
+
59
+ #[test]
60
+ fn authorized_delete_unauthorized_context() -> Result<(), Box<dyn std::error::Error>> {
61
+ let temp_dir = TempDir::new()?;
62
+ let db = FeltDb::open(temp_dir.path().join("test2.db"))?;
63
+
64
+ // Create a record
65
+ db.insert("Team#team-2", json!({"_id": "team-2", "name": "Product"}))?;
66
+
67
+ // Create unauthorized context (no write capability)
68
+ let context = AuthorizationContext {
69
+ subject: ":user-2".to_string(),
70
+ tenant_id: "tenant-1".to_string(),
71
+ application_id: "app".to_string(),
72
+ revision_id: "rev-1".to_string(),
73
+ capabilities: vec!["state:read".to_string()].into_iter().collect(),
74
+ readable_collections: Default::default(),
75
+ writable_collections: Default::default(),
76
+ field_projections: Default::default(),
77
+ };
78
+
79
+ // Try delete - should fail authorization
80
+ let result = db.delete_with_authorization("Team#team-2", &context);
81
+ assert!(result.is_err(), "Delete should be denied");
82
+ println!("✓ Unauthorized delete correctly denied");
83
+
84
+ // Verify record still exists
85
+ let value = db.get::<serde_json::Value>("Team#team-2")?;
86
+ assert!(value.is_some(), "Record should still exist after denied delete");
87
+ println!("✓ Record unchanged after authorization denial");
88
+
89
+ Ok(())
90
+ }