Commit d8f09bf
committed
refactor(auth): read the session policy fresh instead of caching it
Follows the pattern the two sibling enterprise-settings call sites already
establish. `lib/logs/execution/logger.ts` and
`lib/workflows/executor/execution-core.ts` both resolve org enterprise settings
with a single indexed read at enforcement time and carry explicit comments
rejecting a cached/re-checked resolution, because a stale or failed read there
silently skips the control.
The session policy is the same shape of thing, and it is not a hot path: it is
read on sign-in and on a sliding session refresh, which the 24h cookie cache
limits to roughly once per user per day. Caching it bought nothing and cost the
correctness bug this branch set out to fix — so drop the cache rather than build
machinery to keep two caches coherent. Better Auth also documents
`cookieCache.version` as a static string bumped at deploy and offers no
cross-instance invalidation, so the version TTL is inherent to the design while
the policy cache never was.
This removes, rather than fixes, the whole class of problems the previous
revision introduced: no merged record, no cache-bypass flag, no last-known-good
fallback, no entitlement cache, no destructive-invalidation window, and no
ordering hazard between concurrent readers. `security-policy.ts` is back to
caching only the cookie-cache version — which genuinely is per-request — plus
the membership lookup.
Failure posture is now chosen per call site instead of globally fail-open:
- `getSessionPolicy` and `getMemberOrganizationId` propagate read errors.
- The session UPDATE hook refuses to EXTEND on a failed read, keeping the
member's current expiry rather than granting a fresh 30 days the policy may
forbid.
- The session CREATE hook allows the sign-in but logs that the session went
unclamped — refusing sign-ins would turn a read blip into an org-wide outage.
- `getSessionCookieCacheVersion` still never throws; it runs on every request,
and its fallback only costs a cookie mismatch and a database session read.
Kept from the previous revision: the entry caps with low-water-mark eviction,
the organizations-disabled short-circuit, the propagating enterprise-plan
resolver, and the corrected idle-timeout copy.1 parent 825959b commit d8f09bf
9 files changed
Lines changed: 212 additions & 330 deletions
File tree
- apps/sim
- app/api/organizations/[id]
- session-policy
- sessions/revoke
- lib/auth
Lines changed: 1 addition & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
24 | 24 | | |
25 | 25 | | |
26 | 26 | | |
27 | | - | |
| 27 | + | |
28 | 28 | | |
29 | 29 | | |
30 | 30 | | |
| |||
Lines changed: 2 additions & 2 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
9 | 9 | | |
10 | 10 | | |
11 | 11 | | |
12 | | - | |
| 12 | + | |
13 | 13 | | |
14 | 14 | | |
15 | 15 | | |
| |||
160 | 160 | | |
161 | 161 | | |
162 | 162 | | |
163 | | - | |
| 163 | + | |
164 | 164 | | |
165 | 165 | | |
166 | 166 | | |
| |||
Lines changed: 4 additions & 4 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
13 | 13 | | |
14 | 14 | | |
15 | 15 | | |
16 | | - | |
| 16 | + | |
17 | 17 | | |
18 | 18 | | |
19 | | - | |
| 19 | + | |
20 | 20 | | |
21 | 21 | | |
22 | 22 | | |
23 | | - | |
| 23 | + | |
24 | 24 | | |
25 | 25 | | |
26 | 26 | | |
| |||
157 | 157 | | |
158 | 158 | | |
159 | 159 | | |
160 | | - | |
| 160 | + | |
161 | 161 | | |
162 | 162 | | |
163 | 163 | | |
| |||
Lines changed: 2 additions & 2 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
8 | 8 | | |
9 | 9 | | |
10 | 10 | | |
11 | | - | |
| 11 | + | |
12 | 12 | | |
13 | 13 | | |
14 | 14 | | |
| |||
105 | 105 | | |
106 | 106 | | |
107 | 107 | | |
108 | | - | |
| 108 | + | |
109 | 109 | | |
110 | 110 | | |
111 | 111 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
685 | 685 | | |
686 | 686 | | |
687 | 687 | | |
688 | | - | |
689 | | - | |
690 | | - | |
691 | | - | |
692 | | - | |
693 | | - | |
694 | | - | |
| 688 | + | |
695 | 689 | | |
696 | 690 | | |
697 | 691 | | |
| |||
703 | 697 | | |
704 | 698 | | |
705 | 699 | | |
706 | | - | |
| 700 | + | |
| 701 | + | |
| 702 | + | |
| 703 | + | |
707 | 704 | | |
708 | 705 | | |
709 | 706 | | |
| |||
728 | 725 | | |
729 | 726 | | |
730 | 727 | | |
731 | | - | |
732 | | - | |
733 | | - | |
734 | | - | |
735 | | - | |
| 728 | + | |
| 729 | + | |
| 730 | + | |
| 731 | + | |
| 732 | + | |
| 733 | + | |
| 734 | + | |
| 735 | + | |
| 736 | + | |
| 737 | + | |
| 738 | + | |
| 739 | + | |
| 740 | + | |
| 741 | + | |
| 742 | + | |
| 743 | + | |
736 | 744 | | |
737 | 745 | | |
738 | 746 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
12 | 12 | | |
13 | 13 | | |
14 | 14 | | |
15 | | - | |
16 | 15 | | |
17 | 16 | | |
18 | 17 | | |
19 | | - | |
| 18 | + | |
20 | 19 | | |
21 | 20 | | |
22 | 21 | | |
| |||
33 | 32 | | |
34 | 33 | | |
35 | 34 | | |
36 | | - | |
37 | | - | |
| 35 | + | |
| 36 | + | |
38 | 37 | | |
39 | | - | |
40 | | - | |
41 | | - | |
| 38 | + | |
42 | 39 | | |
43 | | - | |
44 | | - | |
45 | | - | |
46 | | - | |
| 40 | + | |
| 41 | + | |
47 | 42 | | |
48 | | - | |
49 | 43 | | |
50 | | - | |
51 | | - | |
52 | | - | |
53 | | - | |
54 | | - | |
55 | | - | |
56 | | - | |
57 | | - | |
58 | | - | |
59 | | - | |
60 | | - | |
61 | | - | |
62 | | - | |
63 | | - | |
64 | | - | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
65 | 47 | | |
66 | 48 | | |
67 | | - | |
68 | | - | |
69 | | - | |
70 | | - | |
71 | | - | |
72 | | - | |
73 | | - | |
74 | | - | |
75 | | - | |
76 | | - | |
77 | | - | |
78 | | - | |
79 | | - | |
80 | | - | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
81 | 52 | | |
82 | 53 | | |
83 | | - | |
84 | | - | |
85 | | - | |
86 | | - | |
87 | | - | |
88 | | - | |
89 | | - | |
90 | | - | |
91 | | - | |
92 | | - | |
93 | | - | |
94 | | - | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
95 | 57 | | |
96 | 58 | | |
97 | | - | |
98 | | - | |
99 | | - | |
100 | | - | |
101 | | - | |
102 | | - | |
103 | | - | |
104 | | - | |
105 | | - | |
106 | | - | |
107 | | - | |
108 | | - | |
109 | | - | |
110 | | - | |
111 | | - | |
112 | | - | |
113 | | - | |
114 | | - | |
115 | | - | |
116 | | - | |
117 | | - | |
118 | | - | |
| 59 | + | |
119 | 60 | | |
120 | 61 | | |
121 | 62 | | |
122 | | - | |
123 | | - | |
124 | | - | |
125 | | - | |
126 | | - | |
127 | | - | |
128 | | - | |
129 | | - | |
130 | | - | |
131 | | - | |
132 | | - | |
133 | | - | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
134 | 66 | | |
135 | 67 | | |
136 | 68 | | |
| |||
152 | 84 | | |
153 | 85 | | |
154 | 86 | | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
155 | 94 | | |
156 | 95 | | |
157 | 96 | | |
158 | 97 | | |
159 | 98 | | |
160 | 99 | | |
161 | 100 | | |
162 | | - | |
| 101 | + | |
163 | 102 | | |
164 | 103 | | |
165 | 104 | | |
| |||
169 | 108 | | |
170 | 109 | | |
171 | 110 | | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
172 | 119 | | |
173 | 120 | | |
174 | 121 | | |
| |||
0 commit comments