plugin-git-manager 1.2.21 → 1.2.24

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 (50) hide show
  1. package/dist/client/{434.1d19d4d6acc114f1.js → 434.b2eeaaef33aae6e0.js} +1 -1
  2. package/dist/client/index.js +1 -1
  3. package/dist/client-v2/{434.f8836ea116648de1.js → 434.7a27051db18526c0.js} +1 -1
  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 +217 -54
  14. package/dist/server/actions/subtree.js +4 -8
  15. package/dist/server/migrations/20260730000000-migrate-legacy-repository-permissions.js +94 -0
  16. package/dist/server/plugin.js +44 -19
  17. package/dist/server/repository-access.js +570 -0
  18. package/dist/server/services/registry-content-service.js +137 -22
  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 +1 -1
  24. package/src/client/components/RepositoryConfig.tsx +4 -1
  25. package/src/locale/en-US.json +4 -3
  26. package/src/locale/vi-VN.json +4 -3
  27. package/src/locale/zh-CN.json +4 -3
  28. package/src/server/__tests__/git-actions.test.ts +24 -0
  29. package/src/server/__tests__/plugin-url-security.test.ts +17 -0
  30. package/src/server/__tests__/poller.test.ts +37 -0
  31. package/src/server/__tests__/repository-access.test.ts +752 -0
  32. package/src/server/__tests__/review.test.ts +137 -0
  33. package/src/server/__tests__/role-permissions.test.ts +147 -0
  34. package/src/server/actions/git-actions.ts +33 -10
  35. package/src/server/actions/gitlab-api.ts +5 -8
  36. package/src/server/actions/poller.ts +2 -1
  37. package/src/server/actions/review.ts +74 -15
  38. package/src/server/actions/role-permissions.ts +251 -59
  39. package/src/server/actions/subtree.ts +4 -8
  40. package/src/server/migrations/20260730000000-migrate-legacy-repository-permissions.ts +81 -0
  41. package/src/server/plugin.ts +48 -24
  42. package/src/server/repository-access.ts +773 -0
  43. package/src/server/services/__tests__/registry-content-service.test.ts +209 -13
  44. package/src/server/services/registry-content-service.ts +184 -30
  45. package/src/server/utils/__tests__/redact.test.ts +56 -0
  46. package/src/server/utils/__tests__/url-security.test.ts +85 -0
  47. package/src/server/utils/get-repo-account.ts +22 -4
  48. package/src/server/utils/gitlab-url.ts +3 -0
  49. package/src/server/utils/redact.ts +68 -5
  50. package/src/server/utils/url-security.ts +95 -0
@@ -38,7 +38,8 @@ var plugin_exports = {};
38
38
  __export(plugin_exports, {
39
39
  PluginGitManagerServer: () => PluginGitManagerServer,
40
40
  default: () => plugin_default,
41
- ensureAutoReviewFlowSchema: () => ensureAutoReviewFlowSchema
41
+ ensureAutoReviewFlowSchema: () => ensureAutoReviewFlowSchema,
42
+ isGitConfigurationUrlMutation: () => isGitConfigurationUrlMutation
42
43
  });
43
44
  module.exports = __toCommonJS(plugin_exports);
44
45
  var import_server = require("@nocobase/server");
@@ -54,10 +55,27 @@ var import_review = require("./actions/review");
54
55
  var import_ai_tools = require("./ai-tools");
55
56
  var import_poller = require("./poller");
56
57
  var import_registry_content_service = require("./services/registry-content-service");
