plugin-git-manager 1.2.27 → 1.3.1

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 (138) hide show
  1. package/README.md +151 -108
  2. package/client-v2.d.ts +2 -2
  3. package/client-v2.js +1 -1
  4. package/client.d.ts +3 -3
  5. package/client.js +1 -1
  6. package/dist/client/{326.90c276058682b536.js → 313.d38fc1d40c4589b9.js} +1 -1
  7. package/dist/client/941.1793e67472872992.js +10 -0
  8. package/dist/client/index.js +1 -1
  9. package/dist/client-v2/156.445366026c1cc9a8.js +10 -0
  10. package/dist/client-v2/{326.42b7b53651ffab3a.js → 313.3c867179c3a1e817.js} +1 -1
  11. package/dist/client-v2/434.19c7579c06b9077c.js +10 -0
  12. package/dist/client-v2/928.bba701b5a1ece58e.js +10 -0
  13. package/dist/client-v2/941.b0d55429cd053f32.js +10 -0
  14. package/dist/client-v2/index.js +1 -1
  15. package/dist/externalVersion.js +9 -8
  16. package/dist/locale/en-US.json +396 -297
  17. package/dist/locale/vi-VN.json +384 -292
  18. package/dist/locale/zh-CN.json +150 -51
  19. package/dist/node_modules/simple-git/dist/cjs/index.js +25 -25
  20. package/dist/node_modules/simple-git/package.json +1 -1
  21. package/dist/node_modules/zod/index.cjs +1 -1
  22. package/dist/node_modules/zod/package.json +1 -1
  23. package/dist/server/actions/git-actions.js +90 -36
  24. package/dist/server/actions/gitlab-api.js +4 -2
  25. package/dist/server/actions/poller.js +5 -2
  26. package/dist/server/actions/review-cutover.js +137 -0
  27. package/dist/server/actions/review.js +150 -51
  28. package/dist/server/actions/role-permissions.js +1 -0
  29. package/dist/server/ai-tools.js +8 -9
  30. package/dist/server/collections/gitCodeReviews.js +10 -0
  31. package/dist/server/collections/gitReviewCutover.js +44 -0
  32. package/dist/server/collections/gitReviewFlows.js +3 -0
  33. package/dist/server/collections/gitReviewInbox.js +51 -0
  34. package/dist/server/collections/gitReviewProjects.js +43 -0
  35. package/dist/server/collections/gitReviewRequests.js +56 -0
  36. package/dist/server/index.js +8 -2
  37. package/dist/server/plugin.js +57 -20
  38. package/dist/server/poller.js +95 -74
  39. package/dist/server/repository-access.js +23 -5
  40. package/dist/server/request-method-policy.js +76 -0
  41. package/dist/server/review-control-access.js +79 -0
  42. package/dist/server/services/legacy-post-service.js +412 -0
  43. package/dist/server/services/registry-content-service.js +4 -3
  44. package/dist/server/services/review-service.js +1451 -0
  45. package/dist/server/services/review-source.js +325 -0
  46. package/dist/server/utils/get-repo-account.js +3 -2
  47. package/dist/server/utils/url-security.js +24 -0
  48. package/package.json +61 -43
  49. package/server.d.ts +2 -2
  50. package/server.js +1 -1
  51. package/src/client/__tests__/CommitHistory.test.tsx +79 -0
  52. package/src/client/__tests__/ReviewHistory.test.tsx +126 -0
  53. package/src/client/__tests__/RunReviewButton.test.tsx +84 -0
  54. package/src/client/__tests__/workflowRoutes.test.ts +65 -0
  55. package/src/client/ai-context.tsx +114 -114
  56. package/src/client/components/AIEmployeeSelect.tsx +63 -63
  57. package/src/client/components/CommitHistory.tsx +429 -374
  58. package/src/client/components/FileExplorer.tsx +386 -386
  59. package/src/client/components/GitAccounts.tsx +179 -179
  60. package/src/client/components/GitManagerSettings.tsx +160 -160
  61. package/src/client/components/GitOperations.tsx +220 -220
  62. package/src/client/components/LLMModelSelect.tsx +63 -63
  63. package/src/client/components/LLMServiceSelect.tsx +58 -58
  64. package/src/client/components/MarkdownView.tsx +80 -80
  65. package/src/client/components/MergeRequests.tsx +790 -790
  66. package/src/client/components/PollingStatus.tsx +225 -225
  67. package/src/client/components/RepositoryConfig.tsx +335 -335
  68. package/src/client/components/RepositoryPermissions.tsx +123 -123
  69. package/src/client/components/ReviewFlows.tsx +352 -303
  70. package/src/client/components/ReviewHistory.tsx +796 -566
  71. package/src/client/components/RunReviewButton.tsx +57 -173
  72. package/src/client/context/GitManagerContext.tsx +92 -92
  73. package/src/client/index.tsx +91 -82
  74. package/src/client/locale.ts +9 -9
  75. package/src/client/promptTemplates.ts +31 -31
  76. package/src/client/workflowRoutes.ts +42 -0
  77. package/src/client-v2/ai-context.tsx +99 -0
  78. package/src/client-v2/components/SubtreeSplits.tsx +547 -547
  79. package/src/client-v2/index.tsx +1 -1
  80. package/src/client-v2/locale.ts +8 -8
  81. package/src/client-v2/plugin.tsx +67 -39
  82. package/src/index.ts +2 -2
  83. package/src/locale/en-US.json +396 -297
  84. package/src/locale/vi-VN.json +384 -292
  85. package/src/locale/zh-CN.json +150 -51
  86. package/src/server/__tests__/git-actions.test.ts +24 -24
  87. package/src/server/__tests__/git-security.test.ts +170 -0
  88. package/src/server/__tests__/plugin-url-security.test.ts +17 -17
  89. package/src/server/__tests__/poller-lifecycle.test.ts +71 -0
  90. package/src/server/__tests__/poller.test.ts +37 -37
  91. package/src/server/__tests__/repository-access.test.ts +760 -752
  92. package/src/server/__tests__/review-service.integration.test.ts +1263 -0
  93. package/src/server/__tests__/review.test.ts +76 -137
  94. package/src/server/__tests__/role-permissions.test.ts +147 -147
  95. package/src/server/__tests__/smoke.test.ts +139 -17
  96. package/src/server/__tests__/subtree.test.ts +85 -85
  97. package/src/server/actions/git-actions.ts +540 -480
  98. package/src/server/actions/gitlab-api.ts +422 -411
  99. package/src/server/actions/poller.ts +39 -27
  100. package/src/server/actions/review-cutover.ts +121 -0
  101. package/src/server/actions/review.ts +1498 -1369
  102. package/src/server/actions/role-permissions.ts +331 -321
  103. package/src/server/actions/subtree.ts +508 -508
  104. package/src/server/ai-tools.ts +334 -319
  105. package/src/server/collections/gitAccounts.ts +56 -56
  106. package/src/server/collections/gitCodeReviews.ts +218 -199
  107. package/src/server/collections/gitRepositories.ts +87 -87
  108. package/src/server/collections/gitReviewCutover.ts +23 -0
  109. package/src/server/collections/gitReviewFlows.ts +120 -108
  110. package/src/server/collections/gitReviewInbox.ts +30 -0
  111. package/src/server/collections/gitReviewProjects.ts +22 -0
  112. package/src/server/collections/gitReviewRequests.ts +35 -0
  113. package/src/server/collections/gitSubtreeConfigs.ts +38 -38
  114. package/src/server/collections/gitSubtreeRuns.ts +30 -30
  115. package/src/server/index.ts +14 -2
  116. package/src/server/migrations/20260508000000-add-auto-review-flow-id.ts +29 -29
  117. package/src/server/migrations/20260712000000-add-git-accounts.ts +198 -198
  118. package/src/server/migrations/20260730000000-migrate-legacy-repository-permissions.ts +81 -81
  119. package/src/server/plugin.ts +409 -361
  120. package/src/server/poller.ts +365 -352
  121. package/src/server/repository-access.ts +803 -773
  122. package/src/server/request-method-policy.ts +56 -0
  123. package/src/server/review-control-access.ts +71 -0
  124. package/src/server/services/__tests__/registry-content-service.test.ts +359 -345
  125. package/src/server/services/__tests__/review-source.test.ts +226 -0
  126. package/src/server/services/legacy-post-service.ts +550 -0
  127. package/src/server/services/registry-content-service.ts +495 -484
  128. package/src/server/services/review-service.ts +1899 -0
  129. package/src/server/services/review-source.ts +359 -0
  130. package/src/server/utils/__tests__/redact.test.ts +56 -56
  131. package/src/server/utils/__tests__/url-security.test.ts +85 -85
  132. package/src/server/utils/get-repo-account.ts +51 -41
  133. package/src/server/utils/gitlab-url.ts +38 -38
  134. package/src/server/utils/redact.ts +87 -87
  135. package/src/server/utils/url-security.ts +128 -95
  136. package/dist/client/900.acf7d1fa22f95e71.js +0 -10
  137. package/dist/client-v2/434.7a27051db18526c0.js +0 -10
  138. package/dist/client-v2/900.f32b9dc85249fc0a.js +0 -10
