plugin-git-manager 1.2.23 → 1.2.26

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 (53) hide show
  1. package/dist/client/900.acf7d1fa22f95e71.js +10 -0
  2. package/dist/client/index.js +1 -1
  3. package/dist/client-v2/900.f32b9dc85249fc0a.js +10 -0
  4. package/dist/client-v2/index.js +1 -1
  5. package/dist/externalVersion.js +8 -8
  6. package/dist/locale/en-US.json +4 -3
  7. package/dist/locale/vi-VN.json +4 -3
  8. package/dist/locale/zh-CN.json +4 -3
  9. package/dist/server/actions/git-actions.js +21 -8
  10. package/dist/server/actions/gitlab-api.js +5 -8
  11. package/dist/server/actions/poller.js +2 -1
  12. package/dist/server/actions/review.js +50 -13
  13. package/dist/server/actions/role-permissions.js +109 -16
  14. package/dist/server/actions/subtree.js +4 -8
  15. package/dist/server/migrations/20260730000000-migrate-legacy-repository-permissions.js +23 -7
  16. package/dist/server/plugin.js +42 -19
  17. package/dist/server/repository-access.js +505 -27
  18. package/dist/server/services/registry-content-service.js +149 -32
  19. package/dist/server/utils/get-repo-account.js +5 -2
  20. package/dist/server/utils/gitlab-url.js +2 -0
  21. package/dist/server/utils/redact.js +51 -5
  22. package/dist/server/utils/url-security.js +109 -0
  23. package/package.json +4 -2
  24. package/src/client/components/RunReviewButton.tsx +437 -436
  25. package/src/client/index.tsx +2 -1
  26. package/src/locale/en-US.json +4 -3
  27. package/src/locale/vi-VN.json +4 -3
  28. package/src/locale/zh-CN.json +4 -3
  29. package/src/server/__tests__/git-actions.test.ts +24 -0
  30. package/src/server/__tests__/plugin-url-security.test.ts +17 -0
  31. package/src/server/__tests__/poller.test.ts +37 -0
  32. package/src/server/__tests__/repository-access.test.ts +695 -1
  33. package/src/server/__tests__/review.test.ts +137 -0
  34. package/src/server/__tests__/role-permissions.test.ts +123 -2
  35. package/src/server/actions/git-actions.ts +33 -10
  36. package/src/server/actions/gitlab-api.ts +5 -8
  37. package/src/server/actions/poller.ts +2 -1
  38. package/src/server/actions/review.ts +74 -15
  39. package/src/server/actions/role-permissions.ts +136 -14
  40. package/src/server/actions/subtree.ts +4 -8
  41. package/src/server/migrations/20260730000000-migrate-legacy-repository-permissions.ts +23 -8
  42. package/src/server/plugin.ts +46 -25
  43. package/src/server/repository-access.ts +737 -28
  44. package/src/server/services/__tests__/registry-content-service.test.ts +209 -13
  45. package/src/server/services/registry-content-service.ts +473 -315
  46. package/src/server/utils/__tests__/redact.test.ts +56 -0
  47. package/src/server/utils/__tests__/url-security.test.ts +85 -0
  48. package/src/server/utils/get-repo-account.ts +22 -4
  49. package/src/server/utils/gitlab-url.ts +3 -0
  50. package/src/server/utils/redact.ts +68 -5
  51. package/src/server/utils/url-security.ts +95 -0
  52. package/dist/client/900.ee2fa037aff389e2.js +0 -10
  53. package/dist/client-v2/900.bef1c41d267e50c5.js +0 -10
@@ -35,17 +35,121 @@ export const GIT_WRITE_ACTIONS = [
35
35
  'subtreeRun',
36
36
  ] as const;
37
37
 