58
+ var import_repository_access = require("./repository-access");
59
+ var import_redact = require("./utils/redact");
60
+ var import_url_security = require("./utils/url-security");
61
+ const GIT_CONFIGURATION_URL_FIELDS = /* @__PURE__ */ new Set(["repoUrl", "baseUrl"]);
62
+ function isGitConfigurationUrlMutation(resourceName, actionName) {
63
+ return typeof actionName === "string" && ["create", "update"].includes(actionName) && (0, import_redact.isGitManagerResourceResponse)(resourceName);
64
+ }
57
65
  class PluginGitManagerServer extends import_server.Plugin {
58
66
  registryContentService;
67
+ skillHubContentService;
59
68
  async afterAdd() {
60
- this.registryContentService = new import_registry_content_service.RegistryGitContentService(this.db);
69
+ this.registryContentService = new import_registry_content_service.RegistryGitContentService(
70
+ this.db,
71
+ void 0,
72
+ this.app.acl
73
+ );
74
+ this.skillHubContentService = new import_registry_content_service.SkillHubGitContentService(
75
+ this.db,
76
+ void 0,
77
+ this.app.acl
78
+ );
61
79
  }
62
80
  async beforeLoad() {
63
81
  await this.app.db.import({
@@ -106,6 +124,8 @@ class PluginGitManagerServer extends import_server.Plugin {
106
124
  subtreeReplace: subtreeActions.subtreeRunOnAppProcess
107
125
  }
108
126
  });
127
+ this.app.resourceManager.use(import_repository_access.enforceRepositoryAccess);
128
+ this.app.resourceManager.use(import_repository_access.enforceRepositoryCollectionAccess);
109
129
  this.app.use(async (ctx, next) => {
110
130
  if (ctx.logger && ctx.logger.warn) {
111
131
  const originalWarn = ctx.logger.warn.bind(ctx.logger);
@@ -227,7 +247,7 @@ class PluginGitManagerServer extends import_server.Plugin {
227
247
  ]
228
248
  });
229
249
  this.app.resourceManager.use(async (ctx, next) => {
230
- var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j;
250
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m;
231
251
  const resource = (_a = ctx.action) == null ? void 0 : _a.resourceName;
232
252
  if (resource === "gitSubtreeConfigs" && ["create", "update"].includes((_b = ctx.action) == null ? void 0 : _b.actionName) && ((_c = ctx.action.params) == null ? void 0 : _c.values)) {
233
253
  try {
@@ -262,33 +282,37 @@ class PluginGitManagerServer extends import_server.Plugin {
262
282
  }
263
283
  }
264
284
  }
265
- if ((resource === "gitRepositories" || resource === "gitAccounts") && ["create", "update"].includes((_g = ctx.action) == null ? void 0 : _g.actionName)) {
266
- if (((_i = (_h = ctx.action.params) == null ? void 0 : _h.values) == null ? void 0 : _i.pat) === "\u2022\u2022\u2022\u2022\u2022\u2022\u2022\u2022") {
285
+ if (isGitConfigurationUrlMutation(resource, (_g = ctx.action) == null ? void 0 : _g.actionName)) {
286
+ const actionParams = (_h = ctx.action) == null ? void 0 : _h.params;
287
+ const requestBody = (_i = ctx.request) == null ? void 0 : _i.body;
288
+ const hasCredentialBearingUrl = [
289
+ actionParams,
290
+ actionParams == null ? void 0 : actionParams.values,
291
+ requestBody,
292
+ requestBody == null ? void 0 : requestBody.values,
293
+ ctx.query,
294
+ (_j = ctx.request) == null ? void 0 : _j.query
295
+ ].some((source) => (0, import_url_security.containsCredentialBearingUrlField)(source, GIT_CONFIGURATION_URL_FIELDS));
296
+ if (hasCredentialBearingUrl) {
297
+ ctx.throw(400, ctx.t(import_url_security.URL_USERINFO_NOT_ALLOWED, { ns: this.name }));
298
+ }
299
+ if (((_l = (_k = ctx.action.params) == null ? void 0 : _k.values) == null ? void 0 : _l.pat) === "\u2022\u2022\u2022\u2022\u2022\u2022\u2022\u2022") {
267
300
  delete ctx.action.params.values.pat;
268
301
  }
269
- if (((_j = ctx.request.body) == null ? void 0 : _j.pat) === "\u2022\u2022\u2022\u2022\u2022\u2022\u2022\u2022") {
302
+ if (((_m = ctx.request.body) == null ? void 0 : _m.pat) === "\u2022\u2022\u2022\u2022\u2022\u2022\u2022\u2022") {
270
303
  delete ctx.request.body.pat;
271
304
  }
272
305
  }
273
306
  return next();
274
307
  });
275
308
  this.app.resourceManager.use(async (ctx, next) => {
276
- var _a, _b;
309
+ var _a;
277
310
  const resource = (_a = ctx.action) == null ? void 0 : _a.resourceName;
278
- if (resource !== "gitRepositories" && resource !== "gitAccounts") {
311
+ if (!(0, import_redact.isGitManagerResourceResponse)(resource)) {
279
312
  return next();
280
313
  }
281
314
  await next();
282
- if (ctx.body) {
283
- const items = Array.isArray(ctx.body) ? ctx.body : ((_b = ctx.body) == null ? void 0 : _b.data) ? Array.isArray(ctx.body.data) ? ctx.body.data : [ctx.body.data] : [ctx.body];
284
- items.forEach((item) => {
285
- var _a2;
286
- if (item && typeof item === "object") {
287
- if (item.pat) item.pat = "\u2022\u2022\u2022\u2022\u2022\u2022\u2022\u2022";
288
- if ((_a2 = item.dataValues) == null ? void 0 : _a2.pat) item.dataValues.pat = "\u2022\u2022\u2022\u2022\u2022\u2022\u2022\u2022";
289
- }
290
- });
291
- }
315
+ (0, import_redact.redactCredentialFields)(ctx.body);
292
316
  });
293
317
  }
294
318
  async install() {
@@ -334,5 +358,6 @@ var plugin_default = PluginGitManagerServer;
334
358
  // Annotate the CommonJS export names for ESM import in node:
335
359
  0 && (module.exports = {
336
360
  PluginGitManagerServer,
337
- ensureAutoReviewFlowSchema
361
+ ensureAutoReviewFlowSchema,
362
+ isGitConfigurationUrlMutation
338
363
  });
@@ -0,0 +1,570 @@
1
+ /**
2
+ * This file is part of the NocoBase (R) project.
3
+ * Copyright (c) 2020-2024 NocoBase Co., Ltd.
4
+ * Authors: NocoBase Team.
5
+ *
6
+ * This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
7
+ * For more information, please refer to: https://www.nocobase.com/agreement.
8
+ */
9
+
10
+ var __defProp = Object.defineProperty;
11
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
12
+ var __getOwnPropNames = Object.getOwnPropertyNames;
13
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
14
+ var __export = (target, all) => {
15
+ for (var name in all)
16
+ __defProp(target, name, { get: all[name], enumerable: true });
17
+ };
18
+ var __copyProps = (to, from, except, desc) => {
19
+ if (from && typeof from === "object" || typeof from === "function") {
20
+ for (let key of __getOwnPropNames(from))
21
+ if (!__hasOwnProp.call(to, key) && key !== except)
22
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
23
+ }
24
+ return to;
25
+ };
26
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
27
+ var repository_access_exports = {};
28
+ __export(repository_access_exports, {
29
+ enforceRepositoryAccess: () => enforceRepositoryAccess,
30
+ enforceRepositoryCollectionAccess: () => enforceRepositoryCollectionAccess,
31
+ getEffectiveActionParams: () => getEffectiveActionParams,
32
+ hasRepositoryAccess: () => hasRepositoryAccess,
33
+ resolveRepositoryScope: () => resolveRepositoryScope,
34
+ scopeIncludesRepository: () => scopeIncludesRepository
35
+ });
36
+ module.exports = __toCommonJS(repository_access_exports);
37
+ const REPOSITORY_ID_ACTIONS = /* @__PURE__ */ new Set([
38
+ "clone",
39
+ "pull",
40
+ "push",
41
+ "fetch",
42
+ "diff",
43
+ "status",
44
+ "log",
45
+ "branches",
46
+ "checkout",
47
+ "fileTree",
48
+ "fileContent",
49
+ "commitDetail",
50
+ "mergeRequests",
51
+ "mergeRequestDetail",
52
+ "mergeRequestNotes",
53
+ "triggerReview",
54
+ "subtreeOptions"
55
+ ]);
56
+ const SUBTREE_CONFIG_ACTIONS = /* @__PURE__ */ new Set(["subtreePreview", "subtreeRun", "subtreeReplace"]);
57
+ const REVIEW_ID_ACTIONS = /* @__PURE__ */ new Set(["reviewApprovePost", "reviewReject"]);
58
+ function asRecord(value) {
59
+ return value && typeof value === "object" && !Array.isArray(value) ? value : {};
60
+ }
61
+ function asRepositoryId(value) {
62
+ if (typeof value === "number" && Number.isFinite(value)) return value;
63
+ if (typeof value === "string" && value.trim()) return value;
64
+ return void 0;
65
+ }
66
+ function getRecordValue(record, attribute) {
67
+ if (!record || typeof record !== "object") return void 0;
68
+ const model = record;
69
+ return typeof model.get === "function" ? model.get(attribute) : model[attribute];
70
+ }
71
+ function getEffectiveActionParams(ctx) {
72
+ var _a, _b;
73
+ const params = asRecord((_a = ctx.action) == null ? void 0 : _a.params);
74
+ return {
75
+ ...params,
76
+ ...asRecord(params.values),
77
+ ...asRecord((_b = ctx.request) == null ? void 0 : _b.body)
78
+ };
79
+ }
80
+ function supportedRepositoryIds(ids) {
81
+ const uniqueIds = [];
82
+ for (const id of ids) {
83
+ if (!uniqueIds.some((candidate) => String(candidate) === String(id))) {
84
+ uniqueIds.push(id);
85
+ }
86
+ }
87
+ return { ids: uniqueIds, unrestricted: false, supported: true };
88
+ }
89
+ function unsupportedRepositoryScope() {
90
+ return { ids: [], unrestricted: false, supported: false };
91
+ }
92
+ function resolveRepositoryScopeBranches(operator, branches) {
93
+ const resolutions = branches.map(resolveRepositoryScope);
94
+ if (resolutions.some((resolution) => !resolution.supported)) {
95
+ return unsupportedRepositoryScope();
96
+ }
97
+ if (operator === "$or") {
98
+ if (resolutions.some((resolution) => resolution.unrestricted)) {
99
+ return { ids: [], unrestricted: true, supported: true };
100
+ }
101
+ return supportedRepositoryIds(resolutions.flatMap((resolution) => resolution.ids));
102
+ }
103
+ const restricted = resolutions.filter((resolution) => !resolution.unrestricted);
104
+ if (restricted.length === 0) {
105
+ return { ids: [], unrestricted: true, supported: true };
106
+ }
107
+ const [first, ...rest] = restricted;
108
+ return supportedRepositoryIds(
109
+ first.ids.filter(
110
+ (id) => rest.every((resolution) => resolution.ids.some((candidate) => String(candidate) === String(id)))
111
+ )
112
+ );
113
+ }
114
+ function resolveRepositoryScope(scope) {
115
+ if (scope == null) {
116
+ return { ids: [], unrestricted: true, supported: true };
117
+ }
118
+ if (typeof scope !== "object" || Array.isArray(scope)) {
119
+ return unsupportedRepositoryScope();
120
+ }
121
+ const condition = scope;
122
+ const keys = Object.keys(condition);
123
+ if (keys.length === 0) {
124
+ return { ids: [], unrestricted: true, supported: true };
125
+ }
126
+ const operator = keys.find((key) => key === "$and" || key === "$or");
127
+ if (operator) {
128
+ if (keys.length !== 1 || !Array.isArray(condition[operator])) {
129
+ return unsupportedRepositoryScope();
130
+ }
131
+ return resolveRepositoryScopeBranches(operator, condition[operator]);
132
+ }
133
+ if (keys.length !== 1 || !("id" in condition)) {
134
+ return unsupportedRepositoryScope();
135
+ }
136
+ const id = condition.id;
137
+ if (typeof id === "number" || typeof id === "string") {
138
+ return supportedRepositoryIds([id]);
139
+ }
140
+ if (!id || typeof id !== "object" || Array.isArray(id)) {
141
+ return unsupportedRepositoryScope();
142
+ }
143
+ const idCondition = id;
144
+ const idKeys = Object.keys(idCondition);
145
+ if (idKeys.length !== 1) {
146
+ return unsupportedRepositoryScope();
147
+ }
148
+ const equal = idCondition.$eq;
149
+ if (typeof equal === "number" || typeof equal === "string") {
150
+ return supportedRepositoryIds([equal]);
151
+ }
152
+ const included = idCondition.$in;
153
+ if (Array.isArray(included) && included.every((value) => typeof value === "number" || typeof value === "string")) {
154
+ return supportedRepositoryIds(included);
155
+ }
156
+ return unsupportedRepositoryScope();
157
+ }
158
+ function scopeIncludesRepository(filter, repositoryId) {
159
+ const scope = resolveRepositoryScope(filter);
160
+ return scope.supported && (scope.unrestricted || scope.ids.some((id) => String(id) === String(repositoryId)));
161
+ }
162
+ async function hasRepositoryAccess(input) {
163
+ var _a;
164
+ if (input.roles.includes("root")) {
165
+ return true;
166
+ }
167
+ if (!input.acl) {
168
+ return false;
169
+ }
170
+ const permission = await input.acl.can({
171
+ roles: input.roles,
172
+ resource: "gitManager",
173
+ action: input.action || "fileContent"
174
+ });
175
+ return Boolean(permission && scopeIncludesRepository((_a = permission.params) == null ? void 0 : _a.filter, input.repositoryId));
176
+ }
177
+ async function hasUnrestrictedRepositoryAccess(input) {
178
+ var _a;
179
+ if (input.roles.includes("root")) return true;
180
+ if (!input.acl) return false;
181
+ const permission = await input.acl.can({
182
+ roles: input.roles,
183
+ resource: "gitManager",
184
+ action: input.action
185
+ });
186
+ const scope = resolveRepositoryScope((_a = permission == null ? void 0 : permission.params) == null ? void 0 : _a.filter);
187
+ return Boolean(permission && scope.supported && scope.unrestricted);
188
+ }
189
+ const COLLECTION_SCOPE_FIELDS = {
190
+ gitRepositories: "id",
191
+ gitReviewFlows: "repositoryId",
192
+ gitCodeReviews: "repositoryId",
193
+ gitSubtreeConfigs: "repositoryId",
194
+ gitSubtreeRuns: "config.repositoryId"
195
+ };
196
+ const STANDARD_COLLECTION_ACTIONS = /* @__PURE__ */ new Set(["list", "get", "create", "update", "destroy"]);
197
+ function currentRoles(ctx) {
198
+ var _a;
199
+ const rawRoles = (_a = ctx.state) == null ? void 0 : _a.currentRoles;
200
+ const roles = Array.isArray(rawRoles) ? rawRoles.filter((role) => typeof role === "string") : [];
201
+ if (Array.isArray(rawRoles) && roles.length !== rawRoles.length) {
202
+ return ctx.throw(403, "Permission denied");
203
+ }
204
+ return roles;
205
+ }
206
+ function scopedCollectionName(ctx) {
207
+ var _a, _b, _c, _d, _e;
208
+ let targetCollectionName;
209
+ try {
210
+ targetCollectionName = (_c = (_b = (_a = ctx.getCurrentRepository) == null ? void 0 : _a.call(ctx)) == null ? void 0 : _b.targetCollection) == null ? void 0 : _c.name;
211
+ } catch {
212
+ }
213
+ if (targetCollectionName && Object.prototype.hasOwnProperty.call(COLLECTION_SCOPE_FIELDS, targetCollectionName)) {
214
+ return targetCollectionName;
215
+ }
216
+ if (targetCollectionName === "gitAccounts") {
217
+ return targetCollectionName;
218
+ }
219
+ const params = asRecord((_d = ctx.action) == null ? void 0 : _d.params);
220
+ const candidates = [(_e = ctx.action) == null ? void 0 : _e.resourceName, params.resourceName, params.associatedName, params.targetCollection].filter((value) => typeof value === "string").flatMap((value) => value.split("."));
221
+ return candidates.find(
222
+ (candidate) => candidate === "gitAccounts" || Object.prototype.hasOwnProperty.call(COLLECTION_SCOPE_FIELDS, candidate)
223
+ ) || null;
224
+ }
225
+ function collectionFilter(collectionName, ids) {
226
+ if (collectionName === "gitSubtreeRuns") {
227
+ return { config: { repositoryId: { $in: ids } } };
228
+ }
229
+ return { [COLLECTION_SCOPE_FIELDS[collectionName]]: { $in: ids } };
230
+ }
231
+ function effectiveMutationValues(ctx) {
232
+ var _a;
233
+ const body = asRecord((_a = ctx.request) == null ? void 0 : _a.body);
234
+ return { ...asRecord(body.values), ...getEffectiveActionParams(ctx) };
235
+ }
236
+ function rejectTargetCollectionOverride(ctx, collectionName) {
237
+ var _a, _b;
238
+ const actionParams = asRecord((_a = ctx.action) == null ? void 0 : _a.params);
239
+ const requestBody = asRecord((_b = ctx.request) == null ? void 0 : _b.body);
240
+ const actionValues = asRecord(actionParams.values);
241
+ const bodyValues = asRecord(requestBody.values);
242
+ const requestedCollections = [
243
+ actionParams.targetCollection,
244
+ actionParams.__collection,
245
+ actionValues.targetCollection,
246
+ actionValues.__collection,
247
+ requestBody.targetCollection,
248
+ requestBody.__collection,
249
+ bodyValues.targetCollection,
250
+ bodyValues.__collection
251
+ ];
252
+ for (const requestedCollection of requestedCollections) {
253
+ if (requestedCollection == null || requestedCollection === collectionName) continue;
254
+ ctx.throw(403, "Permission denied");
255
+ }
256
+ }
257
+ function rejectScopedRepositoryConnectionMutation(ctx) {
258
+ var _a, _b, _c;
259
+ const valueSources = [(_a = ctx.action) == null ? void 0 : _a.params, ctx.query, (_b = ctx.request) == null ? void 0 : _b.query, (_c = ctx.request) == null ? void 0 : _c.body];
260
+ if (valueSources.some((values) => containsMutationField(values, REPOSITORY_CONNECTION_FIELD_SEGMENTS))) {
261
+ ctx.throw(403, "Permission denied");
262
+ }
263
+ }
264
+ function rejectScopedAssociationMutation(ctx, associationSegments) {
265
+ var _a, _b, _c;
266
+ const valueSources = [(_a = ctx.action) == null ? void 0 : _a.params, ctx.query, (_b = ctx.request) == null ? void 0 : _b.query, (_c = ctx.request) == null ? void 0 : _c.body];
267
+ if (valueSources.some((values) => containsMutationField(values, associationSegments))) {
268
+ ctx.throw(403, "Permission denied");
269
+ }
270
+ }
271
+ function rejectScopedBulkMutationValues(ctx, actionName) {
272
+ var _a, _b;
273
+ if (actionName !== "create" && actionName !== "update") {
274
+ return;
275
+ }
276
+ const actionParams = asRecord((_a = ctx.action) == null ? void 0 : _a.params);
277
+ const requestBody = (_b = ctx.request) == null ? void 0 : _b.body;
278
+ const bodyValues = asRecord(requestBody).values;
279
+ if (Array.isArray(actionParams.values) || Array.isArray(requestBody) || Array.isArray(bodyValues)) {
280
+ ctx.throw(403, "Permission denied");
281
+ }
282
+ }
283
+ const ASSOCIATION_READ_PARAM_KEYS = ["appends", "include", "fields"];
284
+ const CREDENTIAL_ASSOCIATION_SEGMENTS = /* @__PURE__ */ new Set(["gitAccount", "gitAccounts"]);
285
+ const REPOSITORY_CONNECTION_FIELD_SEGMENTS = /* @__PURE__ */ new Set(["gitAccount", "gitAccountId", "repoUrl", "localPath"]);
286
+ const REPOSITORY_ASSOCIATION_SEGMENTS = /* @__PURE__ */ new Set(["repository"]);
287
+ const SUBTREE_RUN_ASSOCIATION_SEGMENTS = /* @__PURE__ */ new Set(["config"]);
288
+ function pathSegments(value) {
289
+ return value.split(/[.[\],:/()=\s'"]+/);
290
+ }
291
+ function containsCredentialAssociationSegment(value) {
292
+ return pathSegments(value).some((segment) => CREDENTIAL_ASSOCIATION_SEGMENTS.has(segment));
293
+ }
294
+ function containsMutationField(value, fieldSegments, visited = /* @__PURE__ */ new WeakSet()) {
295
+ if (!value || typeof value !== "object") {
296
+ return false;
297
+ }
298
+ if (visited.has(value)) {
299
+ return false;
300
+ }
301
+ visited.add(value);
302
+ if (Array.isArray(value)) {
303
+ return value.some((item) => containsMutationField(item, fieldSegments, visited));
304
+ }
305
+ return Object.entries(value).some(
306
+ ([key, item]) => pathSegments(key).some((segment) => fieldSegments.has(segment)) || containsMutationField(item, fieldSegments, visited)
307
+ );
308
+ }
309
+ function containsCredentialAssociation(value, visited = /* @__PURE__ */ new WeakSet()) {
310
+ if (typeof value === "string") {
311
+ return containsCredentialAssociationSegment(value);
312
+ }
313
+ if (!value || typeof value !== "object") {
314
+ return false;
315
+ }
316
+ if (visited.has(value)) {
317
+ return false;
318
+ }
319
+ visited.add(value);
320
+ if (Array.isArray(value)) {
321
+ return value.some((item) => containsCredentialAssociation(item, visited));
322
+ }
323
+ return Object.entries(value).some(
324
+ ([key, item]) => containsCredentialAssociationSegment(key) || containsCredentialAssociation(item, visited)
325
+ );
326
+ }
327
+ function isAssociationReadParamKey(key) {
328
+ return ASSOCIATION_READ_PARAM_KEYS.some(
329
+ (paramKey) => key === paramKey || key.startsWith(`${paramKey}[`) || key.startsWith(`${paramKey}.`)
330
+ );
331
+ }
332
+ function rejectScopedCredentialAssociationRoute(ctx) {
333
+ var _a, _b, _c, _d, _e, _f;
334
+ const actionParams = asRecord((_a = ctx.action) == null ? void 0 : _a.params);
335
+ const requestBody = asRecord((_b = ctx.request) == null ? void 0 : _b.body);
336
+ const routeValues = [
337
+ (_c = ctx.action) == null ? void 0 : _c.resourceName,
338
+ actionParams.resourceName,
339
+ actionParams.associatedName,
340
+ actionParams.targetCollection,
341
+ requestBody.resourceName,
342
+ requestBody.associatedName,
343
+ requestBody.targetCollection,
344
+ asRecord(actionParams.values).resourceName,
345
+ asRecord(actionParams.values).associatedName,
346
+ asRecord(actionParams.values).targetCollection,
347
+ asRecord(requestBody.values).resourceName,
348
+ asRecord(requestBody.values).associatedName,
349
+ asRecord(requestBody.values).targetCollection,
350
+ asRecord(ctx.query).resourceName,
351
+ asRecord(ctx.query).associatedName,
352
+ asRecord(ctx.query).targetCollection,
353
+ asRecord((_d = ctx.request) == null ? void 0 : _d.query).resourceName,
354
+ asRecord((_e = ctx.request) == null ? void 0 : _e.query).associatedName,
355
+ asRecord((_f = ctx.request) == null ? void 0 : _f.query).targetCollection
356
+ ];
357
+ if (routeValues.some((value) => typeof value === "string" && containsCredentialAssociationSegment(value))) {
358
+ ctx.throw(403, "Permission denied");
359
+ }
360
+ }
361
+ function rejectScopedRepositoryCredentialAssociationRead(ctx, actionName) {
362
+ var _a, _b, _c;
363
+ if (actionName !== "list" && actionName !== "get") {
364
+ return;
365
+ }
366
+ const actionParams = asRecord((_a = ctx.action) == null ? void 0 : _a.params);
367
+ const requestBody = asRecord((_b = ctx.request) == null ? void 0 : _b.body);
368
+ const valueSources = [
369
+ actionParams,
370
+ asRecord(actionParams.values),
371
+ asRecord(ctx.query),
372
+ asRecord((_c = ctx.request) == null ? void 0 : _c.query),
373
+ requestBody,
374
+ asRecord(requestBody.values)
375
+ ];
376
+ for (const values of valueSources) {
377
+ for (const [key, value] of Object.entries(values)) {
378
+ if (isAssociationReadParamKey(key) && containsCredentialAssociation(value)) {
379
+ ctx.throw(403, "Permission denied");
380
+ }
381
+ }
382
+ }
383
+ }
384
+ function scopeAllowsRepository(scope, repositoryId) {
385
+ return scope.unrestricted || scope.ids.some((id) => String(id) === String(repositoryId));
386
+ }
387
+ function hasOwn(object, key) {
388
+ return Object.prototype.hasOwnProperty.call(object, key);
389
+ }
390
+ function requireScopedRepositoryValue(ctx, values, scope, actionName) {
391
+ if (hasOwn(values, "repository")) {
392
+ ctx.throw(403, "Permission denied");
393
+ }
394
+ if (!hasOwn(values, "repositoryId")) {
395
+ if (actionName === "create") {
396
+ ctx.throw(400, "repositoryId is required");
397
+ }
398
+ return;
399
+ }
400
+ const repositoryId = asRepositoryId(values.repositoryId);
401
+ if (repositoryId == null || !scopeAllowsRepository(scope, repositoryId)) {
402
+ ctx.throw(403, "Permission denied");
403
+ }
404
+ }
405
+ async function requireScopedRunConfigValue(ctx, values, scope, actionName) {
406
+ if (hasOwn(values, "config")) {
407
+ ctx.throw(403, "Permission denied");
408
+ }
409
+ if (!hasOwn(values, "configId")) {
410
+ if (actionName === "create") {
411
+ ctx.throw(400, "configId is required");
412
+ }
413
+ return;
414
+ }
415
+ const configId = asRepositoryId(values.configId);
416
+ const database = ctx.db;
417
+ if (configId == null || !database) {
418
+ ctx.throw(403, "Permission denied");
419
+ }
420
+ const config = await database.getRepository("gitSubtreeConfigs").findOne({ filterByTk: configId });
421
+ if (!config) {
422
+ ctx.throw(404, "Subtree configuration not found");
423
+ }
424
+ const repositoryId = asRepositoryId(getRecordValue(config, "repositoryId"));
425
+ if (repositoryId == null || !scopeAllowsRepository(scope, repositoryId)) {
426
+ ctx.throw(403, "Permission denied");
427
+ }
428
+ }
429
+ async function enforceRepositoryCollectionAccess(ctx, next) {
430
+ var _a, _b, _c;
431
+ const collectionName = scopedCollectionName(ctx);
432
+ if (!collectionName) {
433
+ await next();
434
+ return;
435
+ }
436
+ rejectTargetCollectionOverride(ctx, collectionName);
437
+ const roles = currentRoles(ctx);
438
+ if (roles.includes("root")) {
439
+ await next();
440
+ return;
441
+ }
442
+ const repositoryPermission = await ctx.app.acl.can({
443
+ roles,
444
+ resource: "gitRepositories",
445
+ action: "list"
446
+ });
447
+ if (!repositoryPermission) {
448
+ await next();
449
+ return;
450
+ }
451
+ const scope = resolveRepositoryScope((_a = repositoryPermission.params) == null ? void 0 : _a.filter);
452
+ if (!scope.supported) {
453
+ ctx.throw(403, "Permission denied");
454
+ }
455
+ if (scope.unrestricted) {
456
+ await next();
457
+ return;
458
+ }
459
+ const actionName = (_b = ctx.action) == null ? void 0 : _b.actionName;
460
+ if (!actionName || !STANDARD_COLLECTION_ACTIONS.has(actionName)) {
461
+ ctx.throw(403, "Permission denied");
462
+ }
463
+ if (collectionName === "gitAccounts") {
464
+ ctx.throw(403, "Permission denied");
465
+ }
466
+ if (collectionName === "gitCodeReviews" && (actionName === "create" || actionName === "update")) {
467
+ ctx.throw(403, "Permission denied");
468
+ }
469
+ rejectScopedCredentialAssociationRoute(ctx);
470
+ rejectScopedRepositoryCredentialAssociationRead(ctx, actionName);
471
+ rejectScopedBulkMutationValues(ctx, actionName);
472
+ const mergeParams = (_c = ctx.action) == null ? void 0 : _c.mergeParams;
473
+ if (!mergeParams) {
474
+ ctx.throw(403, "Permission denied");
475
+ }
476
+ mergeParams({ filter: collectionFilter(collectionName, scope.ids) });
477
+ if (actionName === "create" && collectionName === "gitRepositories") {
478
+ ctx.throw(403, "Permission denied");
479
+ }
480
+ if (actionName === "create" || actionName === "update") {
481
+ const values = effectiveMutationValues(ctx);
482
+ if (collectionName === "gitSubtreeRuns") {
483
+ rejectScopedAssociationMutation(ctx, SUBTREE_RUN_ASSOCIATION_SEGMENTS);
484
+ await requireScopedRunConfigValue(ctx, values, scope, actionName);
485
+ } else if (collectionName === "gitRepositories") {
486
+ if (actionName === "update") {
487
+ rejectScopedRepositoryConnectionMutation(ctx);
488
+ }
489
+ } else {
490
+ rejectScopedAssociationMutation(ctx, REPOSITORY_ASSOCIATION_SEGMENTS);
491
+ requireScopedRepositoryValue(ctx, values, scope, actionName);
492
+ }
493
+ }
494
+ await next();
495
+ }
496
+ async function repositoryIdFromSubtreeConfig(ctx, configId) {
497
+ if (!ctx.db) ctx.throw(403, "Permission denied");
498
+ const config = await ctx.db.getRepository("gitSubtreeConfigs").findOne({ filterByTk: configId });
499
+ if (!config) ctx.throw(404, "Subtree configuration not found");
500
+ const repositoryId = asRepositoryId(getRecordValue(config, "repositoryId"));
501
+ if (repositoryId == null) ctx.throw(403, "Permission denied");
502
+ return repositoryId;
503
+ }
504
+ async function repositoryIdFromReview(ctx, reviewId) {
505
+ if (!ctx.db) ctx.throw(403, "Permission denied");
506
+ const review = await ctx.db.getRepository("gitCodeReviews").findOne({ filterByTk: reviewId });
507
+ if (!review) ctx.throw(404, "Review not found");
508
+ const repositoryId = asRepositoryId(getRecordValue(review, "repositoryId"));
509
+ if (repositoryId == null) ctx.throw(403, "Permission denied");
510
+ return repositoryId;
511
+ }
512
+ async function enforceRepositoryAccess(ctx, next) {
513
+ var _a;
514
+ const action = ctx.action;
515
+ const actionName = action == null ? void 0 : action.actionName;
516
+ if ((action == null ? void 0 : action.resourceName) !== "gitManager" || !actionName) {
517
+ return next();
518
+ }
519
+ const currentRoles2 = (_a = ctx.state) == null ? void 0 : _a.currentRoles;
520
+ const roles = Array.isArray(currentRoles2) ? currentRoles2.filter((role) => typeof role === "string") : [];
521
+ if (Array.isArray(currentRoles2) && roles.length !== currentRoles2.length) {
522
+ return ctx.throw(403, "Permission denied");
523
+ }
524
+ const params = getEffectiveActionParams(ctx);
525
+ let repositoryId;
526
+ if (REPOSITORY_ID_ACTIONS.has(actionName)) {
527
+ repositoryId = asRepositoryId(params.repositoryId);
528
+ if (repositoryId == null) return ctx.throw(400, "repositoryId is required");
529
+ } else if (SUBTREE_CONFIG_ACTIONS.has(actionName)) {
530
+ const configId = asRepositoryId(params.configId);
531
+ if (configId == null) return ctx.throw(400, "configId is required");
532
+ repositoryId = await repositoryIdFromSubtreeConfig(ctx, configId);
533
+ } else if (REVIEW_ID_ACTIONS.has(actionName)) {
534
+ const reviewId = asRepositoryId(params.reviewId);
535
+ if (reviewId == null) return ctx.throw(400, "reviewId is required");
536
+ repositoryId = await repositoryIdFromReview(ctx, reviewId);
537
+ } else if (actionName === "pollNow") {
538
+ repositoryId = asRepositoryId(params.repositoryId);
539
+ if (repositoryId == null) {
540
+ const allowed2 = await hasUnrestrictedRepositoryAccess({
541
+ acl: ctx.app.acl,
542
+ roles,
543
+ action: actionName
544
+ });
545
+ if (!allowed2) return ctx.throw(403, "Permission denied");
546
+ return next();
547
+ }
548
+ } else {
549
+ return next();
550
+ }
551
+ const allowed = await hasRepositoryAccess({
552
+ acl: ctx.app.acl,
553
+ roles,
554
+ repositoryId,
555
+ action: actionName
556
+ });
557
+ if (!allowed) {
558
+ return ctx.throw(403, "Permission denied");
559
+ }
560
+ return next();
561
+ }
562
+ // Annotate the CommonJS export names for ESM import in node:
563
+ 0 && (module.exports = {
564
+ enforceRepositoryAccess,
565
+ enforceRepositoryCollectionAccess,
566
+ getEffectiveActionParams,
567
+ hasRepositoryAccess,
568
+ resolveRepositoryScope,
569
+ scopeIncludesRepository
570
+ });