Skip to content

Commit 748ee49

Browse files
authored
fix(github): encode owner and repo in request paths (#10085)
* fix(github): encode owner and repo in request paths Route owner, repo, numeric ids, gist ids, usernames and workflow ids through safeUrlPathSegment, and branches/refs through a slash-preserving encoder, so a model-supplied value cannot steer the authenticated request to another api.github.com endpoint. Real names produce byte-identical URLs. * fix(github): await comment operations so path errors return 400
1 parent f42cf21 commit 748ee49

79 files changed

Lines changed: 428 additions & 91 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎apps/sim/lib/internal/github/execute-tool.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ export const executeGitHubTool: InternalToolOperationHandler = async (request) =
2222
try {
2323
switch (request.toolId) {
2424
case 'github_comment':
25-
return executeToolOperationImplementation(executeGitHubCommentOperation, request)
25+
return await executeToolOperationImplementation(executeGitHubCommentOperation, request)
2626
case 'github_comment_v2':
27-
return executeToolOperationImplementation(executeGitHubCommentV2Operation, request)
27+
return await executeToolOperationImplementation(executeGitHubCommentV2Operation, request)
2828
case 'github_latest_commit':
2929
case 'github_latest_commit_v2': {
3030
const parsed = inputSchema.safeParse(request.input)

‎apps/sim/lib/internal/github/operations.ts‎

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { createLogger } from '@sim/logger'
2+
import { getErrorMessage } from '@sim/utils/errors'
23
import { isRecordLike } from '@sim/utils/object'
34
import {
45
secureFetchWithPinnedIP,
@@ -11,6 +12,7 @@ import {
1112
} from '@/lib/core/utils/stream-limits'
1213
import { GitHubOperationError } from '@/lib/internal/github/errors'
1314
import { MAX_BUFFERED_TRANSFER_BYTES } from '@/lib/uploads/shared/types'
15+
import { githubPathSegment, githubRepoPath } from '@/tools/github/request-path'
1416
import { formatGitHubErrorMessage } from '@/tools/github/response-parsers'
1517
import type {
1618
CreateCommentParams,
@@ -19,6 +21,7 @@ import type {
1921
} from '@/tools/github/types'
2022
import { secureGitHubRequest } from '@/tools/github/utils.server'
2123
import type { ToolResponse } from '@/tools/types'
24+
import { safeUrlPathSegment } from '@/tools/url-path'
2225

2326
const logger = createLogger('GitHubLatestCommitOperation')
2427
const MAX_COMMIT_RESPONSE_BYTES = 10 * 1024 * 1024
@@ -123,8 +126,20 @@ function githubHeaders(apiKey: string): Record<string, string> {
123126
}
124127
}
125128

129+
/** Builds a request path from caller input, reporting a rejected segment as a 400. */
130+
function buildRequestPath(build: () => string): string {
131+
try {
132+
return build()
133+
} catch (error) {
134+
throw new GitHubOperationError(getErrorMessage(error, 'Invalid GitHub request path'), 400)
135+
}
136+
}
137+
126138
function pullRequestUrl(params: CreateCommentParams): string {
127-
return `${GITHUB_API_BASE}/repos/${params.owner}/${params.repo}/pulls/${params.pullNumber}`
139+
return buildRequestPath(
140+
() =>
141+
`${GITHUB_API_BASE}/repos/${githubRepoPath(params.owner, params.repo)}/pulls/${githubPathSegment(params.pullNumber, 'pullNumber')}`
142+
)
128143
}
129144

130145
function isFileCommentRequest(params: CreateCommentParams): boolean {
@@ -400,10 +415,10 @@ export async function getGitHubLatestCommit(
400415
context: GitHubOperationContext
401416
): Promise<LatestCommitResponse> {
402417
context.signal?.throwIfAborted()
403-
const owner = encodeURIComponent(input.owner)
404-
const repo = encodeURIComponent(input.repo)
405-
const revision = encodeURIComponent(input.branch || 'HEAD')
406-
const commitUrl = `https://api.github.com/repos/${owner}/${repo}/commits/${revision}`
418+
const commitUrl = buildRequestPath(
419+
() =>
420+
`${GITHUB_API_BASE}/repos/${githubRepoPath(input.owner, input.repo)}/commits/${safeUrlPathSegment(input.branch || 'HEAD', 'branch', { allowSeparators: true })}`
421+
)
407422
const validation = await validateUrlWithDNS(commitUrl, 'commitUrl', 'configuredEndpoint')
408423
context.signal?.throwIfAborted()
409424
if (!validation.isValid) {

‎apps/sim/tools/github/add_assignees.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { githubPathSegment, githubRepoPath } from '@/tools/github/request-path'
12
import type { AddAssigneesParams, IssueResponse } from '@/tools/github/types'
23
import { GITHUB_CREDENTIAL_OAUTH } from '@/tools/shared/saved-credentials'
34
import type { ToolConfig } from '@/tools/types'
@@ -44,7 +45,7 @@ export const addAssigneesTool: ToolConfig<AddAssigneesParams, IssueResponse> = {
4445

4546
request: {
4647
url: (params) =>
47-
`https://api.github.com/repos/${params.owner}/${params.repo}/issues/${params.issue_number}/assignees`,
48+
`https://api.github.com/repos/${githubRepoPath(params.owner, params.repo)}/issues/${githubPathSegment(params.issue_number, 'issue_number')}/assignees`,
4849
method: 'POST',
4950
headers: (params) => ({
5051
Accept: 'application/vnd.github.v3+json',

‎apps/sim/tools/github/add_labels.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { githubPathSegment, githubRepoPath } from '@/tools/github/request-path'
12
import type { AddLabelsParams, LabelsResponse } from '@/tools/github/types'
23
import { GITHUB_CREDENTIAL_OAUTH } from '@/tools/shared/saved-credentials'
34
import type { ToolConfig } from '@/tools/types'
@@ -44,7 +45,7 @@ export const addLabelsTool: ToolConfig<AddLabelsParams, LabelsResponse> = {
4445

4546
request: {
4647
url: (params) =>
47-
`https://api.github.com/repos/${params.owner}/${params.repo}/issues/${params.issue_number}/labels`,
48+
`https://api.github.com/repos/${githubRepoPath(params.owner, params.repo)}/issues/${githubPathSegment(params.issue_number, 'issue_number')}/labels`,
4849
method: 'POST',
4950
headers: (params) => ({
5051
Accept: 'application/vnd.github.v3+json',

‎apps/sim/tools/github/cancel_workflow_run.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { githubPathSegment, githubRepoPath } from '@/tools/github/request-path'
12
import type { CancelWorkflowRunParams, CancelWorkflowRunResponse } from '@/tools/github/types'
23
import { GITHUB_CREDENTIAL_OAUTH } from '@/tools/shared/saved-credentials'
34
import type { ToolConfig } from '@/tools/types'
@@ -40,7 +41,7 @@ export const cancelWorkflowRunTool: ToolConfig<CancelWorkflowRunParams, CancelWo
4041

4142
request: {
4243
url: (params) =>
43-
`https://api.github.com/repos/${params.owner}/${params.repo}/actions/runs/${params.run_id}/cancel`,
44+
`https://api.github.com/repos/${githubRepoPath(params.owner, params.repo)}/actions/runs/${githubPathSegment(params.run_id, 'run_id')}/cancel`,
4445
method: 'POST',
4546
headers: (params) => ({
4647
Accept: 'application/vnd.github+json',

‎apps/sim/tools/github/close_issue.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { githubPathSegment, githubRepoPath } from '@/tools/github/request-path'
12
import type { CloseIssueParams, IssueResponse } from '@/tools/github/types'
23
import { ISSUE_OUTPUT_PROPERTIES, LABEL_OUTPUT, USER_OUTPUT } from '@/tools/github/types'
34
import { GITHUB_CREDENTIAL_OAUTH } from '@/tools/shared/saved-credentials'
@@ -45,7 +46,7 @@ export const closeIssueTool: ToolConfig<CloseIssueParams, IssueResponse> = {
4546

4647
request: {
4748
url: (params) =>
48-
`https://api.github.com/repos/${params.owner}/${params.repo}/issues/${params.issue_number}`,
49+
`https://api.github.com/repos/${githubRepoPath(params.owner, params.repo)}/issues/${githubPathSegment(params.issue_number, 'issue_number')}`,
4950
method: 'PATCH',
5051
headers: (params) => ({
5152
Accept: 'application/vnd.github.v3+json',

‎apps/sim/tools/github/close_pr.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { githubPathSegment, githubRepoPath } from '@/tools/github/request-path'
12
import type { ClosePRParams, PRResponse } from '@/tools/github/types'
23
import { GITHUB_CREDENTIAL_OAUTH } from '@/tools/shared/saved-credentials'
34
import type { ToolConfig } from '@/tools/types'
@@ -38,7 +39,7 @@ export const closePRTool: ToolConfig<ClosePRParams, PRResponse> = {
3839

3940
request: {
4041
url: (params) =>
41-
`https://api.github.com/repos/${params.owner}/${params.repo}/pulls/${params.pullNumber}`,
42+
`https://api.github.com/repos/${githubRepoPath(params.owner, params.repo)}/pulls/${githubPathSegment(params.pullNumber, 'pullNumber')}`,
4243
method: 'PATCH',
4344
headers: (params) => ({
4445
Accept: 'application/vnd.github.v3+json',

‎apps/sim/tools/github/compare_commits.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { githubRefPath, githubRepoPath } from '@/tools/github/request-path'
12
import {
23
COMMIT_DATA_OUTPUT,
34
COMMIT_FILE_OUTPUT_PROPERTIES,
@@ -105,7 +106,7 @@ export const compareCommitsTool: ToolConfig<CompareCommitsParams, CompareCommits
105106
request: {
106107
url: (params) => {
107108
const url = new URL(
108-
`https://api.github.com/repos/${params.owner}/${params.repo}/compare/${params.base}...${params.head}`
109+
`https://api.github.com/repos/${githubRepoPath(params.owner, params.repo)}/compare/${githubRefPath(params.base, 'base')}...${githubRefPath(params.head, 'head')}`
109110
)
110111
if (params.per_page) url.searchParams.append('per_page', String(params.per_page))
111112
if (params.page) url.searchParams.append('page', String(params.page))

‎apps/sim/tools/github/create_branch.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { githubRepoPath } from '@/tools/github/request-path'
12
import type { CreateBranchParams, RefResponse } from '@/tools/github/types'
23
import { GIT_REF_OUTPUT_PROPERTIES } from '@/tools/github/types'
34
import { GITHUB_CREDENTIAL_OAUTH } from '@/tools/shared/saved-credentials'
@@ -45,7 +46,8 @@ export const createBranchTool: ToolConfig<CreateBranchParams, RefResponse> = {
4546
},
4647

4748
request: {
48-
url: (params) => `https://api.github.com/repos/${params.owner}/${params.repo}/git/refs`,
49+
url: (params) =>
50+
`https://api.github.com/repos/${githubRepoPath(params.owner, params.repo)}/git/refs`,
4951
method: 'POST',
5052
headers: (params) => ({
5153
Accept: 'application/vnd.github+json',

‎apps/sim/tools/github/create_comment_reaction.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { githubPathSegment, githubRepoPath } from '@/tools/github/request-path'
12
import { REACTION_OUTPUT_PROPERTIES, USER_OUTPUT } from '@/tools/github/types'
23
import { GITHUB_CREDENTIAL_OAUTH } from '@/tools/shared/saved-credentials'
34
import type { ToolConfig } from '@/tools/types'
@@ -69,7 +70,7 @@ export const createCommentReactionTool: ToolConfig<
6970

7071
request: {
7172
url: (params) =>
72-
`https://api.github.com/repos/${params.owner}/${params.repo}/issues/comments/${params.comment_id}/reactions`,
73+
`https://api.github.com/repos/${githubRepoPath(params.owner, params.repo)}/issues/comments/${githubPathSegment(params.comment_id, 'comment_id')}/reactions`,
7374
method: 'POST',
7475
headers: (params) => ({
7576
Accept: 'application/vnd.github.squirrel-girl-preview+json',

0 commit comments

Comments
 (0)