@@ -1,41 +1,51 @@
1
- import { assertUrlHasNoUserInfo } from './url-security';
2
-
3
- export interface GitAccountCredentials {
4
- pat: string;
5
- username: string;
6
- baseUrl: string | null;
7
- provider: string;
8
- }
9
-
10
- type RecordGetter = {
11
- get(attribute: string): unknown;
12
- };
13
-
14
- type GitAccountDatabase = {
15
- getRepository(name: 'gitAccounts'): {
16
- findOne(input: { filterByTk: number | string }): Promise<RecordGetter | null>;
17
- };
18
- };
19
-
20
- export async function getRepoAccount(
21
- db: GitAccountDatabase,
22
- repo: RecordGetter,
23
- ): Promise<GitAccountCredentials | null> {
24
- const gitAccountId = repo.get('gitAccountId');
25
- if (typeof gitAccountId !== 'number' && typeof gitAccountId !== 'string') return null;
26
-
27
- const account = await db.getRepository('gitAccounts').findOne({
28
- filterByTk: gitAccountId,
29
- });
30
- if (!account) return null;
31
-
32
- const baseUrl = ((account.get('baseUrl') as string) || '').trim() || null;
33
- if (baseUrl) assertUrlHasNoUserInfo(baseUrl);
34
-
35
- return {
36
- pat: ((account.get('pat') as string) || '').trim(),
37
- username: ((account.get('username') as string) || '').trim(),
38
- baseUrl,
39
- provider: ((account.get('provider') as string) || 'gitlab').trim(),
40
- };
41
- }
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
+ import { assertAccountRepositoryUrl } from './url-security';
11
+
12
+ export interface GitAccountCredentials {
13
+ pat: string;
14
+ username: string;
15
+ baseUrl: string | null;
16
+ provider: string;
17
+ }
18
+
19
+ type RecordGetter = {
20
+ get(attribute: string): unknown;
21
+ };
22
+
23
+ type GitAccountDatabase = {
24
+ getRepository(name: 'gitAccounts'): {
25
+ findOne(input: { filterByTk: number | string }): Promise<RecordGetter | null>;
26
+ };
27
+ };
28
+
29
+ export async function getRepoAccount(
30
+ db: GitAccountDatabase,
31
+ repo: RecordGetter,
32
+ ): Promise<GitAccountCredentials | null> {
33
+ const gitAccountId = repo.get('gitAccountId');
34
+ if (typeof gitAccountId !== 'number' && typeof gitAccountId !== 'string') return null;
35
+
36
+ const account = await db.getRepository('gitAccounts').findOne({
37
+ filterByTk: gitAccountId,
38
+ });
39
+ if (!account) return null;
40
+
41
+ const baseUrl = ((account.get('baseUrl') as string) || '').trim() || null;
42
+ const provider = ((account.get('provider') as string) || 'gitlab').trim();
43
+ assertAccountRepositoryUrl(String(repo.get('repoUrl') || ''), baseUrl, provider);
44
+
45
+ return {
46
+ pat: ((account.get('pat') as string) || '').trim(),
47
+ username: ((account.get('username') as string) || '').trim(),
48
+ baseUrl,
49
+ provider,
50
+ };
51
+ }
@@ -1,38 +1,38 @@
1
- import { assertUrlHasNoUserInfo } from './url-security';
2
-
3
- /**
4
- * Shared utility for parsing GitLab repository URLs.
5
- * Extracted to avoid duplicating URL-parsing logic across
6
- * gitlab-api.ts, poller.ts, and review.ts.
7
- */
8
-
9
- export interface GitLabProject {
10
- /** Full API base URL, e.g. https://gitlab.com/api/v4 */
11
- apiBase: string;
12
- /** Raw project path, e.g. group/sub/project */
13
- projectPath: string;
14
- /** URL-encoded project path for use in API endpoints */
15
- encodedProject: string;
16
- }
17
-
18
- /**
19
- * Extract GitLab API base URL and project path from a repository URL.
20
- * Supports: https://gitlab.com/group/project.git
21
- * https://self-hosted.gitlab.com/group/sub/project.git
22
- *
23
- * @throws Error if the URL cannot be parsed or has no project path
24
- */
25
- export function parseGitLabProject(repoUrl: string): GitLabProject {
26
- assertUrlHasNoUserInfo(repoUrl);
27
- const url = new URL(repoUrl);
28
- // Remove .git suffix and leading slash
29
- const projectPath = url.pathname.replace(/\.git$/, '').replace(/^\//, '');
30
- if (!projectPath) {
31
- throw new Error('Cannot extract project path from repository URL');
32
- }
33
- return {
34
- apiBase: `${url.protocol}//${url.host}/api/v4`,
35
- projectPath,
36
- encodedProject: encodeURIComponent(projectPath),
37
- };
38
- }
1
+ import { assertUrlHasNoUserInfo } from './url-security';
2
+
3
+ /**
4
+ * Shared utility for parsing GitLab repository URLs.
5
+ * Extracted to avoid duplicating URL-parsing logic across
6
+ * gitlab-api.ts, poller.ts, and review.ts.
7
+ */
8
+
9
+ export interface GitLabProject {
10
+ /** Full API base URL, e.g. https://gitlab.com/api/v4 */
11
+ apiBase: string;
12
+ /** Raw project path, e.g. group/sub/project */
13
+ projectPath: string;
14
+ /** URL-encoded project path for use in API endpoints */
15
+ encodedProject: string;
16
+ }
17
+
18
+ /**
19
+ * Extract GitLab API base URL and project path from a repository URL.
20
+ * Supports: https://gitlab.com/group/project.git
21
+ * https://self-hosted.gitlab.com/group/sub/project.git
22
+ *
23
+ * @throws Error if the URL cannot be parsed or has no project path
24
+ */
25
+ export function parseGitLabProject(repoUrl: string): GitLabProject {
26
+ assertUrlHasNoUserInfo(repoUrl);
27
+ const url = new URL(repoUrl);
28
+ // Remove .git suffix and leading slash
29
+ const projectPath = url.pathname.replace(/\.git$/, '').replace(/^\//, '');
30
+ if (!projectPath) {
31
+ throw new Error('Cannot extract project path from repository URL');
32
+ }
33
+ return {
34
+ apiBase: `${url.protocol}//${url.host}/api/v4`,
35
+ projectPath,
36
+ encodedProject: encodeURIComponent(projectPath),
37
+ };
38
+ }
@@ -1,87 +1,87 @@
1
- import { redactUrlUserInfo } from './url-security';
2
-
3
- export const REDACTED_PAT = '••••••••';
4
-
5
- const GIT_MANAGER_RESOURCE_SEGMENTS = new Set([
6
- 'gitManager',
7
- 'gitRepositories',
8
- 'gitAccounts',
9
- 'gitReviewFlows',
10
- 'gitCodeReviews',
11
- 'gitSubtreeConfigs',
12
- 'gitSubtreeRuns',
13
- ]);
14
-
15
- /**
16
- * Association routes use names such as `gitAccounts.1.repositories`, not the
17
- * raw collection name. Redact every Git Manager resource response so those
18
- * routes cannot bypass credential scrubbing merely because their name is
19
- * nested.
20
- */
21
- export function isGitManagerResourceResponse(resourceName: unknown): boolean {
22
- return (
23
- typeof resourceName === 'string' &&
24
- resourceName.split(/[.[\],:/()=\s]+/).some((segment) => GIT_MANAGER_RESOURCE_SEGMENTS.has(segment))
25
- );
26
- }
27
-
28
- /**
29
- * Redact embedded credentials from URLs in arbitrary strings.
30
- * Matches `scheme://user:password@host` and replaces with `scheme://***:***@host`.
31
- * Used to scrub error messages before persisting them to the DB or
32
- * returning them to the client, since simple-git often echoes the
33
- * authenticated remote URL in stderr.
34
- */
35
- export function redactPat(s: unknown): string {
36
- if (typeof s !== 'string') return s == null ? '' : String(s);
37
- return redactUrlUserInfo(s);
38
- }
39
-
40
- /**
41
- * Mutate `err.message` (and common fields where simple-git stashes stderr)
42
- * to remove any embedded PAT before the error propagates further.
43
- */
44
- export function redactError<T>(err: T): T {
45
- if (!err || typeof err !== 'object') return err;
46
- const error = err as { message?: unknown; stderr?: unknown; stdout?: unknown };
47
- if (typeof error.message === 'string') error.message = redactPat(error.message);
48
- if (typeof error.stderr === 'string') error.stderr = redactPat(error.stderr);
49
- if (typeof error.stdout === 'string') error.stdout = redactPat(error.stdout);
50
- return err;
51
- }
52
-
53
- /**
54
- * Redact PAT fields anywhere in a serializable API response. Repository
55
- * responses can contain a nested `gitAccount` (and Sequelize's nested
56
- * `dataValues`), so a shallow top-level scrub is not sufficient.
57
- */
58
- export function redactCredentialFields(value: unknown): void {
59
- const visited = new WeakSet<object>();
60
-
61
- const visit = (current: unknown): void => {
62
- if (!current || typeof current !== 'object' || visited.has(current)) {
63
- return;
64
- }
65
- visited.add(current);
66
-
67
- if (Array.isArray(current)) {
68
- current.forEach(visit);
69
- return;
70
- }
71
-
72
- const record = current as Record<string, unknown>;
73
- for (const [key, nestedValue] of Object.entries(record)) {
74
- if (key === 'pat') {
75
- record[key] = REDACTED_PAT;
76
- continue;
77
- }
78
- if ((key === 'repoUrl' || key === 'baseUrl') && typeof nestedValue === 'string') {
79
- record[key] = redactUrlUserInfo(nestedValue);
80
- continue;
81
- }
82
- visit(nestedValue);
83
- }
84
- };
85
-
86
- visit(value);
87
- }
1
+ import { redactUrlUserInfo } from './url-security';
2
+
3
+ export const REDACTED_PAT = '••••••••';
4
+
5
+ const GIT_MANAGER_RESOURCE_SEGMENTS = new Set([
6
+ 'gitManager',
7
+ 'gitRepositories',
8
+ 'gitAccounts',
9
+ 'gitReviewFlows',
10
+ 'gitCodeReviews',
11
+ 'gitSubtreeConfigs',
12
+ 'gitSubtreeRuns',
13
+ ]);
14
+
15
+ /**
16
+ * Association routes use names such as `gitAccounts.1.repositories`, not the
17
+ * raw collection name. Redact every Git Manager resource response so those
18
+ * routes cannot bypass credential scrubbing merely because their name is
19
+ * nested.
20
+ */
21
+ export function isGitManagerResourceResponse(resourceName: unknown): boolean {
22
+ return (
23
+ typeof resourceName === 'string' &&
24
+ resourceName.split(/[.[\],:/()=\s]+/).some((segment) => GIT_MANAGER_RESOURCE_SEGMENTS.has(segment))
25
+ );
26
+ }
27
+
28
+ /**
29
+ * Redact embedded credentials from URLs in arbitrary strings.
30
+ * Matches `scheme://user:password@host` and replaces with `scheme://***:***@host`.
31
+ * Used to scrub error messages before persisting them to the DB or
32
+ * returning them to the client, since simple-git often echoes the
33
+ * authenticated remote URL in stderr.
34
+ */
35
+ export function redactPat(s: unknown): string {
36
+ if (typeof s !== 'string') return s == null ? '' : String(s);
37
+ return redactUrlUserInfo(s);
38
+ }
39
+
40
+ /**
41
+ * Mutate `err.message` (and common fields where simple-git stashes stderr)
42
+ * to remove any embedded PAT before the error propagates further.
43
+ */
44
+ export function redactError<T>(err: T): T {
45
+ if (!err || typeof err !== 'object') return err;
46
+ const error = err as { message?: unknown; stderr?: unknown; stdout?: unknown };
47
+ if (typeof error.message === 'string') error.message = redactPat(error.message);
48
+ if (typeof error.stderr === 'string') error.stderr = redactPat(error.stderr);
49
+ if (typeof error.stdout === 'string') error.stdout = redactPat(error.stdout);
50
+ return err;
51
+ }
52
+
53
+ /**
54
+ * Redact PAT fields anywhere in a serializable API response. Repository
55
+ * responses can contain a nested `gitAccount` (and Sequelize's nested
56
+ * `dataValues`), so a shallow top-level scrub is not sufficient.
57
+ */
58
+ export function redactCredentialFields(value: unknown): void {
59
+ const visited = new WeakSet<object>();
60
+
61
+ const visit = (current: unknown): void => {
62
+ if (!current || typeof current !== 'object' || visited.has(current)) {
63
+ return;
64
+ }
65
+ visited.add(current);
66
+
67
+ if (Array.isArray(current)) {
68
+ current.forEach(visit);
69
+ return;
70
+ }
71
+
72
+ const record = current as Record<string, unknown>;
73
+ for (const [key, nestedValue] of Object.entries(record)) {
74
+ if (key === 'pat') {
75
+ record[key] = REDACTED_PAT;
76
+ continue;
77
+ }
78
+ if ((key === 'repoUrl' || key === 'baseUrl') && typeof nestedValue === 'string') {
79
+ record[key] = redactUrlUserInfo(nestedValue);
80
+ continue;
81
+ }
82
+ visit(nestedValue);
83
+ }
84
+ };
85
+
86
+ visit(value);
87
+ }
@@ -1,95 +1,128 @@
1
- export const URL_USERINFO_NOT_ALLOWED = 'Credential-bearing URLs are not allowed';
2
-
3
- function pathSegments(value: string): string[] {
4
- return value.split(/[.[\],:/()=\s'"]+/);
5
- }
6
-
7
- function parseStructuredValue(value: string): unknown {
8
- const trimmed = value.trim();
9
- if (!trimmed.startsWith('{') && !trimmed.startsWith('[')) {
10
- return undefined;
11
- }
12
-
13
- try {
14
- return JSON.parse(trimmed);
15
- } catch {
16
- return undefined;
17
- }
18
- }
19
-
20
- export function hasUrlUserInfo(value: unknown): boolean {
21
- if (typeof value !== 'string' || !value.trim()) {
22
- return false;
23
- }
24
-
25
- try {
26
- const url = new URL(value.trim());
27
- return Boolean(url.username || url.password);
28
- } catch {
29
- return false;
30
- }
31
- }
32
-
33
- export function assertUrlHasNoUserInfo(value: unknown): void {
34
- if (hasUrlUserInfo(value)) {
35
- throw new Error(URL_USERINFO_NOT_ALLOWED);
36
- }
37
- }
38
-
39
- function containsUrlUserInfo(value: unknown, visited = new WeakSet<object>()): boolean {
40
- if (typeof value === 'string') {
41
- return hasUrlUserInfo(value);
42
- }
43
- if (!value || typeof value !== 'object' || visited.has(value)) {
44
- return false;
45
- }
46
- visited.add(value);
47
-
48
- if (Array.isArray(value)) {
49
- return value.some((item) => containsUrlUserInfo(item, visited));
50
- }
51
-
52
- return Object.values(value as Record<string, unknown>).some((item) => containsUrlUserInfo(item, visited));
53
- }
54
-
55
- /**
56
- * Generic NocoBase actions can place values directly, inside `values`, or in
57
- * dotted/bracket paths that UpdateGuard normalizes later. Find userinfo only
58
- * when it belongs to one of the URL-bearing configuration fields.
59
- */
60
- export function containsCredentialBearingUrlField(
61
- value: unknown,
62
- fieldNames: ReadonlySet<string>,
63
- visited = new WeakSet<object>(),
64
- ): boolean {
65
- if (typeof value === 'string') {
66
- const structuredValue = parseStructuredValue(value);
67
- return structuredValue ? containsCredentialBearingUrlField(structuredValue, fieldNames, visited) : false;
68
- }
69
- if (!value || typeof value !== 'object' || visited.has(value)) {
70
- return false;
71
- }
72
- visited.add(value);
73
-
74
- if (Array.isArray(value)) {
75
- return value.some((item) => containsCredentialBearingUrlField(item, fieldNames, visited));
76
- }
77
-
78
- return Object.entries(value as Record<string, unknown>).some(([key, item]) => {
79
- if (pathSegments(key).some((segment) => fieldNames.has(segment)) && containsUrlUserInfo(item)) {
80
- return true;
81
- }
82
- return containsCredentialBearingUrlField(item, fieldNames, visited);
83
- });
84
- }
85
-
86
- /**
87
- * Preserve the rest of a URL while masking URL userinfo in legacy data or an
88
- * accidentally nested API response.
89
- */
90
- export function redactUrlUserInfo(value: string): string {
91
- // Userinfo may itself contain an unescaped `@`; match through the final
92
- // authority `@` rather than stopping at the first one and leaking part of
93
- // a password in a malformed or legacy URL.
94
- return value.replace(/([a-z][a-z\d+.-]*:\/\/)([^/?#\s]+)@/gi, '$1***:***@');
95
- }
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
+ export const URL_USERINFO_NOT_ALLOWED = 'Credential-bearing URLs are not allowed';
11
+
12
+ export function assertAccountRepositoryUrl(repoUrl: string, baseUrl: string | null, provider: string): void {
13
+ assertUrlHasNoUserInfo(repoUrl);
14
+ const trustedUrl = baseUrl || (provider === 'github' ? 'https://github.com' : 'https://gitlab.com');
15
+ assertUrlHasNoUserInfo(trustedUrl);
16
+ let repository: URL;
17
+ let account: URL;
18
+ try {
19
+ repository = new URL(repoUrl);
20
+ account = new URL(trustedUrl);
21
+ } catch {
22
+ throw new Error('Invalid repository or account URL');
23
+ }
24
+ if (repository.protocol !== 'https:' || account.protocol !== 'https:') {
25
+ throw new Error('Only HTTPS repository and account URLs are allowed');
26
+ }
27
+ if (repository.origin !== account.origin) {
28
+ throw new Error('Repository URL must match the Git account origin');
29
+ }
30
+ // This plugin currently implements the public GitHub API, not GitHub Enterprise endpoints.
31
+ if (provider === 'github' && repository.origin !== 'https://github.com') {
32
+ throw new Error('GitHub accounts currently require github.com');
33
+ }
34
+ }
35
+
36
+ function pathSegments(value: string): string[] {
37
+ return value.split(/[.[\],:/()=\s'"]+/);
38
+ }
39
+
40
+ function parseStructuredValue(value: string): unknown {
41
+ const trimmed = value.trim();
42
+ if (!trimmed.startsWith('{') && !trimmed.startsWith('[')) {
43
+ return undefined;
44
+ }
45
+
46
+ try {
47
+ return JSON.parse(trimmed);
48
+ } catch {
49
+ return undefined;
50
+ }
51
+ }
52
+
53
+ export function hasUrlUserInfo(value: unknown): boolean {
54
+ if (typeof value !== 'string' || !value.trim()) {
55
+ return false;
56
+ }
57
+
58
+ try {
59
+ const url = new URL(value.trim());
60
+ return Boolean(url.username || url.password);
61
+ } catch {
62
+ return false;
63
+ }
64
+ }
65
+
66
+ export function assertUrlHasNoUserInfo(value: unknown): void {
67
+ if (hasUrlUserInfo(value)) {
68
+ throw new Error(URL_USERINFO_NOT_ALLOWED);
69
+ }
70
+ }
71
+
72
+ function containsUrlUserInfo(value: unknown, visited = new WeakSet<object>()): boolean {
73
+ if (typeof value === 'string') {
74
+ return hasUrlUserInfo(value);
75
+ }
76
+ if (!value || typeof value !== 'object' || visited.has(value)) {
77
+ return false;
78
+ }
79
+ visited.add(value);
80
+
81
+ if (Array.isArray(value)) {
82
+ return value.some((item) => containsUrlUserInfo(item, visited));
83
+ }
84
+
85
+ return Object.values(value as Record<string, unknown>).some((item) => containsUrlUserInfo(item, visited));
86
+ }
87
+
88
+ /**
89
+ * Generic NocoBase actions can place values directly, inside `values`, or in
90
+ * dotted/bracket paths that UpdateGuard normalizes later. Find userinfo only
91
+ * when it belongs to one of the URL-bearing configuration fields.
92
+ */
93
+ export function containsCredentialBearingUrlField(
94
+ value: unknown,
95
+ fieldNames: ReadonlySet<string>,
96
+ visited = new WeakSet<object>(),
97
+ ): boolean {
98
+ if (typeof value === 'string') {
99
+ const structuredValue = parseStructuredValue(value);
100
+ return structuredValue ? containsCredentialBearingUrlField(structuredValue, fieldNames, visited) : false;
101
+ }
102
+ if (!value || typeof value !== 'object' || visited.has(value)) {
103
+ return false;
104
+ }
105
+ visited.add(value);
106
+
107
+ if (Array.isArray(value)) {
108
+ return value.some((item) => containsCredentialBearingUrlField(item, fieldNames, visited));
109
+ }
110
+
111
+ return Object.entries(value as Record<string, unknown>).some(([key, item]) => {
112
+ if (pathSegments(key).some((segment) => fieldNames.has(segment)) && containsUrlUserInfo(item)) {
113
+ return true;
114
+ }
115
+ return containsCredentialBearingUrlField(item, fieldNames, visited);
116
+ });
117
+ }
118
+
119
+ /**
120
+ * Preserve the rest of a URL while masking URL userinfo in legacy data or an
121
+ * accidentally nested API response.
122
+ */
123
+ export function redactUrlUserInfo(value: string): string {
124
+ // Userinfo may itself contain an unescaped `@`; match through the final
125
+ // authority `@` rather than stopping at the first one and leaking part of
126
+ // a password in a malformed or legacy URL.
127
+ return value.replace(/([a-z][a-z\d+.-]*:\/\/)([^/?#\s]+)@/gi, '$1***:***@');
128
+ }