38
- export function getRepositoryPermissionIds(actions: Model[], names: readonly string[]) {
38
+ export type RepositoryPermissionScope = {
39
+ ids: Array<number | string>;
40
+ unrestricted: boolean;
41
+ supported: boolean;
42
+ };
43
+
44
+ type ScopeResolution = {
45
+ ids: Set<number | string>;
46
+ unrestricted: boolean;
47
+ supported: boolean;
48
+ };
49
+
50
+ function supportedIds(ids: Iterable<number | string>): ScopeResolution {
51
+ return { ids: new Set(ids), unrestricted: false, supported: true };
52
+ }
53
+
54
+ function unsupportedScope(): ScopeResolution {
55
+ return { ids: new Set(), unrestricted: false, supported: false };
56
+ }
57
+
58
+ function combineScopeBranches(operator: '$and' | '$or', branches: unknown[]): ScopeResolution {
59
+ const resolutions = branches.map(resolveRepositoryScope);
60
+ if (resolutions.some((resolution) => !resolution.supported)) return unsupportedScope();
61
+
62
+ if (operator === '$or') {
63
+ if (resolutions.some((resolution) => resolution.unrestricted)) {
64
+ return { ids: new Set(), unrestricted: true, supported: true };
65
+ }
66
+ return supportedIds(resolutions.flatMap((resolution) => [...resolution.ids]));
67
+ }
68
+
69
+ const restricted = resolutions.filter((resolution) => !resolution.unrestricted);
70
+ if (restricted.length === 0) {
71
+ return { ids: new Set(), unrestricted: true, supported: true };
72
+ }
73
+ const [first, ...rest] = restricted;
74
+ const ids = new Set([...first.ids].filter((id) => rest.every((resolution) => resolution.ids.has(id))));
75
+ return supportedIds(ids);
76
+ }
77
+
78
+ function resolveRepositoryScope(scope: unknown): ScopeResolution {
79
+ if (scope == null) {
80
+ return { ids: new Set(), unrestricted: true, supported: true };
81
+ }
82
+ if (typeof scope !== 'object' || Array.isArray(scope)) return unsupportedScope();
83
+
84
+ const condition = scope as Record<string, unknown>;
85
+ const keys = Object.keys(condition);
86
+ if (keys.length === 0) {
87
+ return { ids: new Set(), unrestricted: true, supported: true };
88
+ }
89
+
90
+ const operator = keys.find((key) => key === '$and' || key === '$or');
91
+ if (operator) {
92
+ if (keys.length !== 1 || !Array.isArray(condition[operator])) return unsupportedScope();
93
+ return combineScopeBranches(operator as '$and' | '$or', condition[operator] as unknown[]);
94
+ }
95
+
96
+ if (keys.length !== 1 || !('id' in condition)) return unsupportedScope();
97
+ const id = condition.id;
98
+ if (typeof id === 'number' || typeof id === 'string') {
99
+ return supportedIds([id]);
100
+ }
101
+ if (!id || typeof id !== 'object' || Array.isArray(id)) return unsupportedScope();
102
+
103
+ const idCondition = id as Record<string, unknown>;
104
+ const idKeys = Object.keys(idCondition);
105
+ if (idKeys.length !== 1) return unsupportedScope();
106
+ const equal = idCondition.$eq;
107
+ if (typeof equal === 'number' || typeof equal === 'string') {
108
+ return supportedIds([equal]);
109
+ }
110
+ const included = idCondition.$in;
111
+ if (Array.isArray(included) && included.every((value) => typeof value === 'number' || typeof value === 'string')) {
112
+ return supportedIds(included as Array<number | string>);
113
+ }
114
+ return unsupportedScope();
115
+ }
116
+
117
+ function actionScope(actionData: Record<string, unknown>): unknown {
118
+ const scopeRow = actionData.scope;
119
+ if (!scopeRow || typeof scopeRow !== 'object') return undefined;
120
+ const scopeRecord = scopeRow as Record<string, unknown>;
121
+ return 'scope' in scopeRecord ? scopeRecord.scope : scopeRecord;
122
+ }
123
+
124
+ export function getRepositoryPermissionScope(actions: Model[], names: readonly string[]): RepositoryPermissionScope {
39
125
  const ids = new Set<number | string>();
126
+ let unrestricted = false;
40
127
  for (const action of actions) {
41
128
  const actionData = action.toJSON() as Record<string, unknown>;
42
129
  if (typeof actionData.name !== 'string' || !names.includes(actionData.name)) continue;
43
- const scopeRow = actionData.scope as { scope?: unknown } | undefined;
44
- const scope = scopeRow?.scope || scopeRow;
45
- const scopedIds = (scope as { $and?: Array<{ id?: { $in?: unknown } }> } | undefined)?.$and?.[0]?.id?.$in;
46
- if (Array.isArray(scopedIds)) scopedIds.forEach((id: number | string) => ids.add(id));
130
+ const scope = actionScope(actionData);
131
+ const resolution = resolveRepositoryScope(scope);
132
+ if (!resolution.supported) {
133
+ return { ids: [], unrestricted: false, supported: false };
134
+ }
135
+ if (resolution.unrestricted) {
136
+ unrestricted = true;
137
+ continue;
138
+ }
139
+ resolution.ids.forEach((id) => ids.add(id));
47
140
  }
48
- return [...ids];
141
+ return { ids: [...ids], unrestricted, supported: true };
142
+ }
143
+
144
+ export function getRepositoryPermissionIds(actions: Model[], names: readonly string[]) {
145
+ return getRepositoryPermissionScope(actions, names).ids;
146
+ }
147
+
148
+ export function repositoryPermissionIdsForDisplay(
149
+ scope: RepositoryPermissionScope,
150
+ repositoryIds: Array<number | string>,
151
+ ): Array<number | string> {
152
+ return scope.unrestricted ? repositoryIds : scope.ids;
49
153
  }
50
154
 
51
155
  export async function syncScopedActions(
@@ -53,6 +157,7 @@ export async function syncScopedActions(
53
157
  resource: Model,
54
158
  actionNames: readonly string[],
55
159
  ids: Array<number | string>,
160
+ options: { unrestricted?: boolean } = {},
56
161
  ) {
57
162
  const rolesResourceId = getRecordValue(resource, 'id');
58
163
  const actionsRepo = db.getRepository('rolesResourcesActions');
@@ -61,12 +166,12 @@ export async function syncScopedActions(
61
166
  filter: { rolesResourceId, name: storedActionName },
62
167
  appends: ['scope'],
63
168
  });
64
- if (ids.length === 0) {
65
- if (action) await action.destroy();
66
- continue;
67
- }
68
- const scopeJson = { $and: [{ id: { $in: ids } }] };
69
169
  if (action) {
170
+ if (options.unrestricted) {
171
+ await action.update({ scopeId: null });
172
+ continue;
173
+ }
174
+ const scopeJson = { $and: [{ id: { $in: ids } }] };
70
175
  const actionData = action.toJSON() as Record<string, unknown>;
71
176
  const actionScopeId =
72
177
  getRecordValue(actionData.scope, 'id') || actionData.scopeId || getRecordValue(action, 'scopeId');
@@ -80,6 +185,13 @@ export async function syncScopedActions(
80
185
  await action.update({ scopeId: getRecordValue(newScope, 'id') });
81
186
  }
82
187
  } else {
188
+ if (options.unrestricted) {
189
+ await actionsRepo.create({
190
+ values: { rolesResourceId, name: storedActionName },
191
+ });
192
+ continue;
193
+ }
194
+ const scopeJson = { $and: [{ id: { $in: ids } }] };
83
195
  const newScope = await db.getRepository('rolesResourcesScopes').create({ values: { scope: scopeJson } });
84
196
  await actionsRepo.create({
85
197
  values: { rolesResourceId, name: storedActionName, scopeId: getRecordValue(newScope, 'id') },
@@ -123,12 +235,22 @@ export async function rolePermissions(ctx: any, _next: () => Promise<void>) {
123
235
  })
124
236
  : [];
125
237
 
238
+ const readScope = getRepositoryPermissionScope(actions, [...REPOSITORY_READ_ACTIONS, 'read']);
239
+ const gitWriteScope = getRepositoryPermissionScope(gitManagerActions, GIT_WRITE_ACTIONS);
240
+ const repositoryWriteScope = getRepositoryPermissionScope(actions, ['write']);
241
+ const requiresRepositoryList =
242
+ readScope.unrestricted || gitWriteScope.unrestricted || repositoryWriteScope.unrestricted;
243
+ const repositoryIds = requiresRepositoryList
244
+ ? (await ctx.db.getRepository('gitRepositories').find({ fields: ['id'] })).map((repository: Model) =>
245
+ getRecordValue(repository, 'id'),
246
+ )
247
+ : [];
126
248
  const result = {
127
- read: getRepositoryPermissionIds(actions, [...REPOSITORY_READ_ACTIONS, 'read']),
249
+ read: repositoryPermissionIdsForDisplay(readScope, repositoryIds),
128
250
  write: [
129
251
  ...new Set([
130
- ...getRepositoryPermissionIds(gitManagerActions, GIT_WRITE_ACTIONS),
131
- ...getRepositoryPermissionIds(actions, ['write']),
252
+ ...repositoryPermissionIdsForDisplay(gitWriteScope, repositoryIds),
253
+ ...repositoryPermissionIdsForDisplay(repositoryWriteScope, repositoryIds),
132
254
  ]),
133
255
  ],
134
256
  };
@@ -4,6 +4,7 @@ import * as os from 'os';
4
4
  import * as path from 'path';
5
5
  import type { SimpleGit } from 'simple-git';
6
6
  import { createGit, validateBranch, validateLocalPath, withAuth } from './git-actions';
7
+ import { getEffectiveActionParams } from '../repository-access';
7
8
  import { getRepoAccount } from '../utils/get-repo-account';
8
9
  import { redactError, redactPat } from '../utils/redact';
9
10
 
@@ -43,11 +44,6 @@ interface RunParams {
43
44
  expectedTargetSha?: string;
44
45
  }
45
46
 
46
- function getActionParams(ctx: Context): Record<string, unknown> {
47
- const requestBody = (ctx as Context & { request?: { body?: Record<string, unknown> } }).request?.body || {};
48
- return { ...ctx.action.params, ...ctx.action.params?.values, ...requestBody };
49
- }
50
-
51
47
  function stringValue(value: unknown, fallback = ''): string {
52
48
  return typeof value === 'string' ? value.trim() : fallback;
53
49
  }
@@ -320,7 +316,7 @@ async function loadConfigAndRepository(ctx: Context, configId: number | string)
320
316
  }
321
317
 
322
318
  export async function subtreeOptions(ctx: Context, next: () => Promise<void>) {
323
- const params = getActionParams(ctx);
319
+ const params = getEffectiveActionParams(ctx);
324
320
  const repositoryId = params.repositoryId as number | string;
325
321
  if (!repositoryId) ctx.throw(400, 'repositoryId is required');
326
322
  const remoteName = validateRemoteName(stringValue(params.remoteName, 'origin') || 'origin');
@@ -379,7 +375,7 @@ export async function subtreeOptions(ctx: Context, next: () => Promise<void>) {
379
375
  }
380
376
 
381
377
  export async function subtreePreview(ctx: Context, next: () => Promise<void>) {
382
- const params = getActionParams(ctx);
378
+ const params = getEffectiveActionParams(ctx);
383
379
  const configId = params.configId as number | string;
384
380
  if (!configId) ctx.throw(400, 'configId is required');
385
381
  const { config, repo, account } = await loadConfigAndRepository(ctx, configId);
@@ -410,7 +406,7 @@ export async function subtreePreview(ctx: Context, next: () => Promise<void>) {
410
406
  * This action intentionally does not enqueue work in the automatic-review queue.
411
407
  */
412
408
  export async function subtreeRunOnAppProcess(ctx: Context, next: () => Promise<void>) {
413
- const params = getActionParams(ctx);
409
+ const params = getEffectiveActionParams(ctx);
414
410
  const runParams: RunParams = {
415
411
  configId: params.configId as number | string,
416
412
  policy: validateSubtreePolicy(params.policy),
@@ -5,7 +5,7 @@ import {
5
5
  GIT_READ_ACTIONS,
6
6
  GIT_WRITE_ACTIONS,
7
7
  REPOSITORY_READ_ACTIONS,
8
- getRepositoryPermissionIds,
8
+ getRepositoryPermissionScope,
9
9
  syncScopedActions,
10
10
  } from '../actions/role-permissions';
11
11
 
@@ -36,12 +36,23 @@ export default class MigrateLegacyRepositoryPermissions extends Migration {
36
36
  const legacyWrite = actions.filter((action) => getRecordValue(action, 'name') === 'write') as Model[];
37
37
  if (legacyRead.length === 0 && legacyWrite.length === 0) continue;
38
38
 
39
- const readIds = getRepositoryPermissionIds(legacyRead, ['read']);
40
- const writeIds = getRepositoryPermissionIds(legacyWrite, ['write']);
41
- const effectiveReadIds = [...new Set([...readIds, ...writeIds])];
42
- if (effectiveReadIds.length === 0 && writeIds.length === 0) continue;
39
+ const readPermission = getRepositoryPermissionScope(legacyRead, ['read']);
40
+ const writePermission = getRepositoryPermissionScope(legacyWrite, ['write']);
41
+ if (!readPermission.supported || !writePermission.supported) {
42
+ this.app.log?.warn(
43
+ `[git-manager] skipped repository permission migration for role ${String(
44
+ roleName,
45
+ )} because its legacy scope cannot be represented safely`,
46
+ );
47
+ continue;
48
+ }
49
+ const effectiveReadIds = [...new Set([...readPermission.ids, ...writePermission.ids])];
50
+ const effectiveReadUnrestricted = readPermission.unrestricted || writePermission.unrestricted;
51
+ if (effectiveReadIds.length === 0 && !effectiveReadUnrestricted && !writePermission.unrestricted) continue;
43
52
 
44
- await syncScopedActions(this.db, repositoryResource, REPOSITORY_READ_ACTIONS, effectiveReadIds);
53
+ await syncScopedActions(this.db, repositoryResource, REPOSITORY_READ_ACTIONS, effectiveReadIds, {
54
+ unrestricted: effectiveReadUnrestricted,
55
+ });
45
56
 
46
57
  let gitManagerResource = await resourceRepo.findOne({ filter: { roleName, name: 'gitManager' } });
47
58
  if (!gitManagerResource) {
@@ -52,8 +63,12 @@ export default class MigrateLegacyRepositoryPermissions extends Migration {
52
63
  await gitManagerResource.update({ usingActionsConfig: true });
53
64
  }
54
65
 
55
- await syncScopedActions(this.db, gitManagerResource, GIT_READ_ACTIONS, effectiveReadIds);
56
- await syncScopedActions(this.db, gitManagerResource, GIT_WRITE_ACTIONS, writeIds);
66
+ await syncScopedActions(this.db, gitManagerResource, GIT_READ_ACTIONS, effectiveReadIds, {
67
+ unrestricted: effectiveReadUnrestricted,
68
+ });
69
+ await syncScopedActions(this.db, gitManagerResource, GIT_WRITE_ACTIONS, writePermission.ids, {
70
+ unrestricted: writePermission.unrestricted,
71
+ });
57
72
 
58
73
  for (const action of [...legacyRead, ...legacyWrite]) {
59
74
  await action.destroy();
@@ -11,18 +11,40 @@ import * as subtreeActions from './actions/subtree';
11
11
  import { recoverStuckReviews, registerReviewQueue, unregisterReviewQueue } from './actions/review';
12
12
  import { registerGitReviewAiTools } from './ai-tools';
13
13
  import { startPoller, stopPoller } from './poller';
14
- import { RegistryGitContentService } from './services/registry-content-service';
15
- import { enforceRepositoryAccess } from './repository-access';
14
+ import { RegistryGitContentService, SkillHubGitContentService } from './services/registry-content-service';
15
+ import { enforceRepositoryAccess, enforceRepositoryCollectionAccess } from './repository-access';
16
+ import { isGitManagerResourceResponse, redactCredentialFields } from './utils/redact';
17
+ import { containsCredentialBearingUrlField, URL_USERINFO_NOT_ALLOWED } from './utils/url-security';
18
+
19
+ const GIT_CONFIGURATION_URL_FIELDS = new Set(['repoUrl', 'baseUrl']);
20
+
21
+ export function isGitConfigurationUrlMutation(resourceName: unknown, actionName: unknown): boolean {
22
+ return (
23
+ typeof actionName === 'string' &&
24
+ ['create', 'update'].includes(actionName) &&
25
+ isGitManagerResourceResponse(resourceName)
26
+ );
27
+ }
16
28
 
17
29
  export class PluginGitManagerServer extends Plugin {
18
30
  // @ts-ignore
19
31
  declare app: any;
20
32
  // @ts-ignore
21
33
  declare db: any;
22
- registryContentService: RegistryGitContentService;
34
+ registryContentService!: RegistryGitContentService;
35
+ skillHubContentService!: SkillHubGitContentService;
23
36
 
24
37
  async afterAdd() {
25
- this.registryContentService = new RegistryGitContentService((this as unknown as { db: Database }).db);
38
+ this.registryContentService = new RegistryGitContentService(
39
+ (this as unknown as { db: Database }).db,
40
+ undefined,
41
+ this.app.acl,
42
+ );
43
+ this.skillHubContentService = new SkillHubGitContentService(
44
+ (this as unknown as { db: Database }).db,
45
+ undefined,
46
+ this.app.acl,
47
+ );
26
48
  }
27
49
 
28
50
  async beforeLoad() {
@@ -78,6 +100,7 @@ export class PluginGitManagerServer extends Plugin {
78
100
  });
79
101
 
80
102
  (this as any).app.resourceManager.use(enforceRepositoryAccess);
103
+ (this as any).app.resourceManager.use(enforceRepositoryCollectionAccess);
81
104
 
82
105
  // Suppress noisy workflow pre-action/post-action warnings for custom resources
83
106
  (this as any).app.use(async (ctx, next) => {
@@ -254,10 +277,22 @@ export class PluginGitManagerServer extends Plugin {
254
277
  }
255
278
  }
256
279
 
257
- if (
258
- (resource === 'gitRepositories' || resource === 'gitAccounts') &&
259
- ['create', 'update'].includes(ctx.action?.actionName)
260
- ) {
280
+ if (isGitConfigurationUrlMutation(resource, ctx.action?.actionName)) {
281
+ const actionParams = ctx.action?.params;
282
+ const requestBody = ctx.request?.body;
283
+ const hasCredentialBearingUrl = [
284
+ actionParams,
285
+ actionParams?.values,
286
+ requestBody,
287
+ requestBody?.values,
288
+ ctx.query,
289
+ ctx.request?.query,
290
+ ].some((source) => containsCredentialBearingUrlField(source, GIT_CONFIGURATION_URL_FIELDS));
291
+
292
+ if (hasCredentialBearingUrl) {
293
+ ctx.throw(400, ctx.t(URL_USERINFO_NOT_ALLOWED, { ns: (this as any).name }));
294
+ }
295
+
261
296
  if (ctx.action.params?.values?.pat === '••••••••') {
262
297
  delete ctx.action.params.values.pat;
263
298
  }
@@ -268,28 +303,14 @@ export class PluginGitManagerServer extends Plugin {
268
303
  return next();
269
304
  });
270
305
 
271
- // Strip PAT from API responses — scoped to gitRepositories and gitAccounts
306
+ // Strip PAT from every Git Manager response, including nested association routes.
272
307
  (this as any).app.resourceManager.use(async (ctx, next) => {
273
308
  const resource = ctx.action?.resourceName;
274
- if (resource !== 'gitRepositories' && resource !== 'gitAccounts') {
309
+ if (!isGitManagerResourceResponse(resource)) {
275
310
  return next();
276
311
  }
277
312
  await next();
278
- if (ctx.body) {
279
- const items = Array.isArray(ctx.body)
280
- ? ctx.body
281
- : ctx.body?.data
282
- ? Array.isArray(ctx.body.data)
283
- ? ctx.body.data
284
- : [ctx.body.data]
285
- : [ctx.body];
286
- items.forEach((item) => {
287
- if (item && typeof item === 'object') {
288
- if (item.pat) item.pat = '••••••••';
289
- if (item.dataValues?.pat) item.dataValues.pat = '••••••••';
290
- }
291
- });
292
- }
313
+ redactCredentialFields(ctx.body);
293
314
  });
294
315
  }
295
316