Skip to content

Commit 1b94ceb

Browse files
committed
fix(gitlab): validate combined permission uploads before saving
1 parent 585d4d6 commit 1b94ceb

10 files changed

Lines changed: 83 additions & 12 deletions

File tree

‎apps/docs/openapi-v2-resources.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10233,7 +10233,7 @@
1023310233
"put": {
1023410234
"operationId": "replaceSearchSourcePermissions",
1023510235
"summary": "Replace Search source permissions",
10236-
"description": "Replace the complete permission grants for one Search source. Requires organization Admin access. Supply the current revision and the grants for the configured provider. A stale revision returns 409. GitLab CSV mode accepts both files together, up to 4 MiB and 100,000 data rows each. Workspace API keys return `403`; use a personal API key or scoped OAuth token.\n\nOAuth scope: `api:write`.",
10236+
"description": "Replace the complete permission grants for one Search source. Requires organization Admin access. Supply the current revision and the grants for the configured provider. A stale revision returns 409. GitLab CSV mode accepts both files together, up to 8 MiB and 500,000 data rows each, within a 10 MiB limit for the complete JSON-encoded request. Workspace API keys return `403`; use a personal API key or scoped OAuth token.\n\nOAuth scope: `api:write`.",
1023710237
"x-sim-operation": "knowledge.search.source_permissions.replace",
1023810238
"x-oauth-scope": "api:write",
1023910239
"tags": ["Organizations"],

‎apps/docs/scripts/fixtures/content-baseline.json‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -177236,8 +177236,8 @@
177236177236
{
177237177237
"lang": "yaml",
177238177238
"meta": "title=\".gitlab-ci.yml\"",
177239-
"sha256": "29261e0a66aac3cf52e7b9164aaa44f235765bcfd2cae4e68d744d31b29b57bf",
177240-
"bytes": 1441,
177239+
"sha256": "95f1b2ec16f400d5e0ebe5cd67ae2b9f6476f2c86696a54f9a983b2b8d02a5c7",
177240+
"bytes": 1613,
177241177241
"line": 129
177242177242
},
177243177243
{

‎apps/sim/connectors/gitlab/permission-config/fields.tsx‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,12 @@ function GitLabCsvFile({ kind, form, disabled }: GitLabCsvFileProps) {
8888
disabled={disabled}
8989
loading={selected?.loading}
9090
label={selected?.loading ? 'Validating…' : (filename ?? 'Drop CSV or click to upload')}
91-
error={selected?.error}
91+
error={selected?.error ?? (kind === 'projectPermissions' ? form.uploadError : undefined)}
9292
description={
93-
filename && count !== undefined && !selected?.loading
93+
filename &&
94+
count !== undefined &&
95+
!selected?.loading &&
96+
!(selected?.upload && form.uploadError)
9497
? `${count.toLocaleString()} ${count === 1 ? 'row' : 'rows'} · ${selected?.upload ? 'Ready to save' : 'Saved'}`
9598
: undefined
9699
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
const MAX_UPLOAD_BODY_BYTES = 10 * 1024 * 1024
2+
3+
/** Check encoded CSV content and its request envelope against the proxy's JSON body ceiling. */
4+
export function getGitLabUploadSizeError(body: unknown): string | undefined {
5+
if (new TextEncoder().encode(JSON.stringify(body)).byteLength > MAX_UPLOAD_BODY_BYTES)
6+
return 'The complete upload must be 10 MiB or smaller. Use smaller CSV files or a narrower group.'
7+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/** @vitest-environment jsdom */
2+
import { File } from 'node:buffer'
3+
import { act, createElement } from 'react'
4+
import { createRoot } from 'react-dom/client'
5+
import { describe, expect, it, vi } from 'vitest'
6+
import { useGitLabPermissionForm } from '@/connectors/gitlab/permission-config/use-permission-form'
7+
8+
describe('GitLab permission file selection', () => {
9+
it('blocks an oversized combined upload and recovers when a file is replaced', async () => {
10+
vi.stubGlobal('IS_REACT_ACT_ENVIRONMENT', true)
11+
const root = createRoot(document.createElement('div'))
12+
let form: ReturnType<typeof useGitLabPermissionForm> | undefined
13+
function Probe() {
14+
form = useGitLabPermissionForm()
15+
return null
16+
}
17+
const current = () => {
18+
if (!form) throw new Error('Permission form was not mounted')
19+
return form
20+
}
21+
act(() => root.render(createElement(Probe)))
22+
const csv = (content: string, filename: string) =>
23+
Object.assign(new File([content], filename), { webkitRelativePath: '' })
24+
try {
25+
act(() => current().setMode('csv'))
26+
const users = csv('1,a@example.test\n'.repeat(300_000), 'users.csv')
27+
const grants = csv('group/p,1\n'.repeat(490_000), 'projects.csv')
28+
expect(users.size + grants.size).toBeLessThan(10 * 1024 * 1024)
29+
await act(async () => {
30+
await current().selectFile('userMapping', users)
31+
await current().selectFile('projectPermissions', grants)
32+
})
33+
expect(current().files.userMapping?.error).toBeUndefined()
34+
expect(current().files.projectPermissions?.error).toBeUndefined()
35+
expect(current().complete).toBe(false)
36+
await act(async () => {
37+
await current().selectFile('projectPermissions', csv('group/project,1', 'projects.csv'))
38+
})
39+
expect(current().complete).toBe(true)
40+
act(() => current().reset())
41+
expect(current().files).toEqual({})
42+
} finally {
43+
act(() => root.unmount())
44+
vi.unstubAllGlobals()
45+
}
46+
})
47+
})

‎apps/sim/connectors/gitlab/permission-config/use-permission-form.ts‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import type {
77
GitLabPermissionUploadInput,
88
} from '@/lib/api/contracts/knowledge/gitlab-permissions'
99
import { parseGitLabCsv } from '@/connectors/gitlab/permission-config/parser'
10+
import { getGitLabUploadSizeError } from '@/connectors/gitlab/permission-config/request-size'
1011
import {
1112
GITLAB_CSV_MAX_BYTES,
1213
type GitLabCsvKind,
@@ -55,6 +56,11 @@ export function useGitLabPermissionForm(initial?: GitLabPermissionData) {
5556
}),
5657
[files, mode, saved]
5758
)
59+
const uploadError = useMemo(
60+
() =>
61+
input.mode === 'csv' ? getGitLabUploadSizeError({ permissionConfig: input }) : undefined,
62+
[input]
63+
)
5864
const selectFile = useCallback(async (kind: GitLabCsvKind, file: File) => {
5965
const generation = ++generations.current[kind]
6066
setFiles((previous) => ({ ...previous, [kind]: { loading: true } }))
@@ -89,7 +95,8 @@ export function useGitLabPermissionForm(initial?: GitLabPermissionData) {
8995
apiKey,
9096
setApiKey,
9197
input,
92-
complete,
98+
complete: complete && !uploadError,
99+
uploadError,
93100
dirty,
94101
selectFile,
95102
reset,

‎apps/sim/hooks/queries/kb/connectors.ts‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,13 @@ import type {
3838
} from '@/lib/api/contracts/knowledge/connectors'
3939
import {
4040
type ConnectorDocumentsQuery,
41+
createConnectorBodySchema,
4142
type PrepareSearchSourceBody,
4243
prepareSearchSourceContract,
4344
readSearchIndexContract,
4445
type SearchConnectionOAuthQuery,
4546
type SearchSourcePage,
47+
updateConnectorBodySchema,
4648
} from '@/lib/api/contracts/knowledge/connectors'
4749
import {
4850
type ResourceScope,
@@ -54,6 +56,7 @@ import { connectCredentialGroupInPopup } from '@/lib/credential-groups/oauth-pop
5456
import { isDesktopApp } from '@/lib/desktop'
5557
import { connectDesktopSource } from '@/lib/desktop/source-connect'
5658
import { MAX_KNOWLEDGE_CONNECTOR_DOCUMENT_PAGE_SIZE } from '@/lib/knowledge/constants'
59+
import { getGitLabUploadSizeError } from '@/connectors/gitlab/permission-config/request-size'
5760
import { credentialGroupKeys } from '@/hooks/queries/utils/credential-group-queries'
5861
import { knowledgeKeys } from '@/hooks/queries/utils/knowledge-keys'
5962
import { organizationAccountsKeys } from '@/hooks/queries/utils/organization-accounts-keys'
@@ -276,9 +279,12 @@ async function createConnector({
276279
knowledgeBaseId,
277280
...body
278281
}: CreateConnectorParams): Promise<ConnectorData> {
282+
const payload = createConnectorBodySchema.parse(body)
283+
const uploadError = payload.permissionConfig?.mode === 'csv' && getGitLabUploadSizeError(payload)
284+
if (uploadError) throw new Error(uploadError)
279285
const result = await requestJson(createKnowledgeConnectorContract, {
280286
params: { id: knowledgeBaseId },
281-
body,
287+
body: payload,
282288
})
283289

284290
return result.data
@@ -323,9 +329,12 @@ async function updateConnector({
323329
connectorId,
324330
updates,
325331
}: UpdateConnectorParams): Promise<ConnectorData> {
332+
const payload = updateConnectorBodySchema.parse(updates)
333+
const uploadError = payload.permissionConfig?.mode === 'csv' && getGitLabUploadSizeError(payload)
334+
if (uploadError) throw new Error(uploadError)
326335
const result = await requestJson(updateKnowledgeConnectorContract, {
327336
params: { id: knowledgeBaseId, connectorId },
328-
body: updates,
337+
body: payload,
329338
})
330339

331340
return result.data

‎apps/sim/lib/api/contracts/v2/openapi/organizations.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -592,7 +592,7 @@ export const organizationOpenApiRoutes = [
592592
applicationOperation: searchSourcePermissionOperations.replace,
593593
operationId: 'replaceSearchSourcePermissions',
594594
summary: 'Replace Search source permissions',
595-
description: `Replace the complete permission grants for one Search source. Requires organization Admin access. Supply the current revision and the grants for the configured provider. A stale revision returns 409. GitLab CSV mode accepts both files together, up to 4 MiB and 100,000 data rows each. ${WORKSPACE_API_KEY_DENIED}`,
595+
description: `Replace the complete permission grants for one Search source. Requires organization Admin access. Supply the current revision and the grants for the configured provider. A stale revision returns 409. GitLab CSV mode accepts both files together, up to 8 MiB and 500,000 data rows each, within a 10 MiB limit for the complete JSON-encoded request. ${WORKSPACE_API_KEY_DENIED}`,
596596
tags: ['Organizations'],
597597
errors: RESOURCE_CONFLICT_ERRORS,
598598
success: { description: 'The new permission revision.', headers: RATE_LIMIT_HEADERS },

‎apps/sim/lib/api/mcp/generated/v2-operations.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2516,7 +2516,7 @@ export const V2_MCP_OPERATIONS = {
25162516
contract: v2ReplaceSearchSourcePermissionsContract,
25172517
summary: 'Replace Search source permissions',
25182518
description:
2519-
'Replace the complete permission grants for one Search source. Requires organization Admin access. Supply the current revision and the grants for the configured provider. A stale revision returns 409. GitLab CSV mode accepts both files together, up to 4 MiB and 100,000 data rows each. Workspace API keys return `403`; use a personal API key or scoped OAuth token.\n\nOAuth scope: `api:write`.',
2519+
'Replace the complete permission grants for one Search source. Requires organization Admin access. Supply the current revision and the grants for the configured provider. A stale revision returns 409. GitLab CSV mode accepts both files together, up to 8 MiB and 500,000 data rows each, within a 10 MiB limit for the complete JSON-encoded request. Workspace API keys return `403`; use a personal API key or scoped OAuth token.\n\nOAuth scope: `api:write`.',
25202520
workspaceKeyUnsupported: true,
25212521
handler: () =>
25222522
import(

‎scripts/check-unused-exports.baseline.json‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1521,7 +1521,6 @@
15211521
"apps/sim/lib/api/contracts/knowledge/connectors.ts#connectorDocumentsQuerySchema",
15221522
"apps/sim/lib/api/contracts/knowledge/connectors.ts#connectorMemberSummarySchema",
15231523
"apps/sim/lib/api/contracts/knowledge/connectors.ts#connectorRequestedAccessModeSchema",
1524-
"apps/sim/lib/api/contracts/knowledge/connectors.ts#createConnectorBodySchema",
15251524
"apps/sim/lib/api/contracts/knowledge/connectors.ts#deleteConnectorQuerySchema",
15261525
"apps/sim/lib/api/contracts/knowledge/connectors.ts#listSearchSourcesQuerySchema",
15271526
"apps/sim/lib/api/contracts/knowledge/connectors.ts#memberSyncLogDataSchema",
@@ -1532,7 +1531,6 @@
15321531
"apps/sim/lib/api/contracts/knowledge/connectors.ts#syncLogStatusSchema",
15331532
"apps/sim/lib/api/contracts/knowledge/connectors.ts#triggerKnowledgeConnectorSyncQuerySchema",
15341533
"apps/sim/lib/api/contracts/knowledge/connectors.ts#updateConnectorAccessBodySchema",
1535-
"apps/sim/lib/api/contracts/knowledge/connectors.ts#updateConnectorBodySchema",
15361534
"apps/sim/lib/api/contracts/knowledge/connectors.ts#viewerConnectorMembershipSchema",
15371535
"apps/sim/lib/api/contracts/knowledge/documents.ts#BulkCreateDocumentsBody",
15381536
"apps/sim/lib/api/contracts/knowledge/documents.ts#CreateKnowledgeDocumentsBody",

0 commit comments

Comments
 (0)