plugin-git-manager 1.2.27 → 1.2.28
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 +133 -108
- package/client-v2.d.ts +2 -2
- package/client-v2.js +1 -1
- package/client.d.ts +3 -3
- package/client.js +1 -1
- package/dist/client/{326.90c276058682b536.js → 313.d38fc1d40c4589b9.js} +1 -1
- package/dist/client/900.a48beac7e2ebee7d.js +10 -0
- package/dist/client/index.js +1 -1
- package/dist/client-v2/156.445366026c1cc9a8.js +10 -0
- package/dist/client-v2/{326.42b7b53651ffab3a.js → 313.3c867179c3a1e817.js} +1 -1
- package/dist/client-v2/434.19c7579c06b9077c.js +10 -0
- package/dist/client-v2/900.e6155002a9c97e7f.js +10 -0
- package/dist/client-v2/928.bba701b5a1ece58e.js +10 -0
- package/dist/client-v2/index.js +1 -1
- package/dist/externalVersion.js +9 -8
- package/dist/locale/en-US.json +307 -297
- package/dist/locale/vi-VN.json +295 -292
- package/dist/locale/zh-CN.json +61 -51
- package/dist/node_modules/simple-git/dist/cjs/index.js +25 -25
- package/dist/node_modules/simple-git/package.json +1 -1
- package/dist/node_modules/zod/index.cjs +1 -1
- package/dist/node_modules/zod/package.json +1 -1
- package/dist/server/actions/git-actions.js +90 -36
- package/dist/server/actions/gitlab-api.js +4 -2
- package/dist/server/actions/poller.js +5 -2
- package/dist/server/actions/review.js +20 -10
- package/dist/server/plugin.js +6 -4
- package/dist/server/poller.js +46 -45
- package/dist/server/request-method-policy.js +75 -0
- package/dist/server/services/registry-content-service.js +4 -3
- package/dist/server/utils/get-repo-account.js +3 -2
- package/dist/server/utils/url-security.js +24 -0
- package/package.json +61 -43
- package/server.d.ts +2 -2
- package/server.js +1 -1
- package/src/client/__tests__/RunReviewButton.test.tsx +73 -0
- package/src/client/ai-context.tsx +114 -114
- package/src/client/components/AIEmployeeSelect.tsx +63 -63
- package/src/client/components/CommitHistory.tsx +374 -374
- package/src/client/components/FileExplorer.tsx +386 -386
- package/src/client/components/GitAccounts.tsx +179 -179
- package/src/client/components/GitManagerSettings.tsx +160 -160
- package/src/client/components/GitOperations.tsx +220 -220
- package/src/client/components/LLMModelSelect.tsx +63 -63
- package/src/client/components/LLMServiceSelect.tsx +58 -58
- package/src/client/components/MarkdownView.tsx +80 -80
- package/src/client/components/MergeRequests.tsx +790 -790
- package/src/client/components/PollingStatus.tsx +225 -225
- package/src/client/components/RepositoryConfig.tsx +335 -335
- package/src/client/components/RepositoryPermissions.tsx +123 -123
- package/src/client/components/ReviewFlows.tsx +303 -303
- package/src/client/components/ReviewHistory.tsx +566 -566
- package/src/client/components/RunReviewButton.tsx +63 -55
- package/src/client/context/GitManagerContext.tsx +92 -92
- package/src/client/index.tsx +91 -82
- package/src/client/locale.ts +9 -9
- package/src/client/promptTemplates.ts +31 -31
- package/src/client-v2/ai-context.tsx +99 -0
- package/src/client-v2/components/SubtreeSplits.tsx +547 -547
- package/src/client-v2/index.tsx +1 -1
- package/src/client-v2/locale.ts +8 -8
- package/src/client-v2/plugin.tsx +67 -39
- package/src/index.ts +2 -2
- package/src/locale/en-US.json +307 -297
- package/src/locale/vi-VN.json +295 -292
- package/src/locale/zh-CN.json +61 -51
- package/src/server/__tests__/git-actions.test.ts +24 -24
- package/src/server/__tests__/git-security.test.ts +170 -0
- package/src/server/__tests__/plugin-url-security.test.ts +17 -17
- package/src/server/__tests__/poller-lifecycle.test.ts +71 -0
- package/src/server/__tests__/poller.test.ts +37 -37
- package/src/server/__tests__/repository-access.test.ts +752 -752
- package/src/server/__tests__/review.test.ts +137 -137
- package/src/server/__tests__/role-permissions.test.ts +147 -147
- package/src/server/__tests__/smoke.test.ts +139 -17
- package/src/server/__tests__/subtree.test.ts +85 -85
- package/src/server/actions/git-actions.ts +540 -480
- package/src/server/actions/gitlab-api.ts +422 -411
- package/src/server/actions/poller.ts +39 -27
- package/src/server/actions/review.ts +1388 -1369
- package/src/server/actions/role-permissions.ts +321 -321
- package/src/server/actions/subtree.ts +508 -508
- package/src/server/ai-tools.ts +319 -319
- package/src/server/collections/gitAccounts.ts +56 -56
- package/src/server/collections/gitCodeReviews.ts +199 -199
- package/src/server/collections/gitRepositories.ts +87 -87
- package/src/server/collections/gitReviewFlows.ts +108 -108
- package/src/server/collections/gitSubtreeConfigs.ts +38 -38
- package/src/server/collections/gitSubtreeRuns.ts +30 -30
- package/src/server/index.ts +2 -2
- package/src/server/migrations/20260508000000-add-auto-review-flow-id.ts +29 -29
- package/src/server/migrations/20260712000000-add-git-accounts.ts +198 -198
- package/src/server/migrations/20260730000000-migrate-legacy-repository-permissions.ts +81 -81
- package/src/server/plugin.ts +372 -361
- package/src/server/poller.ts +351 -352
- package/src/server/repository-access.ts +773 -773
- package/src/server/request-method-policy.ts +55 -0
- package/src/server/services/__tests__/registry-content-service.test.ts +359 -345
- package/src/server/services/registry-content-service.ts +495 -484
- package/src/server/utils/__tests__/redact.test.ts +56 -56
- package/src/server/utils/__tests__/url-security.test.ts +85 -85
- package/src/server/utils/get-repo-account.ts +51 -41
- package/src/server/utils/gitlab-url.ts +38 -38
- package/src/server/utils/redact.ts +87 -87
- package/src/server/utils/url-security.ts +128 -95
- package/dist/client/900.acf7d1fa22f95e71.js +0 -10
- package/dist/client-v2/434.7a27051db18526c0.js +0 -10
- package/dist/client-v2/900.f32b9dc85249fc0a.js +0 -10
|
@@ -1,773 +1,773 @@
|
|
|
1
|
-
const REPOSITORY_ID_ACTIONS = new Set([
|
|
2
|
-
'clone',
|
|
3
|
-
'pull',
|
|
4
|
-
'push',
|
|
5
|
-
'fetch',
|
|
6
|
-
'diff',
|
|
7
|
-
'status',
|
|
8
|
-
'log',
|
|
9
|
-
'branches',
|
|
10
|
-
'checkout',
|
|
11
|
-
'fileTree',
|
|
12
|
-
'fileContent',
|
|
13
|
-
'commitDetail',
|
|
14
|
-
'mergeRequests',
|
|
15
|
-
'mergeRequestDetail',
|
|
16
|
-
'mergeRequestNotes',
|
|
17
|
-
'triggerReview',
|
|
18
|
-
'subtreeOptions',
|
|
19
|
-
]);
|
|
20
|
-
|
|
21
|
-
const SUBTREE_CONFIG_ACTIONS = new Set(['subtreePreview', 'subtreeRun', 'subtreeReplace']);
|
|
22
|
-
|
|
23
|
-
const REVIEW_ID_ACTIONS = new Set(['reviewApprovePost', 'reviewReject']);
|
|
24
|
-
|
|
25
|
-
type RepositoryId = number | string;
|
|
26
|
-
|
|
27
|
-
type ScopedCollectionName =
|
|
28
|
-
| 'gitRepositories'
|
|
29
|
-
| 'gitAccounts'
|
|
30
|
-
| 'gitReviewFlows'
|
|
31
|
-
| 'gitCodeReviews'
|
|
32
|
-
| 'gitSubtreeConfigs'
|
|
33
|
-
| 'gitSubtreeRuns';
|
|
34
|
-
|
|
35
|
-
type ActionParams = Record<string, unknown> & { values?: unknown };
|
|
36
|
-
|
|
37
|
-
type RecordWithGetter = Record<string, unknown> & {
|
|
38
|
-
get?: (attribute: string) => unknown;
|
|
39
|
-
};
|
|
40
|
-
|
|
41
|
-
export type RegistryGitAccessContext =
|
|
42
|
-
| {
|
|
43
|
-
kind: 'user';
|
|
44
|
-
roles: string[];
|
|
45
|
-
}
|
|
46
|
-
| {
|
|
47
|
-
kind: 'system';
|
|
48
|
-
reason: 'scheduled-sync';
|
|
49
|
-
};
|
|
50
|
-
|
|
51
|
-
export type RepositoryPermissionResult = { params?: { filter?: unknown } } | null;
|
|
52
|
-
|
|
53
|
-
type ResolvedRepositoryScope = {
|
|
54
|
-
ids: RepositoryId[];
|
|
55
|
-
unrestricted: boolean;
|
|
56
|
-
supported: boolean;
|
|
57
|
-
};
|
|
58
|
-
|
|
59
|
-
export interface RepositoryPermissionChecker {
|
|
60
|
-
can(input: {
|
|
61
|
-
roles: string[];
|
|
62
|
-
resource: string;
|
|
63
|
-
action: string;
|
|
64
|
-
}): Promise<RepositoryPermissionResult> | RepositoryPermissionResult;
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
type RepositoryLookup = {
|
|
68
|
-
findOne(input: { filterByTk: RepositoryId }): Promise<unknown>;
|
|
69
|
-
};
|
|
70
|
-
|
|
71
|
-
type RepositoryAccessDatabase = {
|
|
72
|
-
getRepository(name: 'gitSubtreeConfigs' | 'gitCodeReviews'): RepositoryLookup;
|
|
73
|
-
};
|
|
74
|
-
|
|
75
|
-
function asRecord(value: unknown): Record<string, unknown> {
|
|
76
|
-
return value && typeof value === 'object' && !Array.isArray(value) ? (value as Record<string, unknown>) : {};
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
function asRepositoryId(value: unknown): RepositoryId | undefined {
|
|
80
|
-
if (typeof value === 'number' && Number.isFinite(value)) return value;
|
|
81
|
-
if (typeof value === 'string' && value.trim()) return value;
|
|
82
|
-
return undefined;
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
function getRecordValue(record: unknown, attribute: string): unknown {
|
|
86
|
-
if (!record || typeof record !== 'object') return undefined;
|
|
87
|
-
const model = record as RecordWithGetter;
|
|
88
|
-
return typeof model.get === 'function' ? model.get(attribute) : model[attribute];
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
/**
|
|
92
|
-
* NocoBase actions can receive values in the URL parameters, the action
|
|
93
|
-
* values envelope, or the request body. Keep this precedence identical to
|
|
94
|
-
* the actions themselves so authorization always checks the repository that
|
|
95
|
-
* will actually be used.
|
|
96
|
-
*/
|
|
97
|
-
export function getEffectiveActionParams(ctx: {
|
|
98
|
-
action?: { params?: ActionParams };
|
|
99
|
-
request?: { body?: unknown };
|
|
100
|
-
}): Record<string, unknown> {
|
|
101
|
-
const params = asRecord(ctx.action?.params);
|
|
102
|
-
return {
|
|
103
|
-
...params,
|
|
104
|
-
...asRecord(params.values),
|
|
105
|
-
...asRecord(ctx.request?.body),
|
|
106
|
-
};
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
function supportedRepositoryIds(ids: Iterable<RepositoryId>): ResolvedRepositoryScope {
|
|
110
|
-
const uniqueIds: RepositoryId[] = [];
|
|
111
|
-
for (const id of ids) {
|
|
112
|
-
if (!uniqueIds.some((candidate) => String(candidate) === String(id))) {
|
|
113
|
-
uniqueIds.push(id);
|
|
114
|
-
}
|
|
115
|
-
}
|
|
116
|
-
return { ids: uniqueIds, unrestricted: false, supported: true };
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
function unsupportedRepositoryScope(): ResolvedRepositoryScope {
|
|
120
|
-
return { ids: [], unrestricted: false, supported: false };
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
function resolveRepositoryScopeBranches(operator: '$and' | '$or', branches: unknown[]): ResolvedRepositoryScope {
|
|
124
|
-
const resolutions = branches.map(resolveRepositoryScope);
|
|
125
|
-
if (resolutions.some((resolution) => !resolution.supported)) {
|
|
126
|
-
return unsupportedRepositoryScope();
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
if (operator === '$or') {
|
|
130
|
-
if (resolutions.some((resolution) => resolution.unrestricted)) {
|
|
131
|
-
return { ids: [], unrestricted: true, supported: true };
|
|
132
|
-
}
|
|
133
|
-
return supportedRepositoryIds(resolutions.flatMap((resolution) => resolution.ids));
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
const restricted = resolutions.filter((resolution) => !resolution.unrestricted);
|
|
137
|
-
if (restricted.length === 0) {
|
|
138
|
-
return { ids: [], unrestricted: true, supported: true };
|
|
139
|
-
}
|
|
140
|
-
const [first, ...rest] = restricted;
|
|
141
|
-
return supportedRepositoryIds(
|
|
142
|
-
first.ids.filter((id) =>
|
|
143
|
-
rest.every((resolution) => resolution.ids.some((candidate) => String(candidate) === String(id))),
|
|
144
|
-
),
|
|
145
|
-
);
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
/**
|
|
149
|
-
* Parses the limited ID-only scope grammar written by Repository Permissions.
|
|
150
|
-
* An unknown condition must fail closed: translating it to a collection filter
|
|
151
|
-
* could otherwise turn a restrictive scope into a broader one.
|
|
152
|
-
*/
|
|
153
|
-
export function resolveRepositoryScope(scope: unknown): ResolvedRepositoryScope {
|
|
154
|
-
if (scope == null) {
|
|
155
|
-
return { ids: [], unrestricted: true, supported: true };
|
|
156
|
-
}
|
|
157
|
-
if (typeof scope !== 'object' || Array.isArray(scope)) {
|
|
158
|
-
return unsupportedRepositoryScope();
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
const condition = scope as Record<string, unknown>;
|
|
162
|
-
const keys = Object.keys(condition);
|
|
163
|
-
if (keys.length === 0) {
|
|
164
|
-
return { ids: [], unrestricted: true, supported: true };
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
const operator = keys.find((key) => key === '$and' || key === '$or');
|
|
168
|
-
if (operator) {
|
|
169
|
-
if (keys.length !== 1 || !Array.isArray(condition[operator])) {
|
|
170
|
-
return unsupportedRepositoryScope();
|
|
171
|
-
}
|
|
172
|
-
return resolveRepositoryScopeBranches(operator as '$and' | '$or', condition[operator] as unknown[]);
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
if (keys.length !== 1 || !('id' in condition)) {
|
|
176
|
-
return unsupportedRepositoryScope();
|
|
177
|
-
}
|
|
178
|
-
const id = condition.id;
|
|
179
|
-
if (typeof id === 'number' || typeof id === 'string') {
|
|
180
|
-
return supportedRepositoryIds([id]);
|
|
181
|
-
}
|
|
182
|
-
if (!id || typeof id !== 'object' || Array.isArray(id)) {
|
|
183
|
-
return unsupportedRepositoryScope();
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
const idCondition = id as Record<string, unknown>;
|
|
187
|
-
const idKeys = Object.keys(idCondition);
|
|
188
|
-
if (idKeys.length !== 1) {
|
|
189
|
-
return unsupportedRepositoryScope();
|
|
190
|
-
}
|
|
191
|
-
const equal = idCondition.$eq;
|
|
192
|
-
if (typeof equal === 'number' || typeof equal === 'string') {
|
|
193
|
-
return supportedRepositoryIds([equal]);
|
|
194
|
-
}
|
|
195
|
-
const included = idCondition.$in;
|
|
196
|
-
if (Array.isArray(included) && included.every((value) => typeof value === 'number' || typeof value === 'string')) {
|
|
197
|
-
return supportedRepositoryIds(included as RepositoryId[]);
|
|
198
|
-
}
|
|
199
|
-
return unsupportedRepositoryScope();
|
|
200
|
-
}
|
|
201
|
-
|
|
202
|
-
export function scopeIncludesRepository(filter: unknown, repositoryId: number | string): boolean {
|
|
203
|
-
const scope = resolveRepositoryScope(filter);
|
|
204
|
-
return scope.supported && (scope.unrestricted || scope.ids.some((id) => String(id) === String(repositoryId)));
|
|
205
|
-
}
|
|
206
|
-
|
|
207
|
-
/**
|
|
208
|
-
* Checks the same Git Manager repository scope used by HTTP actions. Registry
|
|
209
|
-
* content reads use `fileContent` because a source sync ultimately reads file
|
|
210
|
-
* bytes, even when its first request is a tree listing.
|
|
211
|
-
*/
|
|
212
|
-
export async function hasRepositoryAccess(input: {
|
|
213
|
-
acl?: RepositoryPermissionChecker;
|
|
214
|
-
roles: string[];
|
|
215
|
-
repositoryId: number | string;
|
|
216
|
-
action?: string;
|
|
217
|
-
}): Promise<boolean> {
|
|
218
|
-
if (input.roles.includes('root')) {
|
|
219
|
-
return true;
|
|
220
|
-
}
|
|
221
|
-
if (!input.acl) {
|
|
222
|
-
return false;
|
|
223
|
-
}
|
|
224
|
-
const permission = await input.acl.can({
|
|
225
|
-
roles: input.roles,
|
|
226
|
-
resource: 'gitManager',
|
|
227
|
-
action: input.action || 'fileContent',
|
|
228
|
-
});
|
|
229
|
-
return Boolean(permission && scopeIncludesRepository(permission.params?.filter, input.repositoryId));
|
|
230
|
-
}
|
|
231
|
-
|
|
232
|
-
async function hasUnrestrictedRepositoryAccess(input: {
|
|
233
|
-
acl?: RepositoryPermissionChecker;
|
|
234
|
-
roles: string[];
|
|
235
|
-
action: string;
|
|
236
|
-
}): Promise<boolean> {
|
|
237
|
-
if (input.roles.includes('root')) return true;
|
|
238
|
-
if (!input.acl) return false;
|
|
239
|
-
const permission = await input.acl.can({
|
|
240
|
-
roles: input.roles,
|
|
241
|
-
resource: 'gitManager',
|
|
242
|
-
action: input.action,
|
|
243
|
-
});
|
|
244
|
-
const scope = resolveRepositoryScope(permission?.params?.filter);
|
|
245
|
-
return Boolean(permission && scope.supported && scope.unrestricted);
|
|
246
|
-
}
|
|
247
|
-
|
|
248
|
-
type RepositoryAccessContext = {
|
|
249
|
-
action?: {
|
|
250
|
-
resourceName?: string;
|
|
251
|
-
actionName?: string;
|
|
252
|
-
params?: ActionParams;
|
|
253
|
-
};
|
|
254
|
-
state?: { currentRoles?: unknown };
|
|
255
|
-
request?: { body?: unknown };
|
|
256
|
-
db?: RepositoryAccessDatabase;
|
|
257
|
-
app: { acl: RepositoryPermissionChecker };
|
|
258
|
-
throw(status: number, message: string): never;
|
|
259
|
-
};
|
|
260
|
-
|
|
261
|
-
type RepositoryCollectionAccessContext = {
|
|
262
|
-
action?: {
|
|
263
|
-
resourceName?: string;
|
|
264
|
-
actionName?: string;
|
|
265
|
-
params?: ActionParams;
|
|
266
|
-
mergeParams?: (params: { filter: unknown }) => void;
|
|
267
|
-
};
|
|
268
|
-
state?: { currentRoles?: unknown };
|
|
269
|
-
query?: unknown;
|
|
270
|
-
request?: { body?: unknown; query?: unknown };
|
|
271
|
-
db?: {
|
|
272
|
-
getRepository(name: 'gitSubtreeConfigs'): RepositoryLookup;
|
|
273
|
-
};
|
|
274
|
-
app: { acl: RepositoryPermissionChecker };
|
|
275
|
-
getCurrentRepository?: () => { targetCollection?: { name?: string } } | null;
|
|
276
|
-
throw(status: number, message: string): never;
|
|
277
|
-
};
|
|
278
|
-
|
|
279
|
-
const COLLECTION_SCOPE_FIELDS: Record<Exclude<ScopedCollectionName, 'gitAccounts'>, string> = {
|
|
280
|
-
gitRepositories: 'id',
|
|
281
|
-
gitReviewFlows: 'repositoryId',
|
|
282
|
-
gitCodeReviews: 'repositoryId',
|
|
283
|
-
gitSubtreeConfigs: 'repositoryId',
|
|
284
|
-
gitSubtreeRuns: 'config.repositoryId',
|
|
285
|
-
};
|
|
286
|
-
|
|
287
|
-
const STANDARD_COLLECTION_ACTIONS = new Set(['list', 'get', 'create', 'update', 'destroy']);
|
|
288
|
-
|
|
289
|
-
function currentRoles(ctx: {
|
|
290
|
-
state?: { currentRoles?: unknown };
|
|
291
|
-
throw(status: number, message: string): never;
|
|
292
|
-
}): string[] {
|
|
293
|
-
const rawRoles = ctx.state?.currentRoles;
|
|
294
|
-
const roles = Array.isArray(rawRoles) ? rawRoles.filter((role): role is string => typeof role === 'string') : [];
|
|
295
|
-
if (Array.isArray(rawRoles) && roles.length !== rawRoles.length) {
|
|
296
|
-
return ctx.throw(403, 'Permission denied');
|
|
297
|
-
}
|
|
298
|
-
return roles;
|
|
299
|
-
}
|
|
300
|
-
|
|
301
|
-
function scopedCollectionName(ctx: RepositoryCollectionAccessContext): ScopedCollectionName | null {
|
|
302
|
-
let targetCollectionName: string | undefined;
|
|
303
|
-
try {
|
|
304
|
-
targetCollectionName = ctx.getCurrentRepository?.()?.targetCollection?.name;
|
|
305
|
-
} catch {
|
|
306
|
-
// Some custom resource routes do not resolve a backing repository. Their
|
|
307
|
-
// route parameters below are still sufficient to identify a scoped target.
|
|
308
|
-
}
|
|
309
|
-
if (targetCollectionName && Object.prototype.hasOwnProperty.call(COLLECTION_SCOPE_FIELDS, targetCollectionName)) {
|
|
310
|
-
return targetCollectionName as Exclude<ScopedCollectionName, 'gitAccounts'>;
|
|
311
|
-
}
|
|
312
|
-
if (targetCollectionName === 'gitAccounts') {
|
|
313
|
-
return targetCollectionName;
|
|
314
|
-
}
|
|
315
|
-
|
|
316
|
-
const params = asRecord(ctx.action?.params);
|
|
317
|
-
const candidates = [ctx.action?.resourceName, params.resourceName, params.associatedName, params.targetCollection]
|
|
318
|
-
.filter((value): value is string => typeof value === 'string')
|
|
319
|
-
.flatMap((value) => value.split('.'));
|
|
320
|
-
return (
|
|
321
|
-
candidates.find(
|
|
322
|
-
(candidate): candidate is ScopedCollectionName =>
|
|
323
|
-
candidate === 'gitAccounts' || Object.prototype.hasOwnProperty.call(COLLECTION_SCOPE_FIELDS, candidate),
|
|
324
|
-
) || null
|
|
325
|
-
);
|
|
326
|
-
}
|
|
327
|
-
|
|
328
|
-
function collectionFilter(collectionName: Exclude<ScopedCollectionName, 'gitAccounts'>, ids: RepositoryId[]): unknown {
|
|
329
|
-
if (collectionName === 'gitSubtreeRuns') {
|
|
330
|
-
return { config: { repositoryId: { $in: ids } } };
|
|
331
|
-
}
|
|
332
|
-
return { [COLLECTION_SCOPE_FIELDS[collectionName]]: { $in: ids } };
|
|
333
|
-
}
|
|
334
|
-
|
|
335
|
-
function effectiveMutationValues(ctx: RepositoryCollectionAccessContext): Record<string, unknown> {
|
|
336
|
-
const body = asRecord(ctx.request?.body);
|
|
337
|
-
return { ...asRecord(body.values), ...getEffectiveActionParams(ctx) };
|
|
338
|
-
}
|
|
339
|
-
|
|
340
|
-
/**
|
|
341
|
-
* `targetCollection` is forwarded by the generic repository actions. If a
|
|
342
|
-
* request changes it after routing has selected a Git Manager collection, the
|
|
343
|
-
* repository implementation can execute against a different collection while
|
|
344
|
-
* ACL and repository-scope filters still describe the original one. The
|
|
345
|
-
* `__collection` value is the equivalent association payload form.
|
|
346
|
-
*/
|
|
347
|
-
function rejectTargetCollectionOverride(
|
|
348
|
-
ctx: RepositoryCollectionAccessContext,
|
|
349
|
-
collectionName: ScopedCollectionName,
|
|
350
|
-
): void {
|
|
351
|
-
const actionParams = asRecord(ctx.action?.params);
|
|
352
|
-
const requestBody = asRecord(ctx.request?.body);
|
|
353
|
-
const actionValues = asRecord(actionParams.values);
|
|
354
|
-
const bodyValues = asRecord(requestBody.values);
|
|
355
|
-
const requestedCollections = [
|
|
356
|
-
actionParams.targetCollection,
|
|
357
|
-
actionParams.__collection,
|
|
358
|
-
actionValues.targetCollection,
|
|
359
|
-
actionValues.__collection,
|
|
360
|
-
requestBody.targetCollection,
|
|
361
|
-
requestBody.__collection,
|
|
362
|
-
bodyValues.targetCollection,
|
|
363
|
-
bodyValues.__collection,
|
|
364
|
-
];
|
|
365
|
-
for (const requestedCollection of requestedCollections) {
|
|
366
|
-
if (requestedCollection == null || requestedCollection === collectionName) continue;
|
|
367
|
-
ctx.throw(403, 'Permission denied');
|
|
368
|
-
}
|
|
369
|
-
}
|
|
370
|
-
|
|
371
|
-
/**
|
|
372
|
-
* A repository scope limits which repository rows a role can edit; it does not
|
|
373
|
-
* grant the role use of every shared Git credential. Otherwise a user could
|
|
374
|
-
* attach a guessed global account to an allowed repository, change its URL or
|
|
375
|
-
* local checkout, and use that account's PAT or another repository's clone
|
|
376
|
-
* through a permitted Git action.
|
|
377
|
-
*/
|
|
378
|
-
function rejectScopedRepositoryConnectionMutation(ctx: RepositoryCollectionAccessContext): void {
|
|
379
|
-
const valueSources = [ctx.action?.params, ctx.query, ctx.request?.query, ctx.request?.body];
|
|
380
|
-
if (valueSources.some((values) => containsMutationField(values, REPOSITORY_CONNECTION_FIELD_SEGMENTS))) {
|
|
381
|
-
ctx.throw(403, 'Permission denied');
|
|
382
|
-
}
|
|
383
|
-
}
|
|
384
|
-
|
|
385
|
-
function rejectScopedAssociationMutation(
|
|
386
|
-
ctx: RepositoryCollectionAccessContext,
|
|
387
|
-
associationSegments: ReadonlySet<string>,
|
|
388
|
-
): void {
|
|
389
|
-
const valueSources = [ctx.action?.params, ctx.query, ctx.request?.query, ctx.request?.body];
|
|
390
|
-
if (valueSources.some((values) => containsMutationField(values, associationSegments))) {
|
|
391
|
-
ctx.throw(403, 'Permission denied');
|
|
392
|
-
}
|
|
393
|
-
}
|
|
394
|
-
|
|
395
|
-
/**
|
|
396
|
-
* Standard collection handlers can interpret an array as a bulk update and
|
|
397
|
-
* apply it after the scoped filter has been merged. This policy only supports
|
|
398
|
-
* one scoped mutation at a time, so reject arrays instead of attempting to
|
|
399
|
-
* validate a partially normalized bulk payload.
|
|
400
|
-
*/
|
|
401
|
-
function rejectScopedBulkMutationValues(ctx: RepositoryCollectionAccessContext, actionName: string): void {
|
|
402
|
-
if (actionName !== 'create' && actionName !== 'update') {
|
|
403
|
-
return;
|
|
404
|
-
}
|
|
405
|
-
|
|
406
|
-
const actionParams = asRecord(ctx.action?.params);
|
|
407
|
-
const requestBody = ctx.request?.body;
|
|
408
|
-
const bodyValues = asRecord(requestBody).values;
|
|
409
|
-
if (Array.isArray(actionParams.values) || Array.isArray(requestBody) || Array.isArray(bodyValues)) {
|
|
410
|
-
ctx.throw(403, 'Permission denied');
|
|
411
|
-
}
|
|
412
|
-
}
|
|
413
|
-
|
|
414
|
-
const ASSOCIATION_READ_PARAM_KEYS = ['appends', 'include', 'fields'] as const;
|
|
415
|
-
const CREDENTIAL_ASSOCIATION_SEGMENTS = new Set(['gitAccount', 'gitAccounts']);
|
|
416
|
-
const REPOSITORY_CONNECTION_FIELD_SEGMENTS = new Set(['gitAccount', 'gitAccountId', 'repoUrl', 'localPath']);
|
|
417
|
-
const REPOSITORY_ASSOCIATION_SEGMENTS = new Set(['repository']);
|
|
418
|
-
const SUBTREE_RUN_ASSOCIATION_SEGMENTS = new Set(['config']);
|
|
419
|
-
|
|
420
|
-
function pathSegments(value: string): string[] {
|
|
421
|
-
return value.split(/[.[\],:/()=\s'"]+/);
|
|
422
|
-
}
|
|
423
|
-
|
|
424
|
-
function containsCredentialAssociationSegment(value: string): boolean {
|
|
425
|
-
return pathSegments(value).some((segment) => CREDENTIAL_ASSOCIATION_SEGMENTS.has(segment));
|
|
426
|
-
}
|
|
427
|
-
|
|
428
|
-
/**
|
|
429
|
-
* UpdateGuard expands dotted and bracket notation after middleware runs. Walk
|
|
430
|
-
* request object keys recursively so an attacker cannot smuggle a protected
|
|
431
|
-
* connection or association field through dotted, bracket, or nested
|
|
432
|
-
* association-update payloads.
|
|
433
|
-
*/
|
|
434
|
-
function containsMutationField(
|
|
435
|
-
value: unknown,
|
|
436
|
-
fieldSegments: ReadonlySet<string>,
|
|
437
|
-
visited = new WeakSet<object>(),
|
|
438
|
-
): boolean {
|
|
439
|
-
if (!value || typeof value !== 'object') {
|
|
440
|
-
return false;
|
|
441
|
-
}
|
|
442
|
-
if (visited.has(value)) {
|
|
443
|
-
return false;
|
|
444
|
-
}
|
|
445
|
-
visited.add(value);
|
|
446
|
-
|
|
447
|
-
if (Array.isArray(value)) {
|
|
448
|
-
return value.some((item) => containsMutationField(item, fieldSegments, visited));
|
|
449
|
-
}
|
|
450
|
-
|
|
451
|
-
return Object.entries(value as Record<string, unknown>).some(
|
|
452
|
-
([key, item]) =>
|
|
453
|
-
pathSegments(key).some((segment) => fieldSegments.has(segment)) ||
|
|
454
|
-
containsMutationField(item, fieldSegments, visited),
|
|
455
|
-
);
|
|
456
|
-
}
|
|
457
|
-
|
|
458
|
-
/**
|
|
459
|
-
* Generic collection reads accept association projections in more than one
|
|
460
|
-
* shape: an `appends` array, an ORM-style `include` object, or a relation
|
|
461
|
-
* field path. Limit the inspection to those projection parameters so a normal
|
|
462
|
-
* filter value cannot accidentally be treated as a credential request.
|
|
463
|
-
*/
|
|
464
|
-
function containsCredentialAssociation(value: unknown, visited = new WeakSet<object>()): boolean {
|
|
465
|
-
if (typeof value === 'string') {
|
|
466
|
-
return containsCredentialAssociationSegment(value);
|
|
467
|
-
}
|
|
468
|
-
if (!value || typeof value !== 'object') {
|
|
469
|
-
return false;
|
|
470
|
-
}
|
|
471
|
-
if (visited.has(value)) {
|
|
472
|
-
return false;
|
|
473
|
-
}
|
|
474
|
-
visited.add(value);
|
|
475
|
-
|
|
476
|
-
if (Array.isArray(value)) {
|
|
477
|
-
return value.some((item) => containsCredentialAssociation(item, visited));
|
|
478
|
-
}
|
|
479
|
-
|
|
480
|
-
return Object.entries(value as Record<string, unknown>).some(
|
|
481
|
-
([key, item]) => containsCredentialAssociationSegment(key) || containsCredentialAssociation(item, visited),
|
|
482
|
-
);
|
|
483
|
-
}
|
|
484
|
-
|
|
485
|
-
function isAssociationReadParamKey(key: string): boolean {
|
|
486
|
-
return ASSOCIATION_READ_PARAM_KEYS.some(
|
|
487
|
-
(paramKey) => key === paramKey || key.startsWith(`${paramKey}[`) || key.startsWith(`${paramKey}.`),
|
|
488
|
-
);
|
|
489
|
-
}
|
|
490
|
-
|
|
491
|
-
function rejectScopedCredentialAssociationRoute(ctx: RepositoryCollectionAccessContext): void {
|
|
492
|
-
const actionParams = asRecord(ctx.action?.params);
|
|
493
|
-
const requestBody = asRecord(ctx.request?.body);
|
|
494
|
-
const routeValues = [
|
|
495
|
-
ctx.action?.resourceName,
|
|
496
|
-
actionParams.resourceName,
|
|
497
|
-
actionParams.associatedName,
|
|
498
|
-
actionParams.targetCollection,
|
|
499
|
-
requestBody.resourceName,
|
|
500
|
-
requestBody.associatedName,
|
|
501
|
-
requestBody.targetCollection,
|
|
502
|
-
asRecord(actionParams.values).resourceName,
|
|
503
|
-
asRecord(actionParams.values).associatedName,
|
|
504
|
-
asRecord(actionParams.values).targetCollection,
|
|
505
|
-
asRecord(requestBody.values).resourceName,
|
|
506
|
-
asRecord(requestBody.values).associatedName,
|
|
507
|
-
asRecord(requestBody.values).targetCollection,
|
|
508
|
-
asRecord(ctx.query).resourceName,
|
|
509
|
-
asRecord(ctx.query).associatedName,
|
|
510
|
-
asRecord(ctx.query).targetCollection,
|
|
511
|
-
asRecord(ctx.request?.query).resourceName,
|
|
512
|
-
asRecord(ctx.request?.query).associatedName,
|
|
513
|
-
asRecord(ctx.request?.query).targetCollection,
|
|
514
|
-
];
|
|
515
|
-
if (routeValues.some((value) => typeof value === 'string' && containsCredentialAssociationSegment(value))) {
|
|
516
|
-
ctx.throw(403, 'Permission denied');
|
|
517
|
-
}
|
|
518
|
-
}
|
|
519
|
-
|
|
520
|
-
/**
|
|
521
|
-
* A scoped role is allowed to read only selected repository rows, not the
|
|
522
|
-
* global Git account that happens to be attached to those rows. An `appends`
|
|
523
|
-
* or `include` projection of `gitAccount` would otherwise serialize its PAT
|
|
524
|
-
* through the permitted gitRepositories endpoint.
|
|
525
|
-
*/
|
|
526
|
-
function rejectScopedRepositoryCredentialAssociationRead(
|
|
527
|
-
ctx: RepositoryCollectionAccessContext,
|
|
528
|
-
actionName: string,
|
|
529
|
-
): void {
|
|
530
|
-
if (actionName !== 'list' && actionName !== 'get') {
|
|
531
|
-
return;
|
|
532
|
-
}
|
|
533
|
-
|
|
534
|
-
const actionParams = asRecord(ctx.action?.params);
|
|
535
|
-
const requestBody = asRecord(ctx.request?.body);
|
|
536
|
-
const valueSources = [
|
|
537
|
-
actionParams,
|
|
538
|
-
asRecord(actionParams.values),
|
|
539
|
-
asRecord(ctx.query),
|
|
540
|
-
asRecord(ctx.request?.query),
|
|
541
|
-
requestBody,
|
|
542
|
-
asRecord(requestBody.values),
|
|
543
|
-
];
|
|
544
|
-
for (const values of valueSources) {
|
|
545
|
-
for (const [key, value] of Object.entries(values)) {
|
|
546
|
-
if (isAssociationReadParamKey(key) && containsCredentialAssociation(value)) {
|
|
547
|
-
ctx.throw(403, 'Permission denied');
|
|
548
|
-
}
|
|
549
|
-
}
|
|
550
|
-
}
|
|
551
|
-
}
|
|
552
|
-
|
|
553
|
-
function scopeAllowsRepository(scope: ResolvedRepositoryScope, repositoryId: RepositoryId): boolean {
|
|
554
|
-
return scope.unrestricted || scope.ids.some((id) => String(id) === String(repositoryId));
|
|
555
|
-
}
|
|
556
|
-
|
|
557
|
-
function hasOwn(object: Record<string, unknown>, key: string): boolean {
|
|
558
|
-
return Object.prototype.hasOwnProperty.call(object, key);
|
|
559
|
-
}
|
|
560
|
-
|
|
561
|
-
function requireScopedRepositoryValue(
|
|
562
|
-
ctx: RepositoryCollectionAccessContext,
|
|
563
|
-
values: Record<string, unknown>,
|
|
564
|
-
scope: ResolvedRepositoryScope,
|
|
565
|
-
actionName: string,
|
|
566
|
-
): void {
|
|
567
|
-
if (hasOwn(values, 'repository')) {
|
|
568
|
-
ctx.throw(403, 'Permission denied');
|
|
569
|
-
}
|
|
570
|
-
if (!hasOwn(values, 'repositoryId')) {
|
|
571
|
-
if (actionName === 'create') {
|
|
572
|
-
ctx.throw(400, 'repositoryId is required');
|
|
573
|
-
}
|
|
574
|
-
return;
|
|
575
|
-
}
|
|
576
|
-
const repositoryId = asRepositoryId(values.repositoryId);
|
|
577
|
-
if (repositoryId == null || !scopeAllowsRepository(scope, repositoryId)) {
|
|
578
|
-
ctx.throw(403, 'Permission denied');
|
|
579
|
-
}
|
|
580
|
-
}
|
|
581
|
-
|
|
582
|
-
async function requireScopedRunConfigValue(
|
|
583
|
-
ctx: RepositoryCollectionAccessContext,
|
|
584
|
-
values: Record<string, unknown>,
|
|
585
|
-
scope: ResolvedRepositoryScope,
|
|
586
|
-
actionName: string,
|
|
587
|
-
): Promise<void> {
|
|
588
|
-
if (hasOwn(values, 'config')) {
|
|
589
|
-
ctx.throw(403, 'Permission denied');
|
|
590
|
-
}
|
|
591
|
-
if (!hasOwn(values, 'configId')) {
|
|
592
|
-
if (actionName === 'create') {
|
|
593
|
-
ctx.throw(400, 'configId is required');
|
|
594
|
-
}
|
|
595
|
-
return;
|
|
596
|
-
}
|
|
597
|
-
const configId = asRepositoryId(values.configId);
|
|
598
|
-
const database = ctx.db;
|
|
599
|
-
if (configId == null || !database) {
|
|
600
|
-
ctx.throw(403, 'Permission denied');
|
|
601
|
-
}
|
|
602
|
-
const config = await database.getRepository('gitSubtreeConfigs').findOne({ filterByTk: configId });
|
|
603
|
-
if (!config) {
|
|
604
|
-
ctx.throw(404, 'Subtree configuration not found');
|
|
605
|
-
}
|
|
606
|
-
const repositoryId = asRepositoryId(getRecordValue(config, 'repositoryId'));
|
|
607
|
-
if (repositoryId == null || !scopeAllowsRepository(scope, repositoryId)) {
|
|
608
|
-
ctx.throw(403, 'Permission denied');
|
|
609
|
-
}
|
|
610
|
-
}
|
|
611
|
-
|
|
612
|
-
/**
|
|
613
|
-
* Repository Permissions persist their scope on gitRepositories and custom
|
|
614
|
-
* gitManager actions. Generic collection snippets are otherwise unscoped, so
|
|
615
|
-
* apply the same scope before the standard collection handlers run.
|
|
616
|
-
*/
|
|
617
|
-
export async function enforceRepositoryCollectionAccess(
|
|
618
|
-
ctx: RepositoryCollectionAccessContext,
|
|
619
|
-
next: () => Promise<void>,
|
|
620
|
-
): Promise<void> {
|
|
621
|
-
const collectionName = scopedCollectionName(ctx);
|
|
622
|
-
if (!collectionName) {
|
|
623
|
-
await next();
|
|
624
|
-
return;
|
|
625
|
-
}
|
|
626
|
-
|
|
627
|
-
rejectTargetCollectionOverride(ctx, collectionName);
|
|
628
|
-
|
|
629
|
-
const roles = currentRoles(ctx);
|
|
630
|
-
if (roles.includes('root')) {
|
|
631
|
-
await next();
|
|
632
|
-
return;
|
|
633
|
-
}
|
|
634
|
-
const repositoryPermission = await ctx.app.acl.can({
|
|
635
|
-
roles,
|
|
636
|
-
resource: 'gitRepositories',
|
|
637
|
-
action: 'list',
|
|
638
|
-
});
|
|
639
|
-
// No Repository Permissions action exists for this role. Preserve the
|
|
640
|
-
// independently configured collection ACL instead of inventing a scope.
|
|
641
|
-
if (!repositoryPermission) {
|
|
642
|
-
await next();
|
|
643
|
-
return;
|
|
644
|
-
}
|
|
645
|
-
const scope = resolveRepositoryScope(repositoryPermission.params?.filter);
|
|
646
|
-
if (!scope.supported) {
|
|
647
|
-
ctx.throw(403, 'Permission denied');
|
|
648
|
-
}
|
|
649
|
-
if (scope.unrestricted) {
|
|
650
|
-
await next();
|
|
651
|
-
return;
|
|
652
|
-
}
|
|
653
|
-
|
|
654
|
-
const actionName = ctx.action?.actionName;
|
|
655
|
-
if (!actionName || !STANDARD_COLLECTION_ACTIONS.has(actionName)) {
|
|
656
|
-
ctx.throw(403, 'Permission denied');
|
|
657
|
-
}
|
|
658
|
-
if (collectionName === 'gitAccounts') {
|
|
659
|
-
ctx.throw(403, 'Permission denied');
|
|
660
|
-
}
|
|
661
|
-
if (collectionName === 'gitCodeReviews' && (actionName === 'create' || actionName === 'update')) {
|
|
662
|
-
// Review rows are durable worker jobs. Their flow, metadata, status and
|
|
663
|
-
// session fields control execution, so scoped users must use the checked
|
|
664
|
-
// gitManager review actions instead of generic collection mutations.
|
|
665
|
-
ctx.throw(403, 'Permission denied');
|
|
666
|
-
}
|
|
667
|
-
rejectScopedCredentialAssociationRoute(ctx);
|
|
668
|
-
rejectScopedRepositoryCredentialAssociationRead(ctx, actionName);
|
|
669
|
-
rejectScopedBulkMutationValues(ctx, actionName);
|
|
670
|
-
|
|
671
|
-
const mergeParams = ctx.action?.mergeParams;
|
|
672
|
-
if (!mergeParams) {
|
|
673
|
-
ctx.throw(403, 'Permission denied');
|
|
674
|
-
}
|
|
675
|
-
mergeParams({ filter: collectionFilter(collectionName, scope.ids) });
|
|
676
|
-
|
|
677
|
-
if (actionName === 'create' && collectionName === 'gitRepositories') {
|
|
678
|
-
ctx.throw(403, 'Permission denied');
|
|
679
|
-
}
|
|
680
|
-
if (actionName === 'create' || actionName === 'update') {
|
|
681
|
-
const values = effectiveMutationValues(ctx);
|
|
682
|
-
if (collectionName === 'gitSubtreeRuns') {
|
|
683
|
-
rejectScopedAssociationMutation(ctx, SUBTREE_RUN_ASSOCIATION_SEGMENTS);
|
|
684
|
-
await requireScopedRunConfigValue(ctx, values, scope, actionName);
|
|
685
|
-
} else if (collectionName === 'gitRepositories') {
|
|
686
|
-
if (actionName === 'update') {
|
|
687
|
-
rejectScopedRepositoryConnectionMutation(ctx);
|
|
688
|
-
}
|
|
689
|
-
} else {
|
|
690
|
-
rejectScopedAssociationMutation(ctx, REPOSITORY_ASSOCIATION_SEGMENTS);
|
|
691
|
-
requireScopedRepositoryValue(ctx, values, scope, actionName);
|
|
692
|
-
}
|
|
693
|
-
}
|
|
694
|
-
|
|
695
|
-
await next();
|
|
696
|
-
}
|
|
697
|
-
|
|
698
|
-
async function repositoryIdFromSubtreeConfig(
|
|
699
|
-
ctx: RepositoryAccessContext,
|
|
700
|
-
configId: RepositoryId,
|
|
701
|
-
): Promise<RepositoryId> {
|
|
702
|
-
if (!ctx.db) ctx.throw(403, 'Permission denied');
|
|
703
|
-
const config = await ctx.db.getRepository('gitSubtreeConfigs').findOne({ filterByTk: configId });
|
|
704
|
-
if (!config) ctx.throw(404, 'Subtree configuration not found');
|
|
705
|
-
const repositoryId = asRepositoryId(getRecordValue(config, 'repositoryId'));
|
|
706
|
-
if (repositoryId == null) ctx.throw(403, 'Permission denied');
|
|
707
|
-
return repositoryId;
|
|
708
|
-
}
|
|
709
|
-
|
|
710
|
-
async function repositoryIdFromReview(ctx: RepositoryAccessContext, reviewId: RepositoryId): Promise<RepositoryId> {
|
|
711
|
-
if (!ctx.db) ctx.throw(403, 'Permission denied');
|
|
712
|
-
const review = await ctx.db.getRepository('gitCodeReviews').findOne({ filterByTk: reviewId });
|
|
713
|
-
if (!review) ctx.throw(404, 'Review not found');
|
|
714
|
-
const repositoryId = asRepositoryId(getRecordValue(review, 'repositoryId'));
|
|
715
|
-
if (repositoryId == null) ctx.throw(403, 'Permission denied');
|
|
716
|
-
return repositoryId;
|
|
717
|
-
}
|
|
718
|
-
|
|
719
|
-
export async function enforceRepositoryAccess(ctx: RepositoryAccessContext, next: () => Promise<void>) {
|
|
720
|
-
const action = ctx.action;
|
|
721
|
-
const actionName = action?.actionName;
|
|
722
|
-
if (action?.resourceName !== 'gitManager' || !actionName) {
|
|
723
|
-
return next();
|
|
724
|
-
}
|
|
725
|
-
|
|
726
|
-
const currentRoles = ctx.state?.currentRoles;
|
|
727
|
-
const roles = Array.isArray(currentRoles)
|
|
728
|
-
? currentRoles.filter((role): role is string => typeof role === 'string')
|
|
729
|
-
: [];
|
|
730
|
-
if (Array.isArray(currentRoles) && roles.length !== currentRoles.length) {
|
|
731
|
-
return ctx.throw(403, 'Permission denied');
|
|
732
|
-
}
|
|
733
|
-
|
|
734
|
-
const params = getEffectiveActionParams(ctx);
|
|
735
|
-
let repositoryId: RepositoryId | undefined;
|
|
736
|
-
if (REPOSITORY_ID_ACTIONS.has(actionName)) {
|
|
737
|
-
repositoryId = asRepositoryId(params.repositoryId);
|
|
738
|
-
if (repositoryId == null) return ctx.throw(400, 'repositoryId is required');
|
|
739
|
-
} else if (SUBTREE_CONFIG_ACTIONS.has(actionName)) {
|
|
740
|
-
const configId = asRepositoryId(params.configId);
|
|
741
|
-
if (configId == null) return ctx.throw(400, 'configId is required');
|
|
742
|
-
repositoryId = await repositoryIdFromSubtreeConfig(ctx, configId);
|
|
743
|
-
} else if (REVIEW_ID_ACTIONS.has(actionName)) {
|
|
744
|
-
const reviewId = asRepositoryId(params.reviewId);
|
|
745
|
-
if (reviewId == null) return ctx.throw(400, 'reviewId is required');
|
|
746
|
-
repositoryId = await repositoryIdFromReview(ctx, reviewId);
|
|
747
|
-
} else if (actionName === 'pollNow') {
|
|
748
|
-
repositoryId = asRepositoryId(params.repositoryId);
|
|
749
|
-
if (repositoryId == null) {
|
|
750
|
-
const allowed = await hasUnrestrictedRepositoryAccess({
|
|
751
|
-
acl: ctx.app.acl,
|
|
752
|
-
roles,
|
|
753
|
-
action: actionName,
|
|
754
|
-
});
|
|
755
|
-
if (!allowed) return ctx.throw(403, 'Permission denied');
|
|
756
|
-
return next();
|
|
757
|
-
}
|
|
758
|
-
} else {
|
|
759
|
-
return next();
|
|
760
|
-
}
|
|
761
|
-
|
|
762
|
-
const allowed = await hasRepositoryAccess({
|
|
763
|
-
acl: ctx.app.acl,
|
|
764
|
-
roles,
|
|
765
|
-
repositoryId,
|
|
766
|
-
action: actionName,
|
|
767
|
-
});
|
|
768
|
-
if (!allowed) {
|
|
769
|
-
return ctx.throw(403, 'Permission denied');
|
|
770
|
-
}
|
|
771
|
-
|
|
772
|
-
return next();
|
|
773
|
-
}
|
|
1
|
+
const REPOSITORY_ID_ACTIONS = new Set([
|
|
2
|
+
'clone',
|
|
3
|
+
'pull',
|
|
4
|
+
'push',
|
|
5
|
+
'fetch',
|
|
6
|
+
'diff',
|
|
7
|
+
'status',
|
|
8
|
+
'log',
|
|
9
|
+
'branches',
|
|
10
|
+
'checkout',
|
|
11
|
+
'fileTree',
|
|
12
|
+
'fileContent',
|
|
13
|
+
'commitDetail',
|
|
14
|
+
'mergeRequests',
|
|
15
|
+
'mergeRequestDetail',
|
|
16
|
+
'mergeRequestNotes',
|
|
17
|
+
'triggerReview',
|
|
18
|
+
'subtreeOptions',
|
|
19
|
+
]);
|
|
20
|
+
|
|
21
|
+
const SUBTREE_CONFIG_ACTIONS = new Set(['subtreePreview', 'subtreeRun', 'subtreeReplace']);
|
|
22
|
+
|
|
23
|
+
const REVIEW_ID_ACTIONS = new Set(['reviewApprovePost', 'reviewReject']);
|
|
24
|
+
|
|
25
|
+
type RepositoryId = number | string;
|
|
26
|
+
|
|
27
|
+
type ScopedCollectionName =
|
|
28
|
+
| 'gitRepositories'
|
|
29
|
+
| 'gitAccounts'
|
|
30
|
+
| 'gitReviewFlows'
|
|
31
|
+
| 'gitCodeReviews'
|
|
32
|
+
| 'gitSubtreeConfigs'
|
|
33
|
+
| 'gitSubtreeRuns';
|
|
34
|
+
|
|
35
|
+
type ActionParams = Record<string, unknown> & { values?: unknown };
|
|
36
|
+
|
|
37
|
+
type RecordWithGetter = Record<string, unknown> & {
|
|
38
|
+
get?: (attribute: string) => unknown;
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
export type RegistryGitAccessContext =
|
|
42
|
+
| {
|
|
43
|
+
kind: 'user';
|
|
44
|
+
roles: string[];
|
|
45
|
+
}
|
|
46
|
+
| {
|
|
47
|
+
kind: 'system';
|
|
48
|
+
reason: 'scheduled-sync';
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
export type RepositoryPermissionResult = { params?: { filter?: unknown } } | null;
|
|
52
|
+
|
|
53
|
+
type ResolvedRepositoryScope = {
|
|
54
|
+
ids: RepositoryId[];
|
|
55
|
+
unrestricted: boolean;
|
|
56
|
+
supported: boolean;
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
export interface RepositoryPermissionChecker {
|
|
60
|
+
can(input: {
|
|
61
|
+
roles: string[];
|
|
62
|
+
resource: string;
|
|
63
|
+
action: string;
|
|
64
|
+
}): Promise<RepositoryPermissionResult> | RepositoryPermissionResult;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
type RepositoryLookup = {
|
|
68
|
+
findOne(input: { filterByTk: RepositoryId }): Promise<unknown>;
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
type RepositoryAccessDatabase = {
|
|
72
|
+
getRepository(name: 'gitSubtreeConfigs' | 'gitCodeReviews'): RepositoryLookup;
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
function asRecord(value: unknown): Record<string, unknown> {
|
|
76
|
+
return value && typeof value === 'object' && !Array.isArray(value) ? (value as Record<string, unknown>) : {};
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
function asRepositoryId(value: unknown): RepositoryId | undefined {
|
|
80
|
+
if (typeof value === 'number' && Number.isFinite(value)) return value;
|
|
81
|
+
if (typeof value === 'string' && value.trim()) return value;
|
|
82
|
+
return undefined;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
function getRecordValue(record: unknown, attribute: string): unknown {
|
|
86
|
+
if (!record || typeof record !== 'object') return undefined;
|
|
87
|
+
const model = record as RecordWithGetter;
|
|
88
|
+
return typeof model.get === 'function' ? model.get(attribute) : model[attribute];
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* NocoBase actions can receive values in the URL parameters, the action
|
|
93
|
+
* values envelope, or the request body. Keep this precedence identical to
|
|
94
|
+
* the actions themselves so authorization always checks the repository that
|
|
95
|
+
* will actually be used.
|
|
96
|
+
*/
|
|
97
|
+
export function getEffectiveActionParams(ctx: {
|
|
98
|
+
action?: { params?: ActionParams };
|
|
99
|
+
request?: { body?: unknown };
|
|
100
|
+
}): Record<string, unknown> {
|
|
101
|
+
const params = asRecord(ctx.action?.params);
|
|
102
|
+
return {
|
|
103
|
+
...params,
|
|
104
|
+
...asRecord(params.values),
|
|
105
|
+
...asRecord(ctx.request?.body),
|
|
106
|
+
};
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
function supportedRepositoryIds(ids: Iterable<RepositoryId>): ResolvedRepositoryScope {
|
|
110
|
+
const uniqueIds: RepositoryId[] = [];
|
|
111
|
+
for (const id of ids) {
|
|
112
|
+
if (!uniqueIds.some((candidate) => String(candidate) === String(id))) {
|
|
113
|
+
uniqueIds.push(id);
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
return { ids: uniqueIds, unrestricted: false, supported: true };
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
function unsupportedRepositoryScope(): ResolvedRepositoryScope {
|
|
120
|
+
return { ids: [], unrestricted: false, supported: false };
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
function resolveRepositoryScopeBranches(operator: '$and' | '$or', branches: unknown[]): ResolvedRepositoryScope {
|
|
124
|
+
const resolutions = branches.map(resolveRepositoryScope);
|
|
125
|
+
if (resolutions.some((resolution) => !resolution.supported)) {
|
|
126
|
+
return unsupportedRepositoryScope();
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
if (operator === '$or') {
|
|
130
|
+
if (resolutions.some((resolution) => resolution.unrestricted)) {
|
|
131
|
+
return { ids: [], unrestricted: true, supported: true };
|
|
132
|
+
}
|
|
133
|
+
return supportedRepositoryIds(resolutions.flatMap((resolution) => resolution.ids));
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
const restricted = resolutions.filter((resolution) => !resolution.unrestricted);
|
|
137
|
+
if (restricted.length === 0) {
|
|
138
|
+
return { ids: [], unrestricted: true, supported: true };
|
|
139
|
+
}
|
|
140
|
+
const [first, ...rest] = restricted;
|
|
141
|
+
return supportedRepositoryIds(
|
|
142
|
+
first.ids.filter((id) =>
|
|
143
|
+
rest.every((resolution) => resolution.ids.some((candidate) => String(candidate) === String(id))),
|
|
144
|
+
),
|
|
145
|
+
);
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/**
|
|
149
|
+
* Parses the limited ID-only scope grammar written by Repository Permissions.
|
|
150
|
+
* An unknown condition must fail closed: translating it to a collection filter
|
|
151
|
+
* could otherwise turn a restrictive scope into a broader one.
|
|
152
|
+
*/
|
|
153
|
+
export function resolveRepositoryScope(scope: unknown): ResolvedRepositoryScope {
|
|
154
|
+
if (scope == null) {
|
|
155
|
+
return { ids: [], unrestricted: true, supported: true };
|
|
156
|
+
}
|
|
157
|
+
if (typeof scope !== 'object' || Array.isArray(scope)) {
|
|
158
|
+
return unsupportedRepositoryScope();
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
const condition = scope as Record<string, unknown>;
|
|
162
|
+
const keys = Object.keys(condition);
|
|
163
|
+
if (keys.length === 0) {
|
|
164
|
+
return { ids: [], unrestricted: true, supported: true };
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
const operator = keys.find((key) => key === '$and' || key === '$or');
|
|
168
|
+
if (operator) {
|
|
169
|
+
if (keys.length !== 1 || !Array.isArray(condition[operator])) {
|
|
170
|
+
return unsupportedRepositoryScope();
|
|
171
|
+
}
|
|
172
|
+
return resolveRepositoryScopeBranches(operator as '$and' | '$or', condition[operator] as unknown[]);
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
if (keys.length !== 1 || !('id' in condition)) {
|
|
176
|
+
return unsupportedRepositoryScope();
|
|
177
|
+
}
|
|
178
|
+
const id = condition.id;
|
|
179
|
+
if (typeof id === 'number' || typeof id === 'string') {
|
|
180
|
+
return supportedRepositoryIds([id]);
|
|
181
|
+
}
|
|
182
|
+
if (!id || typeof id !== 'object' || Array.isArray(id)) {
|
|
183
|
+
return unsupportedRepositoryScope();
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
const idCondition = id as Record<string, unknown>;
|
|
187
|
+
const idKeys = Object.keys(idCondition);
|
|
188
|
+
if (idKeys.length !== 1) {
|
|
189
|
+
return unsupportedRepositoryScope();
|
|
190
|
+
}
|
|
191
|
+
const equal = idCondition.$eq;
|
|
192
|
+
if (typeof equal === 'number' || typeof equal === 'string') {
|
|
193
|
+
return supportedRepositoryIds([equal]);
|
|
194
|
+
}
|
|
195
|
+
const included = idCondition.$in;
|
|
196
|
+
if (Array.isArray(included) && included.every((value) => typeof value === 'number' || typeof value === 'string')) {
|
|
197
|
+
return supportedRepositoryIds(included as RepositoryId[]);
|
|
198
|
+
}
|
|
199
|
+
return unsupportedRepositoryScope();
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
export function scopeIncludesRepository(filter: unknown, repositoryId: number | string): boolean {
|
|
203
|
+
const scope = resolveRepositoryScope(filter);
|
|
204
|
+
return scope.supported && (scope.unrestricted || scope.ids.some((id) => String(id) === String(repositoryId)));
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
/**
|
|
208
|
+
* Checks the same Git Manager repository scope used by HTTP actions. Registry
|
|
209
|
+
* content reads use `fileContent` because a source sync ultimately reads file
|
|
210
|
+
* bytes, even when its first request is a tree listing.
|
|
211
|
+
*/
|
|
212
|
+
export async function hasRepositoryAccess(input: {
|
|
213
|
+
acl?: RepositoryPermissionChecker;
|
|
214
|
+
roles: string[];
|
|
215
|
+
repositoryId: number | string;
|
|
216
|
+
action?: string;
|
|
217
|
+
}): Promise<boolean> {
|
|
218
|
+
if (input.roles.includes('root')) {
|
|
219
|
+
return true;
|
|
220
|
+
}
|
|
221
|
+
if (!input.acl) {
|
|
222
|
+
return false;
|
|
223
|
+
}
|
|
224
|
+
const permission = await input.acl.can({
|
|
225
|
+
roles: input.roles,
|
|
226
|
+
resource: 'gitManager',
|
|
227
|
+
action: input.action || 'fileContent',
|
|
228
|
+
});
|
|
229
|
+
return Boolean(permission && scopeIncludesRepository(permission.params?.filter, input.repositoryId));
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
async function hasUnrestrictedRepositoryAccess(input: {
|
|
233
|
+
acl?: RepositoryPermissionChecker;
|
|
234
|
+
roles: string[];
|
|
235
|
+
action: string;
|
|
236
|
+
}): Promise<boolean> {
|
|
237
|
+
if (input.roles.includes('root')) return true;
|
|
238
|
+
if (!input.acl) return false;
|
|
239
|
+
const permission = await input.acl.can({
|
|
240
|
+
roles: input.roles,
|
|
241
|
+
resource: 'gitManager',
|
|
242
|
+
action: input.action,
|
|
243
|
+
});
|
|
244
|
+
const scope = resolveRepositoryScope(permission?.params?.filter);
|
|
245
|
+
return Boolean(permission && scope.supported && scope.unrestricted);
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
type RepositoryAccessContext = {
|
|
249
|
+
action?: {
|
|
250
|
+
resourceName?: string;
|
|
251
|
+
actionName?: string;
|
|
252
|
+
params?: ActionParams;
|
|
253
|
+
};
|
|
254
|
+
state?: { currentRoles?: unknown };
|
|
255
|
+
request?: { body?: unknown };
|
|
256
|
+
db?: RepositoryAccessDatabase;
|
|
257
|
+
app: { acl: RepositoryPermissionChecker };
|
|
258
|
+
throw(status: number, message: string): never;
|
|
259
|
+
};
|
|
260
|
+
|
|
261
|
+
type RepositoryCollectionAccessContext = {
|
|
262
|
+
action?: {
|
|
263
|
+
resourceName?: string;
|
|
264
|
+
actionName?: string;
|
|
265
|
+
params?: ActionParams;
|
|
266
|
+
mergeParams?: (params: { filter: unknown }) => void;
|
|
267
|
+
};
|
|
268
|
+
state?: { currentRoles?: unknown };
|
|
269
|
+
query?: unknown;
|
|
270
|
+
request?: { body?: unknown; query?: unknown };
|
|
271
|
+
db?: {
|
|
272
|
+
getRepository(name: 'gitSubtreeConfigs'): RepositoryLookup;
|
|
273
|
+
};
|
|
274
|
+
app: { acl: RepositoryPermissionChecker };
|
|
275
|
+
getCurrentRepository?: () => { targetCollection?: { name?: string } } | null;
|
|
276
|
+
throw(status: number, message: string): never;
|
|
277
|
+
};
|
|
278
|
+
|
|
279
|
+
const COLLECTION_SCOPE_FIELDS: Record<Exclude<ScopedCollectionName, 'gitAccounts'>, string> = {
|
|
280
|
+
gitRepositories: 'id',
|
|
281
|
+
gitReviewFlows: 'repositoryId',
|
|
282
|
+
gitCodeReviews: 'repositoryId',
|
|
283
|
+
gitSubtreeConfigs: 'repositoryId',
|
|
284
|
+
gitSubtreeRuns: 'config.repositoryId',
|
|
285
|
+
};
|
|
286
|
+
|
|
287
|
+
const STANDARD_COLLECTION_ACTIONS = new Set(['list', 'get', 'create', 'update', 'destroy']);
|
|
288
|
+
|
|
289
|
+
function currentRoles(ctx: {
|
|
290
|
+
state?: { currentRoles?: unknown };
|
|
291
|
+
throw(status: number, message: string): never;
|
|
292
|
+
}): string[] {
|
|
293
|
+
const rawRoles = ctx.state?.currentRoles;
|
|
294
|
+
const roles = Array.isArray(rawRoles) ? rawRoles.filter((role): role is string => typeof role === 'string') : [];
|
|
295
|
+
if (Array.isArray(rawRoles) && roles.length !== rawRoles.length) {
|
|
296
|
+
return ctx.throw(403, 'Permission denied');
|
|
297
|
+
}
|
|
298
|
+
return roles;
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
function scopedCollectionName(ctx: RepositoryCollectionAccessContext): ScopedCollectionName | null {
|
|
302
|
+
let targetCollectionName: string | undefined;
|
|
303
|
+
try {
|
|
304
|
+
targetCollectionName = ctx.getCurrentRepository?.()?.targetCollection?.name;
|
|
305
|
+
} catch {
|
|
306
|
+
// Some custom resource routes do not resolve a backing repository. Their
|
|
307
|
+
// route parameters below are still sufficient to identify a scoped target.
|
|
308
|
+
}
|
|
309
|
+
if (targetCollectionName && Object.prototype.hasOwnProperty.call(COLLECTION_SCOPE_FIELDS, targetCollectionName)) {
|
|
310
|
+
return targetCollectionName as Exclude<ScopedCollectionName, 'gitAccounts'>;
|
|
311
|
+
}
|
|
312
|
+
if (targetCollectionName === 'gitAccounts') {
|
|
313
|
+
return targetCollectionName;
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
const params = asRecord(ctx.action?.params);
|
|
317
|
+
const candidates = [ctx.action?.resourceName, params.resourceName, params.associatedName, params.targetCollection]
|
|
318
|
+
.filter((value): value is string => typeof value === 'string')
|
|
319
|
+
.flatMap((value) => value.split('.'));
|
|
320
|
+
return (
|
|
321
|
+
candidates.find(
|
|
322
|
+
(candidate): candidate is ScopedCollectionName =>
|
|
323
|
+
candidate === 'gitAccounts' || Object.prototype.hasOwnProperty.call(COLLECTION_SCOPE_FIELDS, candidate),
|
|
324
|
+
) || null
|
|
325
|
+
);
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
function collectionFilter(collectionName: Exclude<ScopedCollectionName, 'gitAccounts'>, ids: RepositoryId[]): unknown {
|
|
329
|
+
if (collectionName === 'gitSubtreeRuns') {
|
|
330
|
+
return { config: { repositoryId: { $in: ids } } };
|
|
331
|
+
}
|
|
332
|
+
return { [COLLECTION_SCOPE_FIELDS[collectionName]]: { $in: ids } };
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
function effectiveMutationValues(ctx: RepositoryCollectionAccessContext): Record<string, unknown> {
|
|
336
|
+
const body = asRecord(ctx.request?.body);
|
|
337
|
+
return { ...asRecord(body.values), ...getEffectiveActionParams(ctx) };
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
/**
|
|
341
|
+
* `targetCollection` is forwarded by the generic repository actions. If a
|
|
342
|
+
* request changes it after routing has selected a Git Manager collection, the
|
|
343
|
+
* repository implementation can execute against a different collection while
|
|
344
|
+
* ACL and repository-scope filters still describe the original one. The
|
|
345
|
+
* `__collection` value is the equivalent association payload form.
|
|
346
|
+
*/
|
|
347
|
+
function rejectTargetCollectionOverride(
|
|
348
|
+
ctx: RepositoryCollectionAccessContext,
|
|
349
|
+
collectionName: ScopedCollectionName,
|
|
350
|
+
): void {
|
|
351
|
+
const actionParams = asRecord(ctx.action?.params);
|
|
352
|
+
const requestBody = asRecord(ctx.request?.body);
|
|
353
|
+
const actionValues = asRecord(actionParams.values);
|
|
354
|
+
const bodyValues = asRecord(requestBody.values);
|
|
355
|
+
const requestedCollections = [
|
|
356
|
+
actionParams.targetCollection,
|
|
357
|
+
actionParams.__collection,
|
|
358
|
+
actionValues.targetCollection,
|
|
359
|
+
actionValues.__collection,
|
|
360
|
+
requestBody.targetCollection,
|
|
361
|
+
requestBody.__collection,
|
|
362
|
+
bodyValues.targetCollection,
|
|
363
|
+
bodyValues.__collection,
|
|
364
|
+
];
|
|
365
|
+
for (const requestedCollection of requestedCollections) {
|
|
366
|
+
if (requestedCollection == null || requestedCollection === collectionName) continue;
|
|
367
|
+
ctx.throw(403, 'Permission denied');
|
|
368
|
+
}
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
/**
|
|
372
|
+
* A repository scope limits which repository rows a role can edit; it does not
|
|
373
|
+
* grant the role use of every shared Git credential. Otherwise a user could
|
|
374
|
+
* attach a guessed global account to an allowed repository, change its URL or
|
|
375
|
+
* local checkout, and use that account's PAT or another repository's clone
|
|
376
|
+
* through a permitted Git action.
|
|
377
|
+
*/
|
|
378
|
+
function rejectScopedRepositoryConnectionMutation(ctx: RepositoryCollectionAccessContext): void {
|
|
379
|
+
const valueSources = [ctx.action?.params, ctx.query, ctx.request?.query, ctx.request?.body];
|
|
380
|
+
if (valueSources.some((values) => containsMutationField(values, REPOSITORY_CONNECTION_FIELD_SEGMENTS))) {
|
|
381
|
+
ctx.throw(403, 'Permission denied');
|
|
382
|
+
}
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
function rejectScopedAssociationMutation(
|
|
386
|
+
ctx: RepositoryCollectionAccessContext,
|
|
387
|
+
associationSegments: ReadonlySet<string>,
|
|
388
|
+
): void {
|
|
389
|
+
const valueSources = [ctx.action?.params, ctx.query, ctx.request?.query, ctx.request?.body];
|
|
390
|
+
if (valueSources.some((values) => containsMutationField(values, associationSegments))) {
|
|
391
|
+
ctx.throw(403, 'Permission denied');
|
|
392
|
+
}
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
/**
|
|
396
|
+
* Standard collection handlers can interpret an array as a bulk update and
|
|
397
|
+
* apply it after the scoped filter has been merged. This policy only supports
|
|
398
|
+
* one scoped mutation at a time, so reject arrays instead of attempting to
|
|
399
|
+
* validate a partially normalized bulk payload.
|
|
400
|
+
*/
|
|
401
|
+
function rejectScopedBulkMutationValues(ctx: RepositoryCollectionAccessContext, actionName: string): void {
|
|
402
|
+
if (actionName !== 'create' && actionName !== 'update') {
|
|
403
|
+
return;
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
const actionParams = asRecord(ctx.action?.params);
|
|
407
|
+
const requestBody = ctx.request?.body;
|
|
408
|
+
const bodyValues = asRecord(requestBody).values;
|
|
409
|
+
if (Array.isArray(actionParams.values) || Array.isArray(requestBody) || Array.isArray(bodyValues)) {
|
|
410
|
+
ctx.throw(403, 'Permission denied');
|
|
411
|
+
}
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
const ASSOCIATION_READ_PARAM_KEYS = ['appends', 'include', 'fields'] as const;
|
|
415
|
+
const CREDENTIAL_ASSOCIATION_SEGMENTS = new Set(['gitAccount', 'gitAccounts']);
|
|
416
|
+
const REPOSITORY_CONNECTION_FIELD_SEGMENTS = new Set(['gitAccount', 'gitAccountId', 'repoUrl', 'localPath']);
|
|
417
|
+
const REPOSITORY_ASSOCIATION_SEGMENTS = new Set(['repository']);
|
|
418
|
+
const SUBTREE_RUN_ASSOCIATION_SEGMENTS = new Set(['config']);
|
|
419
|
+
|
|
420
|
+
function pathSegments(value: string): string[] {
|
|
421
|
+
return value.split(/[.[\],:/()=\s'"]+/);
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
function containsCredentialAssociationSegment(value: string): boolean {
|
|
425
|
+
return pathSegments(value).some((segment) => CREDENTIAL_ASSOCIATION_SEGMENTS.has(segment));
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
/**
|
|
429
|
+
* UpdateGuard expands dotted and bracket notation after middleware runs. Walk
|
|
430
|
+
* request object keys recursively so an attacker cannot smuggle a protected
|
|
431
|
+
* connection or association field through dotted, bracket, or nested
|
|
432
|
+
* association-update payloads.
|
|
433
|
+
*/
|
|
434
|
+
function containsMutationField(
|
|
435
|
+
value: unknown,
|
|
436
|
+
fieldSegments: ReadonlySet<string>,
|
|
437
|
+
visited = new WeakSet<object>(),
|
|
438
|
+
): boolean {
|
|
439
|
+
if (!value || typeof value !== 'object') {
|
|
440
|
+
return false;
|
|
441
|
+
}
|
|
442
|
+
if (visited.has(value)) {
|
|
443
|
+
return false;
|
|
444
|
+
}
|
|
445
|
+
visited.add(value);
|
|
446
|
+
|
|
447
|
+
if (Array.isArray(value)) {
|
|
448
|
+
return value.some((item) => containsMutationField(item, fieldSegments, visited));
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
return Object.entries(value as Record<string, unknown>).some(
|
|
452
|
+
([key, item]) =>
|
|
453
|
+
pathSegments(key).some((segment) => fieldSegments.has(segment)) ||
|
|
454
|
+
containsMutationField(item, fieldSegments, visited),
|
|
455
|
+
);
|
|
456
|
+
}
|
|
457
|
+
|
|
458
|
+
/**
|
|
459
|
+
* Generic collection reads accept association projections in more than one
|
|
460
|
+
* shape: an `appends` array, an ORM-style `include` object, or a relation
|
|
461
|
+
* field path. Limit the inspection to those projection parameters so a normal
|
|
462
|
+
* filter value cannot accidentally be treated as a credential request.
|
|
463
|
+
*/
|
|
464
|
+
function containsCredentialAssociation(value: unknown, visited = new WeakSet<object>()): boolean {
|
|
465
|
+
if (typeof value === 'string') {
|
|
466
|
+
return containsCredentialAssociationSegment(value);
|
|
467
|
+
}
|
|
468
|
+
if (!value || typeof value !== 'object') {
|
|
469
|
+
return false;
|
|
470
|
+
}
|
|
471
|
+
if (visited.has(value)) {
|
|
472
|
+
return false;
|
|
473
|
+
}
|
|
474
|
+
visited.add(value);
|
|
475
|
+
|
|
476
|
+
if (Array.isArray(value)) {
|
|
477
|
+
return value.some((item) => containsCredentialAssociation(item, visited));
|
|
478
|
+
}
|
|
479
|
+
|
|
480
|
+
return Object.entries(value as Record<string, unknown>).some(
|
|
481
|
+
([key, item]) => containsCredentialAssociationSegment(key) || containsCredentialAssociation(item, visited),
|
|
482
|
+
);
|
|
483
|
+
}
|
|
484
|
+
|
|
485
|
+
function isAssociationReadParamKey(key: string): boolean {
|
|
486
|
+
return ASSOCIATION_READ_PARAM_KEYS.some(
|
|
487
|
+
(paramKey) => key === paramKey || key.startsWith(`${paramKey}[`) || key.startsWith(`${paramKey}.`),
|
|
488
|
+
);
|
|
489
|
+
}
|
|
490
|
+
|
|
491
|
+
function rejectScopedCredentialAssociationRoute(ctx: RepositoryCollectionAccessContext): void {
|
|
492
|
+
const actionParams = asRecord(ctx.action?.params);
|
|
493
|
+
const requestBody = asRecord(ctx.request?.body);
|
|
494
|
+
const routeValues = [
|
|
495
|
+
ctx.action?.resourceName,
|
|
496
|
+
actionParams.resourceName,
|
|
497
|
+
actionParams.associatedName,
|
|
498
|
+
actionParams.targetCollection,
|
|
499
|
+
requestBody.resourceName,
|
|
500
|
+
requestBody.associatedName,
|
|
501
|
+
requestBody.targetCollection,
|
|
502
|
+
asRecord(actionParams.values).resourceName,
|
|
503
|
+
asRecord(actionParams.values).associatedName,
|
|
504
|
+
asRecord(actionParams.values).targetCollection,
|
|
505
|
+
asRecord(requestBody.values).resourceName,
|
|
506
|
+
asRecord(requestBody.values).associatedName,
|
|
507
|
+
asRecord(requestBody.values).targetCollection,
|
|
508
|
+
asRecord(ctx.query).resourceName,
|
|
509
|
+
asRecord(ctx.query).associatedName,
|
|
510
|
+
asRecord(ctx.query).targetCollection,
|
|
511
|
+
asRecord(ctx.request?.query).resourceName,
|
|
512
|
+
asRecord(ctx.request?.query).associatedName,
|
|
513
|
+
asRecord(ctx.request?.query).targetCollection,
|
|
514
|
+
];
|
|
515
|
+
if (routeValues.some((value) => typeof value === 'string' && containsCredentialAssociationSegment(value))) {
|
|
516
|
+
ctx.throw(403, 'Permission denied');
|
|
517
|
+
}
|
|
518
|
+
}
|
|
519
|
+
|
|
520
|
+
/**
|
|
521
|
+
* A scoped role is allowed to read only selected repository rows, not the
|
|
522
|
+
* global Git account that happens to be attached to those rows. An `appends`
|
|
523
|
+
* or `include` projection of `gitAccount` would otherwise serialize its PAT
|
|
524
|
+
* through the permitted gitRepositories endpoint.
|
|
525
|
+
*/
|
|
526
|
+
function rejectScopedRepositoryCredentialAssociationRead(
|
|
527
|
+
ctx: RepositoryCollectionAccessContext,
|
|
528
|
+
actionName: string,
|
|
529
|
+
): void {
|
|
530
|
+
if (actionName !== 'list' && actionName !== 'get') {
|
|
531
|
+
return;
|
|
532
|
+
}
|
|
533
|
+
|
|
534
|
+
const actionParams = asRecord(ctx.action?.params);
|
|
535
|
+
const requestBody = asRecord(ctx.request?.body);
|
|
536
|
+
const valueSources = [
|
|
537
|
+
actionParams,
|
|
538
|
+
asRecord(actionParams.values),
|
|
539
|
+
asRecord(ctx.query),
|
|
540
|
+
asRecord(ctx.request?.query),
|
|
541
|
+
requestBody,
|
|
542
|
+
asRecord(requestBody.values),
|
|
543
|
+
];
|
|
544
|
+
for (const values of valueSources) {
|
|
545
|
+
for (const [key, value] of Object.entries(values)) {
|
|
546
|
+
if (isAssociationReadParamKey(key) && containsCredentialAssociation(value)) {
|
|
547
|
+
ctx.throw(403, 'Permission denied');
|
|
548
|
+
}
|
|
549
|
+
}
|
|
550
|
+
}
|
|
551
|
+
}
|
|
552
|
+
|
|
553
|
+
function scopeAllowsRepository(scope: ResolvedRepositoryScope, repositoryId: RepositoryId): boolean {
|
|
554
|
+
return scope.unrestricted || scope.ids.some((id) => String(id) === String(repositoryId));
|
|
555
|
+
}
|
|
556
|
+
|
|
557
|
+
function hasOwn(object: Record<string, unknown>, key: string): boolean {
|
|
558
|
+
return Object.prototype.hasOwnProperty.call(object, key);
|
|
559
|
+
}
|
|
560
|
+
|
|
561
|
+
function requireScopedRepositoryValue(
|
|
562
|
+
ctx: RepositoryCollectionAccessContext,
|
|
563
|
+
values: Record<string, unknown>,
|
|
564
|
+
scope: ResolvedRepositoryScope,
|
|
565
|
+
actionName: string,
|
|
566
|
+
): void {
|
|
567
|
+
if (hasOwn(values, 'repository')) {
|
|
568
|
+
ctx.throw(403, 'Permission denied');
|
|
569
|
+
}
|
|
570
|
+
if (!hasOwn(values, 'repositoryId')) {
|
|
571
|
+
if (actionName === 'create') {
|
|
572
|
+
ctx.throw(400, 'repositoryId is required');
|
|
573
|
+
}
|
|
574
|
+
return;
|
|
575
|
+
}
|
|
576
|
+
const repositoryId = asRepositoryId(values.repositoryId);
|
|
577
|
+
if (repositoryId == null || !scopeAllowsRepository(scope, repositoryId)) {
|
|
578
|
+
ctx.throw(403, 'Permission denied');
|
|
579
|
+
}
|
|
580
|
+
}
|
|
581
|
+
|
|
582
|
+
async function requireScopedRunConfigValue(
|
|
583
|
+
ctx: RepositoryCollectionAccessContext,
|
|
584
|
+
values: Record<string, unknown>,
|
|
585
|
+
scope: ResolvedRepositoryScope,
|
|
586
|
+
actionName: string,
|
|
587
|
+
): Promise<void> {
|
|
588
|
+
if (hasOwn(values, 'config')) {
|
|
589
|
+
ctx.throw(403, 'Permission denied');
|
|
590
|
+
}
|
|
591
|
+
if (!hasOwn(values, 'configId')) {
|
|
592
|
+
if (actionName === 'create') {
|
|
593
|
+
ctx.throw(400, 'configId is required');
|
|
594
|
+
}
|
|
595
|
+
return;
|
|
596
|
+
}
|
|
597
|
+
const configId = asRepositoryId(values.configId);
|
|
598
|
+
const database = ctx.db;
|
|
599
|
+
if (configId == null || !database) {
|
|
600
|
+
ctx.throw(403, 'Permission denied');
|
|
601
|
+
}
|
|
602
|
+
const config = await database.getRepository('gitSubtreeConfigs').findOne({ filterByTk: configId });
|
|
603
|
+
if (!config) {
|
|
604
|
+
ctx.throw(404, 'Subtree configuration not found');
|
|
605
|
+
}
|
|
606
|
+
const repositoryId = asRepositoryId(getRecordValue(config, 'repositoryId'));
|
|
607
|
+
if (repositoryId == null || !scopeAllowsRepository(scope, repositoryId)) {
|
|
608
|
+
ctx.throw(403, 'Permission denied');
|
|
609
|
+
}
|
|
610
|
+
}
|
|
611
|
+
|
|
612
|
+
/**
|
|
613
|
+
* Repository Permissions persist their scope on gitRepositories and custom
|
|
614
|
+
* gitManager actions. Generic collection snippets are otherwise unscoped, so
|
|
615
|
+
* apply the same scope before the standard collection handlers run.
|
|
616
|
+
*/
|
|
617
|
+
export async function enforceRepositoryCollectionAccess(
|
|
618
|
+
ctx: RepositoryCollectionAccessContext,
|
|
619
|
+
next: () => Promise<void>,
|
|
620
|
+
): Promise<void> {
|
|
621
|
+
const collectionName = scopedCollectionName(ctx);
|
|
622
|
+
if (!collectionName) {
|
|
623
|
+
await next();
|
|
624
|
+
return;
|
|
625
|
+
}
|
|
626
|
+
|
|
627
|
+
rejectTargetCollectionOverride(ctx, collectionName);
|
|
628
|
+
|
|
629
|
+
const roles = currentRoles(ctx);
|
|
630
|
+
if (roles.includes('root')) {
|
|
631
|
+
await next();
|
|
632
|
+
return;
|
|
633
|
+
}
|
|
634
|
+
const repositoryPermission = await ctx.app.acl.can({
|
|
635
|
+
roles,
|
|
636
|
+
resource: 'gitRepositories',
|
|
637
|
+
action: 'list',
|
|
638
|
+
});
|
|
639
|
+
// No Repository Permissions action exists for this role. Preserve the
|
|
640
|
+
// independently configured collection ACL instead of inventing a scope.
|
|
641
|
+
if (!repositoryPermission) {
|
|
642
|
+
await next();
|
|
643
|
+
return;
|
|
644
|
+
}
|
|
645
|
+
const scope = resolveRepositoryScope(repositoryPermission.params?.filter);
|
|
646
|
+
if (!scope.supported) {
|
|
647
|
+
ctx.throw(403, 'Permission denied');
|
|
648
|
+
}
|
|
649
|
+
if (scope.unrestricted) {
|
|
650
|
+
await next();
|
|
651
|
+
return;
|
|
652
|
+
}
|
|
653
|
+
|
|
654
|
+
const actionName = ctx.action?.actionName;
|
|
655
|
+
if (!actionName || !STANDARD_COLLECTION_ACTIONS.has(actionName)) {
|
|
656
|
+
ctx.throw(403, 'Permission denied');
|
|
657
|
+
}
|
|
658
|
+
if (collectionName === 'gitAccounts') {
|
|
659
|
+
ctx.throw(403, 'Permission denied');
|
|
660
|
+
}
|
|
661
|
+
if (collectionName === 'gitCodeReviews' && (actionName === 'create' || actionName === 'update')) {
|
|
662
|
+
// Review rows are durable worker jobs. Their flow, metadata, status and
|
|
663
|
+
// session fields control execution, so scoped users must use the checked
|
|
664
|
+
// gitManager review actions instead of generic collection mutations.
|
|
665
|
+
ctx.throw(403, 'Permission denied');
|
|
666
|
+
}
|
|
667
|
+
rejectScopedCredentialAssociationRoute(ctx);
|
|
668
|
+
rejectScopedRepositoryCredentialAssociationRead(ctx, actionName);
|
|
669
|
+
rejectScopedBulkMutationValues(ctx, actionName);
|
|
670
|
+
|
|
671
|
+
const mergeParams = ctx.action?.mergeParams;
|
|
672
|
+
if (!mergeParams) {
|
|
673
|
+
ctx.throw(403, 'Permission denied');
|
|
674
|
+
}
|
|
675
|
+
mergeParams({ filter: collectionFilter(collectionName, scope.ids) });
|
|
676
|
+
|
|
677
|
+
if (actionName === 'create' && collectionName === 'gitRepositories') {
|
|
678
|
+
ctx.throw(403, 'Permission denied');
|
|
679
|
+
}
|
|
680
|
+
if (actionName === 'create' || actionName === 'update') {
|
|
681
|
+
const values = effectiveMutationValues(ctx);
|
|
682
|
+
if (collectionName === 'gitSubtreeRuns') {
|
|
683
|
+
rejectScopedAssociationMutation(ctx, SUBTREE_RUN_ASSOCIATION_SEGMENTS);
|
|
684
|
+
await requireScopedRunConfigValue(ctx, values, scope, actionName);
|
|
685
|
+
} else if (collectionName === 'gitRepositories') {
|
|
686
|
+
if (actionName === 'update') {
|
|
687
|
+
rejectScopedRepositoryConnectionMutation(ctx);
|
|
688
|
+
}
|
|
689
|
+
} else {
|
|
690
|
+
rejectScopedAssociationMutation(ctx, REPOSITORY_ASSOCIATION_SEGMENTS);
|
|
691
|
+
requireScopedRepositoryValue(ctx, values, scope, actionName);
|
|
692
|
+
}
|
|
693
|
+
}
|
|
694
|
+
|
|
695
|
+
await next();
|
|
696
|
+
}
|
|
697
|
+
|
|
698
|
+
async function repositoryIdFromSubtreeConfig(
|
|
699
|
+
ctx: RepositoryAccessContext,
|
|
700
|
+
configId: RepositoryId,
|
|
701
|
+
): Promise<RepositoryId> {
|
|
702
|
+
if (!ctx.db) ctx.throw(403, 'Permission denied');
|
|
703
|
+
const config = await ctx.db.getRepository('gitSubtreeConfigs').findOne({ filterByTk: configId });
|
|
704
|
+
if (!config) ctx.throw(404, 'Subtree configuration not found');
|
|
705
|
+
const repositoryId = asRepositoryId(getRecordValue(config, 'repositoryId'));
|
|
706
|
+
if (repositoryId == null) ctx.throw(403, 'Permission denied');
|
|
707
|
+
return repositoryId;
|
|
708
|
+
}
|
|
709
|
+
|
|
710
|
+
async function repositoryIdFromReview(ctx: RepositoryAccessContext, reviewId: RepositoryId): Promise<RepositoryId> {
|
|
711
|
+
if (!ctx.db) ctx.throw(403, 'Permission denied');
|
|
712
|
+
const review = await ctx.db.getRepository('gitCodeReviews').findOne({ filterByTk: reviewId });
|
|
713
|
+
if (!review) ctx.throw(404, 'Review not found');
|
|
714
|
+
const repositoryId = asRepositoryId(getRecordValue(review, 'repositoryId'));
|
|
715
|
+
if (repositoryId == null) ctx.throw(403, 'Permission denied');
|
|
716
|
+
return repositoryId;
|
|
717
|
+
}
|
|
718
|
+
|
|
719
|
+
export async function enforceRepositoryAccess(ctx: RepositoryAccessContext, next: () => Promise<void>) {
|
|
720
|
+
const action = ctx.action;
|
|
721
|
+
const actionName = action?.actionName;
|
|
722
|
+
if (action?.resourceName !== 'gitManager' || !actionName) {
|
|
723
|
+
return next();
|
|
724
|
+
}
|
|
725
|
+
|
|
726
|
+
const currentRoles = ctx.state?.currentRoles;
|
|
727
|
+
const roles = Array.isArray(currentRoles)
|
|
728
|
+
? currentRoles.filter((role): role is string => typeof role === 'string')
|
|
729
|
+
: [];
|
|
730
|
+
if (Array.isArray(currentRoles) && roles.length !== currentRoles.length) {
|
|
731
|
+
return ctx.throw(403, 'Permission denied');
|
|
732
|
+
}
|
|
733
|
+
|
|
734
|
+
const params = getEffectiveActionParams(ctx);
|
|
735
|
+
let repositoryId: RepositoryId | undefined;
|
|
736
|
+
if (REPOSITORY_ID_ACTIONS.has(actionName)) {
|
|
737
|
+
repositoryId = asRepositoryId(params.repositoryId);
|
|
738
|
+
if (repositoryId == null) return ctx.throw(400, 'repositoryId is required');
|
|
739
|
+
} else if (SUBTREE_CONFIG_ACTIONS.has(actionName)) {
|
|
740
|
+
const configId = asRepositoryId(params.configId);
|
|
741
|
+
if (configId == null) return ctx.throw(400, 'configId is required');
|
|
742
|
+
repositoryId = await repositoryIdFromSubtreeConfig(ctx, configId);
|
|
743
|
+
} else if (REVIEW_ID_ACTIONS.has(actionName)) {
|
|
744
|
+
const reviewId = asRepositoryId(params.reviewId);
|
|
745
|
+
if (reviewId == null) return ctx.throw(400, 'reviewId is required');
|
|
746
|
+
repositoryId = await repositoryIdFromReview(ctx, reviewId);
|
|
747
|
+
} else if (actionName === 'pollNow') {
|
|
748
|
+
repositoryId = asRepositoryId(params.repositoryId);
|
|
749
|
+
if (repositoryId == null) {
|
|
750
|
+
const allowed = await hasUnrestrictedRepositoryAccess({
|
|
751
|
+
acl: ctx.app.acl,
|
|
752
|
+
roles,
|
|
753
|
+
action: actionName,
|
|
754
|
+
});
|
|
755
|
+
if (!allowed) return ctx.throw(403, 'Permission denied');
|
|
756
|
+
return next();
|
|
757
|
+
}
|
|
758
|
+
} else {
|
|
759
|
+
return next();
|
|
760
|
+
}
|
|
761
|
+
|
|
762
|
+
const allowed = await hasRepositoryAccess({
|
|
763
|
+
acl: ctx.app.acl,
|
|
764
|
+
roles,
|
|
765
|
+
repositoryId,
|
|
766
|
+
action: actionName,
|
|
767
|
+
});
|
|
768
|
+
if (!allowed) {
|
|
769
|
+
return ctx.throw(403, 'Permission denied');
|
|
770
|
+
}
|
|
771
|
+
|
|
772
|
+
return next();
|
|
773
|
+
}
|