1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
|
import { describe, it, expect } from "bun:test";
import { readFileSync } from "node:fs";
import { join } from "node:path";
import { mayScimDisableAccount } from "../routes/scim";
const ORG_A = "11111111-1111-1111-1111-111111111111";
const ORG_B = "22222222-2222-2222-2222-222222222222";
describe("mayScimDisableAccount", () => {
it("refuses an account the org did not create (the attack)", () => {
expect(
mayScimDisableAccount({
provisionedByOrgId: null,
orgId: ORG_A,
hasOtherOrgMemberships: false,
})
).toBe(false);
});
it("refuses an account another org's connector created", () => {
expect(
mayScimDisableAccount({
provisionedByOrgId: ORG_B,
orgId: ORG_A,
hasOtherOrgMemberships: false,
})
).toBe(false);
});
it("refuses when the user still belongs to another org", () => {
expect(
mayScimDisableAccount({
provisionedByOrgId: ORG_A,
orgId: ORG_A,
hasOtherOrgMemberships: true,
})
).toBe(false);
});
it("allows only our own shadow account that nobody else uses", () => {
expect(
mayScimDisableAccount({
provisionedByOrgId: ORG_A,
orgId: ORG_A,
hasOtherOrgMemberships: false,
})
).toBe(true);
});
it("treats an empty provenance string as no provenance", () => {
expect(
mayScimDisableAccount({
provisionedByOrgId: "",
orgId: "",
hasOtherOrgMemberships: false,
})
).toBe(false);
});
});
function scimSource(): string {
const text = readFileSync(
join(import.meta.dir, "..", "routes", "scim.tsx"),
"utf8"
);
const stripped = text
.split("\n")
.filter((l) => !l.trim().startsWith("//") && !l.trim().startsWith("*"))
.join("\n");
expect(stripped.length).toBeGreaterThan(0);
return stripped;
}
function slice(src: string, startAnchor: string, endAnchor: string): string {
const start = src.indexOf(startAnchor);
expect(start).toBeGreaterThan(-1);
const end = src.indexOf(endAnchor, start + startAnchor.length);
expect(end).toBeGreaterThan(start);
const body = src.slice(start, end);
expect(body.length).toBeGreaterThan(0);
return body;
}
describe("every SCIM verb gates account-level writes", () => {
const DEPROVISION_MARKER = "deletionScheduledFor";
it("PUT gates `active` behind the rule", () => {
const body = slice(
scimSource(),
`scim.put("/scim/v2/:orgId/Users/:userId"`,
`scim.patch("/scim/v2/:orgId/Users/:userId"`
);
expect(body).toContain(DEPROVISION_MARKER);
expect(body).toContain("canScimManageAccount");
expect(body.indexOf("canScimManageAccount")).toBeLessThan(
body.indexOf(DEPROVISION_MARKER)
);
});
it("PATCH gates `active` behind the rule", () => {
const body = slice(
scimSource(),
`scim.patch("/scim/v2/:orgId/Users/:userId"`,
`scim.delete("/scim/v2/:orgId/Users/:userId"`
);
expect(body).toContain(DEPROVISION_MARKER);
expect(body).toContain("canScimManageAccount");
expect(body.indexOf("canScimManageAccount")).toBeLessThan(
body.indexOf(DEPROVISION_MARKER)
);
});
it("DELETE gates the soft-delete behind the rule", () => {
const body = slice(
scimSource(),
`scim.delete("/scim/v2/:orgId/Users/:userId"`,
`scim.get("/scim/v2/:orgId/ServiceProviderConfig"`
);
expect(body).toContain(DEPROVISION_MARKER);
expect(body).toContain("canScimManageAccount");
expect(body.indexOf("canScimManageAccount")).toBeLessThan(
body.indexOf(DEPROVISION_MARKER)
);
expect(body).toContain("delete(orgMembers)");
expect(body.indexOf("delete(orgMembers)")).toBeLessThan(
body.indexOf("canScimManageAccount")
);
});
it("POST stamps provenance so shadow accounts are identifiable", () => {
const body = slice(
scimSource(),
`scim.post("/scim/v2/:orgId/Users"`,
`scim.get("/scim/v2/:orgId/Users/:userId"`
);
expect(body).toContain("scimProvisionedByOrgId: orgId");
expect(body.split("scimProvisionedByOrgId").length - 1).toBe(1);
expect(body.indexOf("scimProvisionedByOrgId")).toBeGreaterThan(
body.indexOf("insert(users)")
);
});
});
|