diff --git a/apps/sim/lib/internal/github/execute-tool.ts b/apps/sim/lib/internal/github/execute-tool.ts index 5043273f8a4..418db0433e2 100644 --- a/apps/sim/lib/internal/github/execute-tool.ts +++ b/apps/sim/lib/internal/github/execute-tool.ts @@ -22,9 +22,9 @@ export const executeGitHubTool: InternalToolOperationHandler = async (request) = try { switch (request.toolId) { case 'github_comment': - return executeToolOperationImplementation(executeGitHubCommentOperation, request) + return await executeToolOperationImplementation(executeGitHubCommentOperation, request) case 'github_comment_v2': - return executeToolOperationImplementation(executeGitHubCommentV2Operation, request) + return await executeToolOperationImplementation(executeGitHubCommentV2Operation, request) case 'github_latest_commit': case 'github_latest_commit_v2': { const parsed = inputSchema.safeParse(request.input) diff --git a/apps/sim/lib/internal/github/operations.ts b/apps/sim/lib/internal/github/operations.ts index 50d5198471b..70132b053e3 100644 --- a/apps/sim/lib/internal/github/operations.ts +++ b/apps/sim/lib/internal/github/operations.ts @@ -1,4 +1,5 @@ import { createLogger } from '@sim/logger' +import { getErrorMessage } from '@sim/utils/errors' import { isRecordLike } from '@sim/utils/object' import { secureFetchWithPinnedIP, @@ -11,6 +12,7 @@ import { } from '@/lib/core/utils/stream-limits' import { GitHubOperationError } from '@/lib/internal/github/errors' import { MAX_BUFFERED_TRANSFER_BYTES } from '@/lib/uploads/shared/types' +import { githubPathSegment, githubRepoPath } from '@/tools/github/request-path' import { formatGitHubErrorMessage } from '@/tools/github/response-parsers' import type { CreateCommentParams, @@ -19,6 +21,7 @@ import type { } from '@/tools/github/types' import { secureGitHubRequest } from '@/tools/github/utils.server' import type { ToolResponse } from '@/tools/types' +import { safeUrlPathSegment } from '@/tools/url-path' const logger = createLogger('GitHubLatestCommitOperation') const MAX_COMMIT_RESPONSE_BYTES = 10 * 1024 * 1024 @@ -123,8 +126,20 @@ function githubHeaders(apiKey: string): Record { } } +/** Builds a request path from caller input, reporting a rejected segment as a 400. */ +function buildRequestPath(build: () => string): string { + try { + return build() + } catch (error) { + throw new GitHubOperationError(getErrorMessage(error, 'Invalid GitHub request path'), 400) + } +} + function pullRequestUrl(params: CreateCommentParams): string { - return `${GITHUB_API_BASE}/repos/${params.owner}/${params.repo}/pulls/${params.pullNumber}` + return buildRequestPath( + () => + `${GITHUB_API_BASE}/repos/${githubRepoPath(params.owner, params.repo)}/pulls/${githubPathSegment(params.pullNumber, 'pullNumber')}` + ) } function isFileCommentRequest(params: CreateCommentParams): boolean { @@ -400,10 +415,10 @@ export async function getGitHubLatestCommit( context: GitHubOperationContext ): Promise { context.signal?.throwIfAborted() - const owner = encodeURIComponent(input.owner) - const repo = encodeURIComponent(input.repo) - const revision = encodeURIComponent(input.branch || 'HEAD') - const commitUrl = `https://api.github.com/repos/${owner}/${repo}/commits/${revision}` + const commitUrl = buildRequestPath( + () => + `${GITHUB_API_BASE}/repos/${githubRepoPath(input.owner, input.repo)}/commits/${safeUrlPathSegment(input.branch || 'HEAD', 'branch', { allowSeparators: true })}` + ) const validation = await validateUrlWithDNS(commitUrl, 'commitUrl', 'configuredEndpoint') context.signal?.throwIfAborted() if (!validation.isValid) { diff --git a/apps/sim/tools/github/add_assignees.ts b/apps/sim/tools/github/add_assignees.ts index 2e6f1d8756d..2f616408fc4 100644 --- a/apps/sim/tools/github/add_assignees.ts +++ b/apps/sim/tools/github/add_assignees.ts @@ -1,3 +1,4 @@ +import { githubPathSegment, githubRepoPath } from '@/tools/github/request-path' import type { AddAssigneesParams, IssueResponse } from '@/tools/github/types' import { GITHUB_CREDENTIAL_OAUTH } from '@/tools/shared/saved-credentials' import type { ToolConfig } from '@/tools/types' @@ -44,7 +45,7 @@ export const addAssigneesTool: ToolConfig = { request: { url: (params) => - `https://api.github.com/repos/${params.owner}/${params.repo}/issues/${params.issue_number}/assignees`, + `https://api.github.com/repos/${githubRepoPath(params.owner, params.repo)}/issues/${githubPathSegment(params.issue_number, 'issue_number')}/assignees`, method: 'POST', headers: (params) => ({ Accept: 'application/vnd.github.v3+json', diff --git a/apps/sim/tools/github/add_labels.ts b/apps/sim/tools/github/add_labels.ts index dbab7152ace..7a74e18e2ab 100644 --- a/apps/sim/tools/github/add_labels.ts +++ b/apps/sim/tools/github/add_labels.ts @@ -1,3 +1,4 @@ +import { githubPathSegment, githubRepoPath } from '@/tools/github/request-path' import type { AddLabelsParams, LabelsResponse } from '@/tools/github/types' import { GITHUB_CREDENTIAL_OAUTH } from '@/tools/shared/saved-credentials' import type { ToolConfig } from '@/tools/types' @@ -44,7 +45,7 @@ export const addLabelsTool: ToolConfig = { request: { url: (params) => - `https://api.github.com/repos/${params.owner}/${params.repo}/issues/${params.issue_number}/labels`, + `https://api.github.com/repos/${githubRepoPath(params.owner, params.repo)}/issues/${githubPathSegment(params.issue_number, 'issue_number')}/labels`, method: 'POST', headers: (params) => ({ Accept: 'application/vnd.github.v3+json', diff --git a/apps/sim/tools/github/cancel_workflow_run.ts b/apps/sim/tools/github/cancel_workflow_run.ts index dc9d4578b12..486ab961d11 100644 --- a/apps/sim/tools/github/cancel_workflow_run.ts +++ b/apps/sim/tools/github/cancel_workflow_run.ts @@ -1,3 +1,4 @@ +import { githubPathSegment, githubRepoPath } from '@/tools/github/request-path' import type { CancelWorkflowRunParams, CancelWorkflowRunResponse } from '@/tools/github/types' import { GITHUB_CREDENTIAL_OAUTH } from '@/tools/shared/saved-credentials' import type { ToolConfig } from '@/tools/types' @@ -40,7 +41,7 @@ export const cancelWorkflowRunTool: ToolConfig - `https://api.github.com/repos/${params.owner}/${params.repo}/actions/runs/${params.run_id}/cancel`, + `https://api.github.com/repos/${githubRepoPath(params.owner, params.repo)}/actions/runs/${githubPathSegment(params.run_id, 'run_id')}/cancel`, method: 'POST', headers: (params) => ({ Accept: 'application/vnd.github+json', diff --git a/apps/sim/tools/github/close_issue.ts b/apps/sim/tools/github/close_issue.ts index adf324bf034..2ab5ce16cce 100644 --- a/apps/sim/tools/github/close_issue.ts +++ b/apps/sim/tools/github/close_issue.ts @@ -1,3 +1,4 @@ +import { githubPathSegment, githubRepoPath } from '@/tools/github/request-path' import type { CloseIssueParams, IssueResponse } from '@/tools/github/types' import { ISSUE_OUTPUT_PROPERTIES, LABEL_OUTPUT, USER_OUTPUT } from '@/tools/github/types' import { GITHUB_CREDENTIAL_OAUTH } from '@/tools/shared/saved-credentials' @@ -45,7 +46,7 @@ export const closeIssueTool: ToolConfig = { request: { url: (params) => - `https://api.github.com/repos/${params.owner}/${params.repo}/issues/${params.issue_number}`, + `https://api.github.com/repos/${githubRepoPath(params.owner, params.repo)}/issues/${githubPathSegment(params.issue_number, 'issue_number')}`, method: 'PATCH', headers: (params) => ({ Accept: 'application/vnd.github.v3+json', diff --git a/apps/sim/tools/github/close_pr.ts b/apps/sim/tools/github/close_pr.ts index 582f2dc5bc9..b5976b0aee5 100644 --- a/apps/sim/tools/github/close_pr.ts +++ b/apps/sim/tools/github/close_pr.ts @@ -1,3 +1,4 @@ +import { githubPathSegment, githubRepoPath } from '@/tools/github/request-path' import type { ClosePRParams, PRResponse } from '@/tools/github/types' import { GITHUB_CREDENTIAL_OAUTH } from '@/tools/shared/saved-credentials' import type { ToolConfig } from '@/tools/types' @@ -38,7 +39,7 @@ export const closePRTool: ToolConfig = { request: { url: (params) => - `https://api.github.com/repos/${params.owner}/${params.repo}/pulls/${params.pullNumber}`, + `https://api.github.com/repos/${githubRepoPath(params.owner, params.repo)}/pulls/${githubPathSegment(params.pullNumber, 'pullNumber')}`, method: 'PATCH', headers: (params) => ({ Accept: 'application/vnd.github.v3+json', diff --git a/apps/sim/tools/github/compare_commits.ts b/apps/sim/tools/github/compare_commits.ts index 3dc47e425ee..ec791d6b65f 100644 --- a/apps/sim/tools/github/compare_commits.ts +++ b/apps/sim/tools/github/compare_commits.ts @@ -1,3 +1,4 @@ +import { githubRefPath, githubRepoPath } from '@/tools/github/request-path' import { COMMIT_DATA_OUTPUT, COMMIT_FILE_OUTPUT_PROPERTIES, @@ -105,7 +106,7 @@ export const compareCommitsTool: ToolConfig { const url = new URL( - `https://api.github.com/repos/${params.owner}/${params.repo}/compare/${params.base}...${params.head}` + `https://api.github.com/repos/${githubRepoPath(params.owner, params.repo)}/compare/${githubRefPath(params.base, 'base')}...${githubRefPath(params.head, 'head')}` ) if (params.per_page) url.searchParams.append('per_page', String(params.per_page)) if (params.page) url.searchParams.append('page', String(params.page)) diff --git a/apps/sim/tools/github/create_branch.ts b/apps/sim/tools/github/create_branch.ts index be255cc7590..53e651e836e 100644 --- a/apps/sim/tools/github/create_branch.ts +++ b/apps/sim/tools/github/create_branch.ts @@ -1,3 +1,4 @@ +import { githubRepoPath } from '@/tools/github/request-path' import type { CreateBranchParams, RefResponse } from '@/tools/github/types' import { GIT_REF_OUTPUT_PROPERTIES } from '@/tools/github/types' import { GITHUB_CREDENTIAL_OAUTH } from '@/tools/shared/saved-credentials' @@ -45,7 +46,8 @@ export const createBranchTool: ToolConfig = { }, request: { - url: (params) => `https://api.github.com/repos/${params.owner}/${params.repo}/git/refs`, + url: (params) => + `https://api.github.com/repos/${githubRepoPath(params.owner, params.repo)}/git/refs`, method: 'POST', headers: (params) => ({ Accept: 'application/vnd.github+json', diff --git a/apps/sim/tools/github/create_comment_reaction.ts b/apps/sim/tools/github/create_comment_reaction.ts index 688a4246526..ceb96151b4b 100644 --- a/apps/sim/tools/github/create_comment_reaction.ts +++ b/apps/sim/tools/github/create_comment_reaction.ts @@ -1,3 +1,4 @@ +import { githubPathSegment, githubRepoPath } from '@/tools/github/request-path' import { REACTION_OUTPUT_PROPERTIES, USER_OUTPUT } from '@/tools/github/types' import { GITHUB_CREDENTIAL_OAUTH } from '@/tools/shared/saved-credentials' import type { ToolConfig } from '@/tools/types' @@ -69,7 +70,7 @@ export const createCommentReactionTool: ToolConfig< request: { url: (params) => - `https://api.github.com/repos/${params.owner}/${params.repo}/issues/comments/${params.comment_id}/reactions`, + `https://api.github.com/repos/${githubRepoPath(params.owner, params.repo)}/issues/comments/${githubPathSegment(params.comment_id, 'comment_id')}/reactions`, method: 'POST', headers: (params) => ({ Accept: 'application/vnd.github.squirrel-girl-preview+json', diff --git a/apps/sim/tools/github/create_file.ts b/apps/sim/tools/github/create_file.ts index 8bb9ce3ae07..0fd7e63eaf1 100644 --- a/apps/sim/tools/github/create_file.ts +++ b/apps/sim/tools/github/create_file.ts @@ -1,4 +1,5 @@ import { encodeContentPath } from '@/tools/github/content-path' +import { githubRepoPath } from '@/tools/github/request-path' import type { CreateFileParams, FileOperationResponse } from '@/tools/github/types' import { GITHUB_CREDENTIAL_OAUTH } from '@/tools/shared/saved-credentials' import type { ToolConfig } from '@/tools/types' @@ -58,7 +59,7 @@ export const createFileTool: ToolConfig request: { url: (params) => - `https://api.github.com/repos/${params.owner}/${params.repo}/contents/${encodeContentPath(params.path)}`, + `https://api.github.com/repos/${githubRepoPath(params.owner, params.repo)}/contents/${encodeContentPath(params.path)}`, method: 'PUT', headers: (params) => ({ Accept: 'application/vnd.github+json', diff --git a/apps/sim/tools/github/create_issue.ts b/apps/sim/tools/github/create_issue.ts index cb2f40bdcc4..08ad84b566f 100644 --- a/apps/sim/tools/github/create_issue.ts +++ b/apps/sim/tools/github/create_issue.ts @@ -1,3 +1,4 @@ +import { githubRepoPath } from '@/tools/github/request-path' import type { CreateIssueParams, IssueResponse } from '@/tools/github/types' import { ISSUE_OUTPUT_PROPERTIES, @@ -67,7 +68,8 @@ export const createIssueTool: ToolConfig = { }, request: { - url: (params) => `https://api.github.com/repos/${params.owner}/${params.repo}/issues`, + url: (params) => + `https://api.github.com/repos/${githubRepoPath(params.owner, params.repo)}/issues`, method: 'POST', headers: (params) => ({ Accept: 'application/vnd.github.v3+json', diff --git a/apps/sim/tools/github/create_issue_reaction.ts b/apps/sim/tools/github/create_issue_reaction.ts index 9cfc2bd58d4..d6548b4c094 100644 --- a/apps/sim/tools/github/create_issue_reaction.ts +++ b/apps/sim/tools/github/create_issue_reaction.ts @@ -1,3 +1,4 @@ +import { githubPathSegment, githubRepoPath } from '@/tools/github/request-path' import { REACTION_OUTPUT_PROPERTIES, USER_OUTPUT } from '@/tools/github/types' import { GITHUB_CREDENTIAL_OAUTH } from '@/tools/shared/saved-credentials' import type { ToolConfig } from '@/tools/types' @@ -69,7 +70,7 @@ export const createIssueReactionTool: ToolConfig< request: { url: (params) => - `https://api.github.com/repos/${params.owner}/${params.repo}/issues/${params.issue_number}/reactions`, + `https://api.github.com/repos/${githubRepoPath(params.owner, params.repo)}/issues/${githubPathSegment(params.issue_number, 'issue_number')}/reactions`, method: 'POST', headers: (params) => ({ Accept: 'application/vnd.github.squirrel-girl-preview+json', diff --git a/apps/sim/tools/github/create_milestone.ts b/apps/sim/tools/github/create_milestone.ts index bf71ae4a8b2..512de95a971 100644 --- a/apps/sim/tools/github/create_milestone.ts +++ b/apps/sim/tools/github/create_milestone.ts @@ -1,3 +1,4 @@ +import { githubRepoPath } from '@/tools/github/request-path' import { MILESTONE_CREATOR_OUTPUT, MILESTONE_V2_OUTPUT_PROPERTIES } from '@/tools/github/types' import { GITHUB_CREDENTIAL_OAUTH } from '@/tools/shared/saved-credentials' import type { ToolConfig } from '@/tools/types' @@ -85,7 +86,8 @@ export const createMilestoneTool: ToolConfig `https://api.github.com/repos/${params.owner}/${params.repo}/milestones`, + url: (params) => + `https://api.github.com/repos/${githubRepoPath(params.owner, params.repo)}/milestones`, method: 'POST', headers: (params) => ({ Accept: 'application/vnd.github.v3+json', diff --git a/apps/sim/tools/github/create_pr.ts b/apps/sim/tools/github/create_pr.ts index 797c76d9ee9..90452aff7a6 100644 --- a/apps/sim/tools/github/create_pr.ts +++ b/apps/sim/tools/github/create_pr.ts @@ -1,3 +1,4 @@ +import { githubRepoPath } from '@/tools/github/request-path' import type { CreatePRParams, PRResponse } from '@/tools/github/types' import { GITHUB_CREDENTIAL_OAUTH } from '@/tools/shared/saved-credentials' import type { ToolConfig } from '@/tools/types' @@ -61,7 +62,8 @@ export const createPRTool: ToolConfig = { }, request: { - url: (params) => `https://api.github.com/repos/${params.owner}/${params.repo}/pulls`, + url: (params) => + `https://api.github.com/repos/${githubRepoPath(params.owner, params.repo)}/pulls`, method: 'POST', headers: (params) => ({ Accept: 'application/vnd.github.v3+json', diff --git a/apps/sim/tools/github/create_pr_review.ts b/apps/sim/tools/github/create_pr_review.ts index f4bc0b111aa..163039a9d7f 100644 --- a/apps/sim/tools/github/create_pr_review.ts +++ b/apps/sim/tools/github/create_pr_review.ts @@ -1,4 +1,5 @@ import { isRecordLike } from '@sim/utils/object' +import { githubPathSegment, githubRepoPath } from '@/tools/github/request-path' import { nullableNonEmptyString, optionalNonEmptyString, @@ -161,7 +162,7 @@ export const createPRReviewTool: ToolConfig - `https://api.github.com/repos/${params.owner}/${params.repo}/pulls/${params.pullNumber}/reviews`, + `https://api.github.com/repos/${githubRepoPath(params.owner, params.repo)}/pulls/${githubPathSegment(params.pullNumber, 'pullNumber')}/reviews`, method: 'POST', headers: (params) => ({ Accept: 'application/vnd.github+json', diff --git a/apps/sim/tools/github/create_release.ts b/apps/sim/tools/github/create_release.ts index cf7e2c64a72..c6fd8d68253 100644 --- a/apps/sim/tools/github/create_release.ts +++ b/apps/sim/tools/github/create_release.ts @@ -1,3 +1,4 @@ +import { githubRepoPath } from '@/tools/github/request-path' import type { CreateReleaseParams, ReleaseResponse } from '@/tools/github/types' import { RELEASE_ASSET_OUTPUT_PROPERTIES, @@ -77,7 +78,8 @@ export const createReleaseTool: ToolConfig }, request: { - url: (params) => `https://api.github.com/repos/${params.owner}/${params.repo}/releases`, + url: (params) => + `https://api.github.com/repos/${githubRepoPath(params.owner, params.repo)}/releases`, method: 'POST', headers: (params) => ({ Accept: 'application/vnd.github+json', diff --git a/apps/sim/tools/github/delete_branch.ts b/apps/sim/tools/github/delete_branch.ts index 836e296972c..ce3617eb128 100644 --- a/apps/sim/tools/github/delete_branch.ts +++ b/apps/sim/tools/github/delete_branch.ts @@ -1,3 +1,4 @@ +import { githubRefPath, githubRepoPath } from '@/tools/github/request-path' import type { DeleteBranchParams, DeleteBranchResponse } from '@/tools/github/types' import { DELETE_BRANCH_OUTPUT_PROPERTIES } from '@/tools/github/types' import { GITHUB_CREDENTIAL_OAUTH } from '@/tools/shared/saved-credentials' @@ -40,7 +41,7 @@ export const deleteBranchTool: ToolConfig - `https://api.github.com/repos/${params.owner}/${params.repo}/git/refs/heads/${params.branch}`, + `https://api.github.com/repos/${githubRepoPath(params.owner, params.repo)}/git/refs/heads/${githubRefPath(params.branch, 'branch')}`, method: 'DELETE', headers: (params) => ({ Accept: 'application/vnd.github+json', diff --git a/apps/sim/tools/github/delete_comment.ts b/apps/sim/tools/github/delete_comment.ts index 9f08edd66e0..dd2b2089588 100644 --- a/apps/sim/tools/github/delete_comment.ts +++ b/apps/sim/tools/github/delete_comment.ts @@ -1,3 +1,4 @@ +import { githubPathSegment, githubRepoPath } from '@/tools/github/request-path' import type { DeleteCommentParams, DeleteCommentResponse } from '@/tools/github/types' import { GITHUB_CREDENTIAL_OAUTH } from '@/tools/shared/saved-credentials' import type { ToolConfig } from '@/tools/types' @@ -38,7 +39,7 @@ export const deleteCommentTool: ToolConfig - `https://api.github.com/repos/${params.owner}/${params.repo}/issues/comments/${params.comment_id}`, + `https://api.github.com/repos/${githubRepoPath(params.owner, params.repo)}/issues/comments/${githubPathSegment(params.comment_id, 'comment_id')}`, method: 'DELETE', headers: (params) => ({ Accept: 'application/vnd.github+json', diff --git a/apps/sim/tools/github/delete_comment_reaction.ts b/apps/sim/tools/github/delete_comment_reaction.ts index 7d859d88139..ba7bdbb5e76 100644 --- a/apps/sim/tools/github/delete_comment_reaction.ts +++ b/apps/sim/tools/github/delete_comment_reaction.ts @@ -1,3 +1,4 @@ +import { githubPathSegment, githubRepoPath } from '@/tools/github/request-path' import { GITHUB_CREDENTIAL_OAUTH } from '@/tools/shared/saved-credentials' import type { ToolConfig } from '@/tools/types' @@ -65,7 +66,7 @@ export const deleteCommentReactionTool: ToolConfig< request: { url: (params) => - `https://api.github.com/repos/${params.owner}/${params.repo}/issues/comments/${params.comment_id}/reactions/${params.reaction_id}`, + `https://api.github.com/repos/${githubRepoPath(params.owner, params.repo)}/issues/comments/${githubPathSegment(params.comment_id, 'comment_id')}/reactions/${githubPathSegment(params.reaction_id, 'reaction_id')}`, method: 'DELETE', headers: (params) => ({ Accept: 'application/vnd.github.squirrel-girl-preview+json', diff --git a/apps/sim/tools/github/delete_file.ts b/apps/sim/tools/github/delete_file.ts index 14236427a6f..6aae32d160f 100644 --- a/apps/sim/tools/github/delete_file.ts +++ b/apps/sim/tools/github/delete_file.ts @@ -1,4 +1,5 @@ import { encodeContentPath } from '@/tools/github/content-path' +import { githubRepoPath } from '@/tools/github/request-path' import type { DeleteFileParams, DeleteFileResponse } from '@/tools/github/types' import { GITHUB_CREDENTIAL_OAUTH } from '@/tools/shared/saved-credentials' import type { ToolConfig } from '@/tools/types' @@ -58,7 +59,7 @@ export const deleteFileTool: ToolConfig = request: { url: (params) => - `https://api.github.com/repos/${params.owner}/${params.repo}/contents/${encodeContentPath(params.path)}`, + `https://api.github.com/repos/${githubRepoPath(params.owner, params.repo)}/contents/${encodeContentPath(params.path)}`, method: 'DELETE', headers: (params) => ({ Accept: 'application/vnd.github+json', diff --git a/apps/sim/tools/github/delete_gist.ts b/apps/sim/tools/github/delete_gist.ts index 28bccefc50f..7b9dcfb737e 100644 --- a/apps/sim/tools/github/delete_gist.ts +++ b/apps/sim/tools/github/delete_gist.ts @@ -1,3 +1,4 @@ +import { githubPathSegment } from '@/tools/github/request-path' import { GITHUB_CREDENTIAL_OAUTH } from '@/tools/shared/saved-credentials' import type { ToolConfig } from '@/tools/types' @@ -40,7 +41,7 @@ export const deleteGistTool: ToolConfig = }, request: { - url: (params) => `https://api.github.com/gists/${params.gist_id?.trim()}`, + url: (params) => `https://api.github.com/gists/${githubPathSegment(params.gist_id, 'gist_id')}`, method: 'DELETE', headers: (params) => ({ Accept: 'application/vnd.github.v3+json', diff --git a/apps/sim/tools/github/delete_issue_reaction.ts b/apps/sim/tools/github/delete_issue_reaction.ts index 6b1766bca2a..7239dfe506d 100644 --- a/apps/sim/tools/github/delete_issue_reaction.ts +++ b/apps/sim/tools/github/delete_issue_reaction.ts @@ -1,3 +1,4 @@ +import { githubPathSegment, githubRepoPath } from '@/tools/github/request-path' import { GITHUB_CREDENTIAL_OAUTH } from '@/tools/shared/saved-credentials' import type { ToolConfig } from '@/tools/types' @@ -65,7 +66,7 @@ export const deleteIssueReactionTool: ToolConfig< request: { url: (params) => - `https://api.github.com/repos/${params.owner}/${params.repo}/issues/${params.issue_number}/reactions/${params.reaction_id}`, + `https://api.github.com/repos/${githubRepoPath(params.owner, params.repo)}/issues/${githubPathSegment(params.issue_number, 'issue_number')}/reactions/${githubPathSegment(params.reaction_id, 'reaction_id')}`, method: 'DELETE', headers: (params) => ({ Accept: 'application/vnd.github.squirrel-girl-preview+json', diff --git a/apps/sim/tools/github/delete_milestone.ts b/apps/sim/tools/github/delete_milestone.ts index 722fc0b560b..4c187f4d1ab 100644 --- a/apps/sim/tools/github/delete_milestone.ts +++ b/apps/sim/tools/github/delete_milestone.ts @@ -1,3 +1,4 @@ +import { githubPathSegment, githubRepoPath } from '@/tools/github/request-path' import { GITHUB_CREDENTIAL_OAUTH } from '@/tools/shared/saved-credentials' import type { ToolConfig } from '@/tools/types' @@ -55,7 +56,7 @@ export const deleteMilestoneTool: ToolConfig - `https://api.github.com/repos/${params.owner}/${params.repo}/milestones/${params.milestone_number}`, + `https://api.github.com/repos/${githubRepoPath(params.owner, params.repo)}/milestones/${githubPathSegment(params.milestone_number, 'milestone_number')}`, method: 'DELETE', headers: (params) => ({ Accept: 'application/vnd.github.v3+json', diff --git a/apps/sim/tools/github/delete_release.ts b/apps/sim/tools/github/delete_release.ts index e0d79204a84..e4b6c9daa12 100644 --- a/apps/sim/tools/github/delete_release.ts +++ b/apps/sim/tools/github/delete_release.ts @@ -1,3 +1,4 @@ +import { githubPathSegment, githubRepoPath } from '@/tools/github/request-path' import type { DeleteReleaseParams, DeleteReleaseResponse } from '@/tools/github/types' import { GITHUB_CREDENTIAL_OAUTH } from '@/tools/shared/saved-credentials' import type { ToolConfig } from '@/tools/types' @@ -39,7 +40,7 @@ export const deleteReleaseTool: ToolConfig - `https://api.github.com/repos/${params.owner}/${params.repo}/releases/${params.release_id}`, + `https://api.github.com/repos/${githubRepoPath(params.owner, params.repo)}/releases/${githubPathSegment(params.release_id, 'release_id')}`, method: 'DELETE', headers: (params) => ({ Accept: 'application/vnd.github+json', diff --git a/apps/sim/tools/github/fork_gist.ts b/apps/sim/tools/github/fork_gist.ts index b0eaaabb852..2b017d03d33 100644 --- a/apps/sim/tools/github/fork_gist.ts +++ b/apps/sim/tools/github/fork_gist.ts @@ -1,3 +1,4 @@ +import { githubPathSegment } from '@/tools/github/request-path' import { GITHUB_CREDENTIAL_OAUTH } from '@/tools/shared/saved-credentials' import type { ToolConfig } from '@/tools/types' @@ -46,7 +47,8 @@ export const forkGistTool: ToolConfig = { }, request: { - url: (params) => `https://api.github.com/gists/${params.gist_id?.trim()}/forks`, + url: (params) => + `https://api.github.com/gists/${githubPathSegment(params.gist_id, 'gist_id')}/forks`, method: 'POST', headers: (params) => ({ Accept: 'application/vnd.github.v3+json', diff --git a/apps/sim/tools/github/fork_repo.ts b/apps/sim/tools/github/fork_repo.ts index 7faf24610a6..8386a61d419 100644 --- a/apps/sim/tools/github/fork_repo.ts +++ b/apps/sim/tools/github/fork_repo.ts @@ -1,3 +1,4 @@ +import { githubRepoPath } from '@/tools/github/request-path' import { PARENT_OWNER_OUTPUT_PROPERTIES, PARENT_REPO_OUTPUT_PROPERTIES, @@ -83,7 +84,8 @@ export const forkRepoTool: ToolConfig = { }, request: { - url: (params) => `https://api.github.com/repos/${params.owner}/${params.repo}/forks`, + url: (params) => + `https://api.github.com/repos/${githubRepoPath(params.owner, params.repo)}/forks`, method: 'POST', headers: (params) => ({ Accept: 'application/vnd.github.v3+json', diff --git a/apps/sim/tools/github/get_branch.ts b/apps/sim/tools/github/get_branch.ts index a7b567c3da7..c8311044378 100644 --- a/apps/sim/tools/github/get_branch.ts +++ b/apps/sim/tools/github/get_branch.ts @@ -1,3 +1,4 @@ +import { githubRefPath, githubRepoPath } from '@/tools/github/request-path' import type { BranchResponse, GetBranchParams } from '@/tools/github/types' import { BRANCH_OUTPUT_PROPERTIES } from '@/tools/github/types' import { GITHUB_CREDENTIAL_OAUTH } from '@/tools/shared/saved-credentials' @@ -40,7 +41,7 @@ export const getBranchTool: ToolConfig = { request: { url: (params) => - `https://api.github.com/repos/${params.owner}/${params.repo}/branches/${params.branch}`, + `https://api.github.com/repos/${githubRepoPath(params.owner, params.repo)}/branches/${githubRefPath(params.branch, 'branch')}`, method: 'GET', headers: (params) => ({ Accept: 'application/vnd.github+json', diff --git a/apps/sim/tools/github/get_branch_protection.ts b/apps/sim/tools/github/get_branch_protection.ts index a04c7958760..3f244f0cebc 100644 --- a/apps/sim/tools/github/get_branch_protection.ts +++ b/apps/sim/tools/github/get_branch_protection.ts @@ -1,3 +1,4 @@ +import { githubRefPath, githubRepoPath } from '@/tools/github/request-path' import type { BranchProtectionResponse, GetBranchProtectionParams } from '@/tools/github/types' import { BRANCH_PROTECTION_OUTPUT_PROPERTIES } from '@/tools/github/types' import { GITHUB_CREDENTIAL_OAUTH } from '@/tools/shared/saved-credentials' @@ -43,7 +44,7 @@ export const getBranchProtectionTool: ToolConfig< request: { url: (params) => - `https://api.github.com/repos/${params.owner}/${params.repo}/branches/${params.branch}/protection`, + `https://api.github.com/repos/${githubRepoPath(params.owner, params.repo)}/branches/${githubRefPath(params.branch, 'branch')}/protection`, method: 'GET', headers: (params) => ({ Accept: 'application/vnd.github+json', diff --git a/apps/sim/tools/github/get_commit.ts b/apps/sim/tools/github/get_commit.ts index ac2b64bb10c..08d1aa4e444 100644 --- a/apps/sim/tools/github/get_commit.ts +++ b/apps/sim/tools/github/get_commit.ts @@ -1,3 +1,4 @@ +import { githubRefPath, githubRepoPath } from '@/tools/github/request-path' import { COMMIT_DATA_OUTPUT, COMMIT_FILE_OUTPUT_PROPERTIES, @@ -77,7 +78,7 @@ export const getCommitTool: ToolConfig = { request: { url: (params) => - `https://api.github.com/repos/${params.owner}/${params.repo}/commits/${params.ref}`, + `https://api.github.com/repos/${githubRepoPath(params.owner, params.repo)}/commits/${githubRefPath(params.ref, 'ref')}`, method: 'GET', headers: (params) => ({ Accept: 'application/vnd.github.v3+json', diff --git a/apps/sim/tools/github/get_file_content.ts b/apps/sim/tools/github/get_file_content.ts index 03801c2dcf4..23cf0dd5eee 100644 --- a/apps/sim/tools/github/get_file_content.ts +++ b/apps/sim/tools/github/get_file_content.ts @@ -1,6 +1,7 @@ import { truncate } from '@sim/utils/string' import { getFileExtension, getMimeTypeFromExtension } from '@/lib/uploads/utils/file' import { encodeContentPath } from '@/tools/github/content-path' +import { githubRepoPath } from '@/tools/github/request-path' import type { FileContentResponse, GetFileContentParams } from '@/tools/github/types' import { GITHUB_CREDENTIAL_OAUTH } from '@/tools/shared/saved-credentials' import type { ToolConfig } from '@/tools/types' @@ -48,7 +49,7 @@ export const getFileContentTool: ToolConfig { - const baseUrl = `https://api.github.com/repos/${params.owner}/${params.repo}/contents/${encodeContentPath(params.path)}` + const baseUrl = `https://api.github.com/repos/${githubRepoPath(params.owner, params.repo)}/contents/${encodeContentPath(params.path)}` return params.ref ? `${baseUrl}?ref=${encodeURIComponent(params.ref)}` : baseUrl }, method: 'GET', diff --git a/apps/sim/tools/github/get_gist.ts b/apps/sim/tools/github/get_gist.ts index 85ebf41d899..25c156fe2be 100644 --- a/apps/sim/tools/github/get_gist.ts +++ b/apps/sim/tools/github/get_gist.ts @@ -1,3 +1,4 @@ +import { githubPathSegment } from '@/tools/github/request-path' import { GIST_FILES_OUTPUT, GIST_OUTPUT_PROPERTIES, GIST_OWNER_OUTPUT } from '@/tools/github/types' import { GITHUB_CREDENTIAL_OAUTH } from '@/tools/shared/saved-credentials' import type { ToolConfig } from '@/tools/types' @@ -55,7 +56,7 @@ export const getGistTool: ToolConfig = { }, request: { - url: (params) => `https://api.github.com/gists/${params.gist_id?.trim()}`, + url: (params) => `https://api.github.com/gists/${githubPathSegment(params.gist_id, 'gist_id')}`, method: 'GET', headers: (params) => ({ Accept: 'application/vnd.github.v3+json', diff --git a/apps/sim/tools/github/get_issue.ts b/apps/sim/tools/github/get_issue.ts index d3c247e593b..a046b266c93 100644 --- a/apps/sim/tools/github/get_issue.ts +++ b/apps/sim/tools/github/get_issue.ts @@ -1,3 +1,4 @@ +import { githubPathSegment, githubRepoPath } from '@/tools/github/request-path' import type { GetIssueParams, IssueResponse } from '@/tools/github/types' import { ISSUE_OUTPUT_PROPERTIES, @@ -44,7 +45,7 @@ export const getIssueTool: ToolConfig = { request: { url: (params) => - `https://api.github.com/repos/${params.owner}/${params.repo}/issues/${params.issue_number}`, + `https://api.github.com/repos/${githubRepoPath(params.owner, params.repo)}/issues/${githubPathSegment(params.issue_number, 'issue_number')}`, method: 'GET', headers: (params) => ({ Accept: 'application/vnd.github.v3+json', diff --git a/apps/sim/tools/github/get_latest_release.ts b/apps/sim/tools/github/get_latest_release.ts index 2d4426c0272..edfe2bb8775 100644 --- a/apps/sim/tools/github/get_latest_release.ts +++ b/apps/sim/tools/github/get_latest_release.ts @@ -1,3 +1,4 @@ +import { githubRepoPath } from '@/tools/github/request-path' import type { GetLatestReleaseParams, ReleaseResponse } from '@/tools/github/types' import { RELEASE_ASSET_OUTPUT_PROPERTIES, @@ -37,7 +38,8 @@ export const getLatestReleaseTool: ToolConfig `https://api.github.com/repos/${params.owner}/${params.repo}/releases/latest`, + url: (params) => + `https://api.github.com/repos/${githubRepoPath(params.owner, params.repo)}/releases/latest`, method: 'GET', headers: (params) => ({ Accept: 'application/vnd.github+json', diff --git a/apps/sim/tools/github/get_milestone.ts b/apps/sim/tools/github/get_milestone.ts index 5db6d1c7e96..d18c49731b5 100644 --- a/apps/sim/tools/github/get_milestone.ts +++ b/apps/sim/tools/github/get_milestone.ts @@ -1,3 +1,4 @@ +import { githubPathSegment, githubRepoPath } from '@/tools/github/request-path' import { MILESTONE_CREATOR_OUTPUT, MILESTONE_V2_OUTPUT_PROPERTIES } from '@/tools/github/types' import { GITHUB_CREDENTIAL_OAUTH } from '@/tools/shared/saved-credentials' import type { ToolConfig } from '@/tools/types' @@ -66,7 +67,7 @@ export const getMilestoneTool: ToolConfig - `https://api.github.com/repos/${params.owner}/${params.repo}/milestones/${params.milestone_number}`, + `https://api.github.com/repos/${githubRepoPath(params.owner, params.repo)}/milestones/${githubPathSegment(params.milestone_number, 'milestone_number')}`, method: 'GET', headers: (params) => ({ Accept: 'application/vnd.github.v3+json', diff --git a/apps/sim/tools/github/get_pr_files.ts b/apps/sim/tools/github/get_pr_files.ts index 6b9e71ec8b3..9aa18b9e14c 100644 --- a/apps/sim/tools/github/get_pr_files.ts +++ b/apps/sim/tools/github/get_pr_files.ts @@ -1,3 +1,4 @@ +import { githubPathSegment, githubRepoPath } from '@/tools/github/request-path' import type { GetPRFilesParams, PRFilesListResponse } from '@/tools/github/types' import { PR_FILE_OUTPUT_PROPERTIES } from '@/tools/github/types' import { GITHUB_CREDENTIAL_OAUTH } from '@/tools/shared/saved-credentials' @@ -54,7 +55,7 @@ export const getPRFilesTool: ToolConfig = request: { url: (params) => { const url = new URL( - `https://api.github.com/repos/${params.owner}/${params.repo}/pulls/${params.pullNumber}/files` + `https://api.github.com/repos/${githubRepoPath(params.owner, params.repo)}/pulls/${githubPathSegment(params.pullNumber, 'pullNumber')}/files` ) if (params.per_page) url.searchParams.append('per_page', Number(params.per_page).toString()) if (params.page) url.searchParams.append('page', Number(params.page).toString()) diff --git a/apps/sim/tools/github/get_readme.ts b/apps/sim/tools/github/get_readme.ts index ea2f4d5b641..5690e7b6f67 100644 --- a/apps/sim/tools/github/get_readme.ts +++ b/apps/sim/tools/github/get_readme.ts @@ -1,3 +1,4 @@ +import { githubRepoPath } from '@/tools/github/request-path' import type { GetReadmeParams, ReadmeResponse } from '@/tools/github/types' import { GITHUB_CREDENTIAL_OAUTH } from '@/tools/shared/saved-credentials' import type { ToolConfig } from '@/tools/types' @@ -40,7 +41,7 @@ export const getReadmeTool: ToolConfig = { request: { url: (params) => { - const baseUrl = `https://api.github.com/repos/${params.owner}/${params.repo}/readme` + const baseUrl = `https://api.github.com/repos/${githubRepoPath(params.owner, params.repo)}/readme` return params.ref ? `${baseUrl}?ref=${encodeURIComponent(params.ref)}` : baseUrl }, method: 'GET', diff --git a/apps/sim/tools/github/get_release.ts b/apps/sim/tools/github/get_release.ts index c27be05d1a2..56cad68d05d 100644 --- a/apps/sim/tools/github/get_release.ts +++ b/apps/sim/tools/github/get_release.ts @@ -1,3 +1,4 @@ +import { githubPathSegment, githubRepoPath } from '@/tools/github/request-path' import type { GetReleaseParams, ReleaseResponse } from '@/tools/github/types' import { RELEASE_ASSET_OUTPUT_PROPERTIES, @@ -44,7 +45,7 @@ export const getReleaseTool: ToolConfig = { request: { url: (params) => - `https://api.github.com/repos/${params.owner}/${params.repo}/releases/${params.release_id}`, + `https://api.github.com/repos/${githubRepoPath(params.owner, params.repo)}/releases/${githubPathSegment(params.release_id, 'release_id')}`, method: 'GET', headers: (params) => ({ Accept: 'application/vnd.github+json', diff --git a/apps/sim/tools/github/get_tree.ts b/apps/sim/tools/github/get_tree.ts index 67243417db2..4b8a04e283b 100644 --- a/apps/sim/tools/github/get_tree.ts +++ b/apps/sim/tools/github/get_tree.ts @@ -1,4 +1,5 @@ import { encodeContentPath } from '@/tools/github/content-path' +import { githubRepoPath } from '@/tools/github/request-path' import type { GetTreeParams, TreeResponse } from '@/tools/github/types' import { GITHUB_CREDENTIAL_OAUTH } from '@/tools/shared/saved-credentials' import type { ToolConfig } from '@/tools/types' @@ -48,7 +49,7 @@ export const getTreeTool: ToolConfig = { request: { url: (params) => { const path = params.path || '' - const baseUrl = `https://api.github.com/repos/${params.owner}/${params.repo}/contents/${encodeContentPath(path)}` + const baseUrl = `https://api.github.com/repos/${githubRepoPath(params.owner, params.repo)}/contents/${encodeContentPath(path)}` return params.ref ? `${baseUrl}?ref=${encodeURIComponent(params.ref)}` : baseUrl }, method: 'GET', diff --git a/apps/sim/tools/github/get_workflow.ts b/apps/sim/tools/github/get_workflow.ts index 4c94729bc53..7f1e32cc172 100644 --- a/apps/sim/tools/github/get_workflow.ts +++ b/apps/sim/tools/github/get_workflow.ts @@ -1,3 +1,4 @@ +import { githubPathSegment, githubRepoPath } from '@/tools/github/request-path' import type { GetWorkflowParams, WorkflowResponse } from '@/tools/github/types' import { WORKFLOW_OUTPUT_PROPERTIES } from '@/tools/github/types' import { GITHUB_CREDENTIAL_OAUTH } from '@/tools/shared/saved-credentials' @@ -40,7 +41,7 @@ export const getWorkflowTool: ToolConfig = request: { url: (params) => - `https://api.github.com/repos/${params.owner}/${params.repo}/actions/workflows/${params.workflow_id}`, + `https://api.github.com/repos/${githubRepoPath(params.owner, params.repo)}/actions/workflows/${githubPathSegment(params.workflow_id, 'workflow_id')}`, method: 'GET', headers: (params) => ({ Accept: 'application/vnd.github+json', diff --git a/apps/sim/tools/github/get_workflow_run.ts b/apps/sim/tools/github/get_workflow_run.ts index 8e3b72adff2..987eb172035 100644 --- a/apps/sim/tools/github/get_workflow_run.ts +++ b/apps/sim/tools/github/get_workflow_run.ts @@ -1,3 +1,4 @@ +import { githubPathSegment, githubRepoPath } from '@/tools/github/request-path' import type { GetWorkflowRunParams, WorkflowRunResponse } from '@/tools/github/types' import { HEAD_COMMIT_OUTPUT, @@ -46,7 +47,7 @@ export const getWorkflowRunTool: ToolConfig - `https://api.github.com/repos/${params.owner}/${params.repo}/actions/runs/${params.run_id}`, + `https://api.github.com/repos/${githubRepoPath(params.owner, params.repo)}/actions/runs/${githubPathSegment(params.run_id, 'run_id')}`, method: 'GET', headers: (params) => ({ Accept: 'application/vnd.github+json', diff --git a/apps/sim/tools/github/issue_comment.ts b/apps/sim/tools/github/issue_comment.ts index 40f42cf078a..578da370fb4 100644 --- a/apps/sim/tools/github/issue_comment.ts +++ b/apps/sim/tools/github/issue_comment.ts @@ -1,4 +1,5 @@ import { truncate } from '@sim/utils/string' +import { githubPathSegment, githubRepoPath } from '@/tools/github/request-path' import type { CreateIssueCommentParams, IssueCommentResponse } from '@/tools/github/types' import { COMMENT_OUTPUT_PROPERTIES, USER_OUTPUT } from '@/tools/github/types' import { GITHUB_CREDENTIAL_OAUTH } from '@/tools/shared/saved-credentials' @@ -46,7 +47,7 @@ export const issueCommentTool: ToolConfig - `https://api.github.com/repos/${params.owner}/${params.repo}/issues/${params.issue_number}/comments`, + `https://api.github.com/repos/${githubRepoPath(params.owner, params.repo)}/issues/${githubPathSegment(params.issue_number, 'issue_number')}/comments`, method: 'POST', headers: (params) => ({ Accept: 'application/vnd.github+json', diff --git a/apps/sim/tools/github/job_logs.test.ts b/apps/sim/tools/github/job_logs.test.ts index 2b3e92c83b8..b566ca88b3f 100644 --- a/apps/sim/tools/github/job_logs.test.ts +++ b/apps/sim/tools/github/job_logs.test.ts @@ -31,15 +31,14 @@ describe('github_job_logs', () => { expect(url).toBe('https://api.github.com/repos/octo/demo/actions/jobs/42/logs') }) - it('escapes coordinates so they cannot redirect the authenticated request', () => { - const url = (jobLogsTool.request.url as (params: JobLogsParams) => string)({ - ...BASE_PARAMS, - owner: '../../orgs/secret', - repo: 'demo?ref=x', - }) + it('refuses coordinates that would redirect the authenticated request', () => { + const url = jobLogsTool.request.url as (params: JobLogsParams) => string - expect(url).toBe( - 'https://api.github.com/repos/..%2F..%2Forgs%2Fsecret/demo%3Fref%3Dx/actions/jobs/42/logs' + expect(() => url({ ...BASE_PARAMS, owner: '../../orgs/secret' })).toThrow( + /owner cannot contain a path separator/ + ) + expect(url({ ...BASE_PARAMS, repo: 'demo?ref=x' })).toBe( + 'https://api.github.com/repos/octo/demo%3Fref%3Dx/actions/jobs/42/logs' ) }) diff --git a/apps/sim/tools/github/job_logs.ts b/apps/sim/tools/github/job_logs.ts index 845e954dff5..c54002a72d0 100644 --- a/apps/sim/tools/github/job_logs.ts +++ b/apps/sim/tools/github/job_logs.ts @@ -1,3 +1,4 @@ +import { githubRepoPath } from '@/tools/github/request-path' import type { JobLogsParams, JobLogsResponse } from '@/tools/github/types' import { GITHUB_CREDENTIAL_OAUTH } from '@/tools/shared/saved-credentials' import type { ToolConfig } from '@/tools/types' @@ -26,7 +27,7 @@ function jobLogsPath(owner: string, repo: string, jobId: number): string { if (!Number.isSafeInteger(jobId) || jobId < 1) { throw new Error('job_id must be a positive integer') } - return `${encodeURIComponent(owner)}/${encodeURIComponent(repo)}/actions/jobs/${jobId}/logs` + return `${githubRepoPath(owner, repo)}/actions/jobs/${jobId}/logs` } /** Byte offsets from a `Content-Range: bytes -/` header. */ diff --git a/apps/sim/tools/github/list_branches.ts b/apps/sim/tools/github/list_branches.ts index f97ddeeaef9..77092b0ff42 100644 --- a/apps/sim/tools/github/list_branches.ts +++ b/apps/sim/tools/github/list_branches.ts @@ -1,3 +1,4 @@ +import { githubRepoPath } from '@/tools/github/request-path' import type { BranchListResponse, ListBranchesParams } from '@/tools/github/types' import { BRANCH_OUTPUT_PROPERTIES } from '@/tools/github/types' import { GITHUB_CREDENTIAL_OAUTH } from '@/tools/shared/saved-credentials' @@ -52,7 +53,7 @@ export const listBranchesTool: ToolConfig { - const baseUrl = `https://api.github.com/repos/${params.owner}/${params.repo}/branches` + const baseUrl = `https://api.github.com/repos/${githubRepoPath(params.owner, params.repo)}/branches` const queryParams = new URLSearchParams() if (params.protected !== undefined) { diff --git a/apps/sim/tools/github/list_commits.ts b/apps/sim/tools/github/list_commits.ts index f6957d2f289..843895e6da7 100644 --- a/apps/sim/tools/github/list_commits.ts +++ b/apps/sim/tools/github/list_commits.ts @@ -1,3 +1,4 @@ +import { githubRepoPath } from '@/tools/github/request-path' import { COMMIT_DATA_OUTPUT, COMMIT_PARENT_OUTPUT_PROPERTIES, @@ -119,7 +120,9 @@ export const listCommitsTool: ToolConfig request: { url: (params) => { - const url = new URL(`https://api.github.com/repos/${params.owner}/${params.repo}/commits`) + const url = new URL( + `https://api.github.com/repos/${githubRepoPath(params.owner, params.repo)}/commits` + ) if (params.sha) url.searchParams.append('sha', params.sha) if (params.path) url.searchParams.append('path', params.path) if (params.author) url.searchParams.append('author', params.author) diff --git a/apps/sim/tools/github/list_forks.ts b/apps/sim/tools/github/list_forks.ts index d019a7bba07..a2e902bbac4 100644 --- a/apps/sim/tools/github/list_forks.ts +++ b/apps/sim/tools/github/list_forks.ts @@ -1,3 +1,4 @@ +import { githubRepoPath } from '@/tools/github/request-path' import { REPO_FULL_OUTPUT_PROPERTIES, USER_FULL_OUTPUT_PROPERTIES } from '@/tools/github/types' import { GITHUB_CREDENTIAL_OAUTH } from '@/tools/shared/saved-credentials' import type { ToolConfig } from '@/tools/types' @@ -83,7 +84,9 @@ export const listForksTool: ToolConfig = { request: { url: (params) => { - const url = new URL(`https://api.github.com/repos/${params.owner}/${params.repo}/forks`) + const url = new URL( + `https://api.github.com/repos/${githubRepoPath(params.owner, params.repo)}/forks` + ) if (params.sort) url.searchParams.append('sort', params.sort) if (params.per_page) url.searchParams.append('per_page', String(params.per_page)) if (params.page) url.searchParams.append('page', String(params.page)) diff --git a/apps/sim/tools/github/list_gists.ts b/apps/sim/tools/github/list_gists.ts index 5b33b8481e0..1132687ce23 100644 --- a/apps/sim/tools/github/list_gists.ts +++ b/apps/sim/tools/github/list_gists.ts @@ -1,3 +1,4 @@ +import { githubPathSegment } from '@/tools/github/request-path' import { GIST_FILES_OUTPUT, GIST_OUTPUT_PROPERTIES, GIST_OWNER_OUTPUT } from '@/tools/github/types' import { GITHUB_CREDENTIAL_OAUTH } from '@/tools/shared/saved-credentials' import type { ToolConfig } from '@/tools/types' @@ -76,7 +77,7 @@ export const listGistsTool: ToolConfig = { request: { url: (params) => { const baseUrl = params.username - ? `https://api.github.com/users/${params.username}/gists` + ? `https://api.github.com/users/${githubPathSegment(params.username, 'username')}/gists` : 'https://api.github.com/gists' const url = new URL(baseUrl) if (params.since) url.searchParams.append('since', params.since) diff --git a/apps/sim/tools/github/list_issue_comments.ts b/apps/sim/tools/github/list_issue_comments.ts index a04451e4795..1e65cdc4c5e 100644 --- a/apps/sim/tools/github/list_issue_comments.ts +++ b/apps/sim/tools/github/list_issue_comments.ts @@ -1,4 +1,5 @@ import { truncate } from '@sim/utils/string' +import { githubPathSegment, githubRepoPath } from '@/tools/github/request-path' import type { CommentsListResponse, ListIssueCommentsParams } from '@/tools/github/types' import { COMMENT_OUTPUT_PROPERTIES, USER_OUTPUT } from '@/tools/github/types' import { GITHUB_CREDENTIAL_OAUTH } from '@/tools/shared/saved-credentials' @@ -60,7 +61,7 @@ export const listIssueCommentsTool: ToolConfig { - const baseUrl = `https://api.github.com/repos/${params.owner}/${params.repo}/issues/${params.issue_number}/comments` + const baseUrl = `https://api.github.com/repos/${githubRepoPath(params.owner, params.repo)}/issues/${githubPathSegment(params.issue_number, 'issue_number')}/comments` const queryParams = new URLSearchParams() if (params.since) queryParams.append('since', params.since) diff --git a/apps/sim/tools/github/list_issues.ts b/apps/sim/tools/github/list_issues.ts index 0b92511db61..0407134f42d 100644 --- a/apps/sim/tools/github/list_issues.ts +++ b/apps/sim/tools/github/list_issues.ts @@ -1,3 +1,4 @@ +import { githubRepoPath } from '@/tools/github/request-path' import type { IssuesListResponse, ListIssuesParams } from '@/tools/github/types' import { ISSUE_OUTPUT_PROPERTIES, @@ -92,7 +93,9 @@ export const listIssuesTool: ToolConfig = request: { url: (params) => { - const url = new URL(`https://api.github.com/repos/${params.owner}/${params.repo}/issues`) + const url = new URL( + `https://api.github.com/repos/${githubRepoPath(params.owner, params.repo)}/issues` + ) if (params.state) url.searchParams.append('state', params.state) if (params.assignee) url.searchParams.append('assignee', params.assignee) if (params.creator) url.searchParams.append('creator', params.creator) diff --git a/apps/sim/tools/github/list_milestones.ts b/apps/sim/tools/github/list_milestones.ts index 61b9d9a866e..73f9608dd53 100644 --- a/apps/sim/tools/github/list_milestones.ts +++ b/apps/sim/tools/github/list_milestones.ts @@ -1,3 +1,4 @@ +import { githubRepoPath } from '@/tools/github/request-path' import { MILESTONE_CREATOR_OUTPUT, MILESTONE_V2_OUTPUT_PROPERTIES } from '@/tools/github/types' import { GITHUB_CREDENTIAL_OAUTH } from '@/tools/shared/saved-credentials' import type { ToolConfig } from '@/tools/types' @@ -98,7 +99,9 @@ export const listMilestonesTool: ToolConfig { - const url = new URL(`https://api.github.com/repos/${params.owner}/${params.repo}/milestones`) + const url = new URL( + `https://api.github.com/repos/${githubRepoPath(params.owner, params.repo)}/milestones` + ) if (params.state) url.searchParams.append('state', params.state) if (params.sort) url.searchParams.append('sort', params.sort) if (params.direction) url.searchParams.append('direction', params.direction) diff --git a/apps/sim/tools/github/list_pr_comments.ts b/apps/sim/tools/github/list_pr_comments.ts index 02762af3a08..9ed5ebfb06a 100644 --- a/apps/sim/tools/github/list_pr_comments.ts +++ b/apps/sim/tools/github/list_pr_comments.ts @@ -1,4 +1,5 @@ import { truncate } from '@sim/utils/string' +import { githubPathSegment, githubRepoPath } from '@/tools/github/request-path' import type { CommentsListResponse, ListPRCommentsParams } from '@/tools/github/types' import { PR_COMMENT_OUTPUT_PROPERTIES, USER_OUTPUT } from '@/tools/github/types' import { GITHUB_CREDENTIAL_OAUTH } from '@/tools/shared/saved-credentials' @@ -74,7 +75,7 @@ export const listPRCommentsTool: ToolConfig { - const baseUrl = `https://api.github.com/repos/${params.owner}/${params.repo}/pulls/${params.pullNumber}/comments` + const baseUrl = `https://api.github.com/repos/${githubRepoPath(params.owner, params.repo)}/pulls/${githubPathSegment(params.pullNumber, 'pullNumber')}/comments` const queryParams = new URLSearchParams() if (params.sort) queryParams.append('sort', params.sort) diff --git a/apps/sim/tools/github/list_prs.ts b/apps/sim/tools/github/list_prs.ts index 7ff96cbcb98..1802ca826a1 100644 --- a/apps/sim/tools/github/list_prs.ts +++ b/apps/sim/tools/github/list_prs.ts @@ -1,3 +1,4 @@ +import { githubRepoPath } from '@/tools/github/request-path' import type { ListPRsParams, PRListResponse } from '@/tools/github/types' import { BRANCH_REF_OUTPUT, PR_SUMMARY_OUTPUT_PROPERTIES, USER_OUTPUT } from '@/tools/github/types' import { GITHUB_CREDENTIAL_OAUTH } from '@/tools/shared/saved-credentials' @@ -81,7 +82,9 @@ export const listPRsTool: ToolConfig = { request: { url: (params) => { - const url = new URL(`https://api.github.com/repos/${params.owner}/${params.repo}/pulls`) + const url = new URL( + `https://api.github.com/repos/${githubRepoPath(params.owner, params.repo)}/pulls` + ) if (params.state) url.searchParams.append('state', params.state) if (params.head) url.searchParams.append('head', params.head) if (params.base) url.searchParams.append('base', params.base) diff --git a/apps/sim/tools/github/list_releases.ts b/apps/sim/tools/github/list_releases.ts index 24b84415182..d7230247347 100644 --- a/apps/sim/tools/github/list_releases.ts +++ b/apps/sim/tools/github/list_releases.ts @@ -1,3 +1,4 @@ +import { githubRepoPath } from '@/tools/github/request-path' import type { ListReleasesParams, ListReleasesResponse } from '@/tools/github/types' import { RELEASE_ASSET_OUTPUT_PROPERTIES, @@ -52,7 +53,9 @@ export const listReleasesTool: ToolConfig { - const url = new URL(`https://api.github.com/repos/${params.owner}/${params.repo}/releases`) + const url = new URL( + `https://api.github.com/repos/${githubRepoPath(params.owner, params.repo)}/releases` + ) if (params.per_page) { url.searchParams.append('per_page', Number(params.per_page).toString()) } diff --git a/apps/sim/tools/github/list_stargazers.ts b/apps/sim/tools/github/list_stargazers.ts index 1e7bd09f54d..f233b48320d 100644 --- a/apps/sim/tools/github/list_stargazers.ts +++ b/apps/sim/tools/github/list_stargazers.ts @@ -1,3 +1,4 @@ +import { githubRepoPath } from '@/tools/github/request-path' import { USER_FULL_OUTPUT_PROPERTIES } from '@/tools/github/types' import { GITHUB_CREDENTIAL_OAUTH } from '@/tools/shared/saved-credentials' import type { ToolConfig } from '@/tools/types' @@ -71,7 +72,9 @@ export const listStargazersTool: ToolConfig { - const url = new URL(`https://api.github.com/repos/${params.owner}/${params.repo}/stargazers`) + const url = new URL( + `https://api.github.com/repos/${githubRepoPath(params.owner, params.repo)}/stargazers` + ) if (params.per_page) url.searchParams.append('per_page', String(params.per_page)) if (params.page) url.searchParams.append('page', String(params.page)) return url.toString() diff --git a/apps/sim/tools/github/list_tags.ts b/apps/sim/tools/github/list_tags.ts index 3cdb1b0f29c..19b2ea78d35 100644 --- a/apps/sim/tools/github/list_tags.ts +++ b/apps/sim/tools/github/list_tags.ts @@ -1,3 +1,4 @@ +import { githubRepoPath } from '@/tools/github/request-path' import type { ListTagsParams, TagsListResponse } from '@/tools/github/types' import { GITHUB_CREDENTIAL_OAUTH } from '@/tools/shared/saved-credentials' import type { ToolConfig } from '@/tools/types' @@ -47,7 +48,9 @@ export const listTagsTool: ToolConfig = { request: { url: (params) => { - const url = new URL(`https://api.github.com/repos/${params.owner}/${params.repo}/tags`) + const url = new URL( + `https://api.github.com/repos/${githubRepoPath(params.owner, params.repo)}/tags` + ) if (params.per_page) { url.searchParams.append('per_page', Number(params.per_page).toString()) } diff --git a/apps/sim/tools/github/list_workflow_runs.ts b/apps/sim/tools/github/list_workflow_runs.ts index e61dd01d79f..4c48b9ef6d3 100644 --- a/apps/sim/tools/github/list_workflow_runs.ts +++ b/apps/sim/tools/github/list_workflow_runs.ts @@ -1,3 +1,4 @@ +import { githubRepoPath } from '@/tools/github/request-path' import type { ListWorkflowRunsParams, ListWorkflowRunsResponse } from '@/tools/github/types' import { HEAD_COMMIT_OUTPUT, @@ -8,6 +9,7 @@ import { } from '@/tools/github/types' import { GITHUB_CREDENTIAL_OAUTH } from '@/tools/shared/saved-credentials' import type { ToolConfig } from '@/tools/types' +import { safeUrlPathSegment } from '@/tools/url-path' export const listWorkflowRunsTool: ToolConfig = { id: 'github_list_workflow_runs', @@ -95,11 +97,11 @@ export const listWorkflowRunsTool: ToolConfig { const url = new URL( - `https://api.github.com/repos/${params.owner}/${params.repo}/actions/workflows` + `https://api.github.com/repos/${githubRepoPath(params.owner, params.repo)}/actions/workflows` ) if (params.per_page) { url.searchParams.append('per_page', Number(params.per_page).toString()) diff --git a/apps/sim/tools/github/merge_pr.ts b/apps/sim/tools/github/merge_pr.ts index c4d0ba8bfc9..9b7b0d70c9d 100644 --- a/apps/sim/tools/github/merge_pr.ts +++ b/apps/sim/tools/github/merge_pr.ts @@ -1,3 +1,4 @@ +import { githubPathSegment, githubRepoPath } from '@/tools/github/request-path' import type { MergePRParams, MergeResultResponse } from '@/tools/github/types' import { GITHUB_CREDENTIAL_OAUTH } from '@/tools/shared/saved-credentials' import type { ToolConfig } from '@/tools/types' @@ -57,7 +58,7 @@ export const mergePRTool: ToolConfig = { request: { url: (params) => - `https://api.github.com/repos/${params.owner}/${params.repo}/pulls/${params.pullNumber}/merge`, + `https://api.github.com/repos/${githubRepoPath(params.owner, params.repo)}/pulls/${githubPathSegment(params.pullNumber, 'pullNumber')}/merge`, method: 'PUT', headers: (params) => ({ Accept: 'application/vnd.github.v3+json', diff --git a/apps/sim/tools/github/pr.ts b/apps/sim/tools/github/pr.ts index feea5d9d580..777e6bfce7f 100644 --- a/apps/sim/tools/github/pr.ts +++ b/apps/sim/tools/github/pr.ts @@ -3,6 +3,7 @@ import { outboundFetch } from '@/lib/core/network/outbound-fetch.server' import { readResponseTextWithLimit } from '@/lib/core/utils/stream-limits' import { MAX_TOOL_RESPONSE_BODY_BYTES } from '@/lib/internal/tool-operations/response-limits' import { MAX_BUFFERED_TRANSFER_BYTES } from '@/lib/uploads/shared/types' +import { githubPathSegment, githubRepoPath } from '@/tools/github/request-path' import { nullableBoolean, nullableString, @@ -165,7 +166,7 @@ async function fetchPullRequestFiles( for (let page = 1; page <= maxPages; page += 1) { signal?.throwIfAborted() const response = await outboundFetch( - `https://api.github.com/repos/${params.owner}/${params.repo}/pulls/${pullNumber}/files?per_page=${PULL_REQUEST_FILES_PER_PAGE}&page=${page}`, + `https://api.github.com/repos/${githubRepoPath(params.owner, params.repo)}/pulls/${pullNumber}/files?per_page=${PULL_REQUEST_FILES_PER_PAGE}&page=${page}`, { headers: { Accept: 'application/vnd.github+json', @@ -248,7 +249,7 @@ export const prTool: ToolConfig = { request: { url: (params) => - `https://api.github.com/repos/${params.owner}/${params.repo}/pulls/${params.pullNumber}`, + `https://api.github.com/repos/${githubRepoPath(params.owner, params.repo)}/pulls/${githubPathSegment(params.pullNumber, 'pullNumber')}`, method: 'GET', headers: (params) => ({ Accept: 'application/vnd.github.v3+json', diff --git a/apps/sim/tools/github/remove_label.ts b/apps/sim/tools/github/remove_label.ts index 2b915078ee5..4fd0a88d45d 100644 --- a/apps/sim/tools/github/remove_label.ts +++ b/apps/sim/tools/github/remove_label.ts @@ -1,3 +1,4 @@ +import { githubPathSegment, githubRepoPath } from '@/tools/github/request-path' import type { LabelsResponse, RemoveLabelParams } from '@/tools/github/types' import { GITHUB_CREDENTIAL_OAUTH } from '@/tools/shared/saved-credentials' import type { ToolConfig } from '@/tools/types' @@ -44,7 +45,7 @@ export const removeLabelTool: ToolConfig = { request: { url: (params) => - `https://api.github.com/repos/${params.owner}/${params.repo}/issues/${params.issue_number}/labels/${encodeURIComponent(params.name)}`, + `https://api.github.com/repos/${githubRepoPath(params.owner, params.repo)}/issues/${githubPathSegment(params.issue_number, 'issue_number')}/labels/${encodeURIComponent(params.name)}`, method: 'DELETE', headers: (params) => ({ Accept: 'application/vnd.github.v3+json', diff --git a/apps/sim/tools/github/repo_info.ts b/apps/sim/tools/github/repo_info.ts index b8fbf295295..9cbc6cc4bfc 100644 --- a/apps/sim/tools/github/repo_info.ts +++ b/apps/sim/tools/github/repo_info.ts @@ -1,3 +1,4 @@ +import { githubRepoPath } from '@/tools/github/request-path' import type { BaseGitHubParams, RepoInfoResponse } from '@/tools/github/types' import { LICENSE_OUTPUT, @@ -38,7 +39,7 @@ export const repoInfoTool: ToolConfig = { }, request: { - url: (params) => `https://api.github.com/repos/${params.owner}/${params.repo}`, + url: (params) => `https://api.github.com/repos/${githubRepoPath(params.owner, params.repo)}`, method: 'GET', headers: (params) => ({ Accept: 'application/vnd.github+json', diff --git a/apps/sim/tools/github/request-path.ts b/apps/sim/tools/github/request-path.ts new file mode 100644 index 00000000000..0cedcac241f --- /dev/null +++ b/apps/sim/tools/github/request-path.ts @@ -0,0 +1,73 @@ +import { safeUrlPathSegment } from '@/tools/url-path' + +/** + * Percent-escapes that `encodeURIComponent` emits for characters which cannot + * change a URL's structure and which `fetch` sends raw in a path: RFC 3986 + * `sub-delims`, `:`, `@`, and the `[`, `]` and `|` the WHATWG parser leaves raw. + * Restoring them keeps an identifier that was interpolated raw before byte + * identical: a branch such as `release+1` or a workflow file such as + * `deploy@v2.yml` still produces the same URL. + */ +const RAW_SAFE_PATH_ESCAPES = /%(?:24|26|2B|2C|3A|3B|3D|40|5B|5D|7C)/g + +function restoreRawSafeCharacters(encoded: string): string { + return encoded.replace(RAW_SAFE_PATH_ESCAPES, (sequence) => decodeURIComponent(sequence)) +} + +/** + * Encodes the `{owner}/{repo}` pair of a GitHub REST path. GitHub names only + * use `[A-Za-z0-9-_.]`, so every real name passes through unchanged; a value + * carrying `/`, `?`, `#`, or a dot segment, which would steer the + * authenticated request to a different endpoint, is rejected or escaped. + * + * @throws When either value is empty, a dot segment, or contains a separator. + */ +export function githubRepoPath(owner: string, repo: string): string { + return `${safeUrlPathSegment(owner, 'owner')}/${safeUrlPathSegment(repo, 'repo')}` +} + +/** + * Encodes an identifier that fills exactly one GitHub REST path segment: a + * number (issue, pull, run, milestone, release, comment, reaction), a gist id, + * a username, or a workflow id or file name. + * + * @throws When the value is empty, a dot segment, or contains a separator. + */ +export function githubPathSegment(value: string | number, paramName: string): string { + return restoreRawSafeCharacters(safeUrlPathSegment(value, paramName)) +} + +/** + * Encodes a git ref (a branch name, `heads/`, a tag, or a SHA) for a + * path position that GitHub matches greedily, so its `/` separators are kept. + * Each segment is escaped so a `?` or `#` cannot end the path, and a `.` or + * `..` segment, which the URL parser would resolve away, is rejected. Git + * forbids both in ref names, so no real ref changes. + * + * A numeric ref (an all-digit short SHA or branch sent as a JSON number) is + * stringified like any other numeric segment. + * + * @throws When the ref is empty or contains a dot segment. + */ +export function githubRefPath(ref: string | number, paramName: string): string { + if (typeof ref !== 'string') { + return safeUrlPathSegment(ref, paramName) + } + if (ref.trim() === '') { + throw new Error(`${paramName} is required`) + } + return ref + .trim() + .split('/') + .map((segment) => { + if (segment === '.' || segment === '..') { + throw new Error(`${paramName} cannot contain "." or ".." path segments`) + } + try { + return restoreRawSafeCharacters(encodeURIComponent(segment)) + } catch { + throw new Error(`${paramName} contains an unpaired UTF-16 surrogate and cannot be encoded`) + } + }) + .join('/') +} diff --git a/apps/sim/tools/github/request-paths.test.ts b/apps/sim/tools/github/request-paths.test.ts new file mode 100644 index 00000000000..8e9352a837b --- /dev/null +++ b/apps/sim/tools/github/request-paths.test.ts @@ -0,0 +1,147 @@ +import { describe, expect, it } from 'vitest' +import { executeGitHubTool } from '@/lib/internal/github/execute-tool' +import { getGitHubLatestCommit } from '@/lib/internal/github/operations' +import { compareCommitsTool } from '@/tools/github/compare_commits' +import { deleteBranchTool } from '@/tools/github/delete_branch' +import { getBranchTool } from '@/tools/github/get_branch' +import { getGistTool } from '@/tools/github/get_gist' +import { getIssueTool } from '@/tools/github/get_issue' +import { getWorkflowTool } from '@/tools/github/get_workflow' +import { listGistsTool } from '@/tools/github/list_gists' +import { repoInfoTool } from '@/tools/github/repo_info' +import { starRepoTool } from '@/tools/github/star_repo' +import type { ToolConfig } from '@/tools/types' + +function url(tool: ToolConfig, params: Record): string { + const { url: builder } = tool.request + return typeof builder === 'function' ? builder({ apiKey: 'ghp_test', ...params }) : builder +} + +describe('GitHub request paths', () => { + it.each([ + ['octocat', 'Hello-World'], + ['my-org', 'repo.name_2'], + ['a1', '.github'], + ['user-123', 'x..y'], + ])('leaves real owner %s and repo %s byte identical', (owner, repo) => { + expect(url(repoInfoTool, { owner, repo })).toBe(`https://api.github.com/repos/${owner}/${repo}`) + expect(url(starRepoTool, { owner, repo })).toBe( + `https://api.github.com/user/starred/${owner}/${repo}` + ) + }) + + it.each([ + [{ owner: '..', repo: 'demo' }, /owner cannot be "\.\."/], + [{ owner: 'octo', repo: '.' }, /repo cannot be "\."/], + [{ owner: 'orgs/acme', repo: 'demo' }, /owner cannot contain a path separator/], + [{ owner: 'octo', repo: 'demo\\..' }, /repo cannot contain a path separator/], + ])('rejects an owner or repo that would leave the repository path', (params, error) => { + expect(() => url(repoInfoTool, params)).toThrow(error) + expect(() => url(starRepoTool, params)).toThrow(error) + }) + + it('escapes a query or fragment delimiter instead of letting it end the path', () => { + expect(url(repoInfoTool, { owner: 'octo', repo: 'x?y' })).toBe( + 'https://api.github.com/repos/octo/x%3Fy' + ) + expect(url(repoInfoTool, { owner: 'octo#z', repo: 'demo' })).toBe( + 'https://api.github.com/repos/octo%23z/demo' + ) + }) + + it('rejects a numeric id that smuggles extra segments', () => { + const params = { owner: 'octo', repo: 'demo' } + + expect(url(getIssueTool, { ...params, issue_number: 42 })).toBe( + 'https://api.github.com/repos/octo/demo/issues/42' + ) + expect(() => url(getIssueTool, { ...params, issue_number: '1/../../../user' })).toThrow( + /issue_number cannot contain a path separator/ + ) + }) + + it('keeps workflow file names and gist ids as one segment', () => { + const params = { owner: 'octo', repo: 'demo' } + + expect(url(getWorkflowTool, { ...params, workflow_id: 'deploy@v2+prod.yml' })).toBe( + 'https://api.github.com/repos/octo/demo/actions/workflows/deploy@v2+prod.yml' + ) + expect(() => url(getWorkflowTool, { ...params, workflow_id: '../../hooks' })).toThrow( + /workflow_id cannot contain a path separator/ + ) + expect(url(getGistTool, { gist_id: ' aa5a315d61ae9438b18d ' })).toBe( + 'https://api.github.com/gists/aa5a315d61ae9438b18d' + ) + expect(() => url(getGistTool, { gist_id: '..' })).toThrow(/gist_id cannot be "\.\."/) + expect(() => url(listGistsTool, { username: 'octo/repos' })).toThrow( + /username cannot contain a path separator/ + ) + }) + + it('keeps the slashes and raw-safe characters of a branch byte identical', () => { + const params = { owner: 'octo', repo: 'demo' } + + expect(url(getBranchTool, { ...params, branch: 'feature/login+v2@team' })).toBe( + 'https://api.github.com/repos/octo/demo/branches/feature/login+v2@team' + ) + expect(url(deleteBranchTool, { ...params, branch: 'users/jane/fix' })).toBe( + 'https://api.github.com/repos/octo/demo/git/refs/heads/users/jane/fix' + ) + expect(url(compareCommitsTool, { ...params, base: 'main', head: 'release/1.2' })).toBe( + 'https://api.github.com/repos/octo/demo/compare/main...release/1.2' + ) + }) + + it('accepts a numeric ref or branch sent as a JSON number', () => { + const params = { owner: 'octo', repo: 'demo' } + + expect(url(getBranchTool, { ...params, branch: 2024 })).toBe( + 'https://api.github.com/repos/octo/demo/branches/2024' + ) + expect(url(compareCommitsTool, { ...params, base: 1234567, head: 'main' })).toBe( + 'https://api.github.com/repos/octo/demo/compare/1234567...main' + ) + }) + + it('escapes delimiters in a branch and rejects its dot segments', () => { + const params = { owner: 'octo', repo: 'demo' } + + expect(url(getBranchTool, { ...params, branch: 'a?b#c' })).toBe( + 'https://api.github.com/repos/octo/demo/branches/a%3Fb%23c' + ) + expect(() => url(deleteBranchTool, { ...params, branch: 'x/../../../../hooks' })).toThrow( + /branch cannot contain "\." or "\.\." path segments/ + ) + expect(() => url(compareCommitsTool, { ...params, base: 'main', head: '..' })).toThrow( + /head cannot contain "\." or "\.\." path segments/ + ) + }) + + it('rejects a traversal owner in the latest-commit operation as a 400 before any request', async () => { + await expect( + getGitHubLatestCommit( + { owner: '..', repo: 'demo', apiKey: 'ghp_test' }, + { requestId: 'test-request' } + ) + ).rejects.toMatchObject({ + status: 400, + message: 'owner cannot be ".." (path traversal is not allowed)', + }) + }) + + it('answers a traversal owner in the PR comment operation with a 400', async () => { + const response = await executeGitHubTool({ + toolId: 'github_comment', + input: { owner: 'octo', repo: '..', body: 'hi', pullNumber: 1, apiKey: 'ghp_test' }, + headers: new Headers(), + context: { workflowId: 'workflow-1' }, + requestId: 'test-request', + }) + + expect(response.status).toBe(400) + expect(await response.json()).toEqual({ + success: false, + error: 'repo cannot be ".." (path traversal is not allowed)', + }) + }) +}) diff --git a/apps/sim/tools/github/request_reviewers.ts b/apps/sim/tools/github/request_reviewers.ts index 4e71791393b..cebbbc7cd59 100644 --- a/apps/sim/tools/github/request_reviewers.ts +++ b/apps/sim/tools/github/request_reviewers.ts @@ -1,3 +1,4 @@ +import { githubPathSegment, githubRepoPath } from '@/tools/github/request-path' import type { RequestReviewersParams, ReviewersResponse } from '@/tools/github/types' import { GITHUB_CREDENTIAL_OAUTH } from '@/tools/shared/saved-credentials' import type { ToolConfig } from '@/tools/types' @@ -51,7 +52,7 @@ export const requestReviewersTool: ToolConfig - `https://api.github.com/repos/${params.owner}/${params.repo}/pulls/${params.pullNumber}/requested_reviewers`, + `https://api.github.com/repos/${githubRepoPath(params.owner, params.repo)}/pulls/${githubPathSegment(params.pullNumber, 'pullNumber')}/requested_reviewers`, method: 'POST', headers: (params) => ({ Accept: 'application/vnd.github.v3+json', diff --git a/apps/sim/tools/github/rerun_workflow.ts b/apps/sim/tools/github/rerun_workflow.ts index 5de1dc38e8d..6a1c9bf27c2 100644 --- a/apps/sim/tools/github/rerun_workflow.ts +++ b/apps/sim/tools/github/rerun_workflow.ts @@ -1,3 +1,4 @@ +import { githubPathSegment, githubRepoPath } from '@/tools/github/request-path' import type { RerunWorkflowParams, RerunWorkflowResponse } from '@/tools/github/types' import { GITHUB_CREDENTIAL_OAUTH } from '@/tools/shared/saved-credentials' import type { ToolConfig } from '@/tools/types' @@ -46,7 +47,7 @@ export const rerunWorkflowTool: ToolConfig - `https://api.github.com/repos/${params.owner}/${params.repo}/actions/runs/${params.run_id}/rerun`, + `https://api.github.com/repos/${githubRepoPath(params.owner, params.repo)}/actions/runs/${githubPathSegment(params.run_id, 'run_id')}/rerun`, method: 'POST', headers: (params) => ({ Accept: 'application/vnd.github+json', diff --git a/apps/sim/tools/github/star_gist.ts b/apps/sim/tools/github/star_gist.ts index cb0ccf1a4c2..5bef447a944 100644 --- a/apps/sim/tools/github/star_gist.ts +++ b/apps/sim/tools/github/star_gist.ts @@ -1,3 +1,4 @@ +import { githubPathSegment } from '@/tools/github/request-path' import { GITHUB_CREDENTIAL_OAUTH } from '@/tools/shared/saved-credentials' import type { ToolConfig } from '@/tools/types' @@ -40,7 +41,8 @@ export const starGistTool: ToolConfig = { }, request: { - url: (params) => `https://api.github.com/gists/${params.gist_id?.trim()}/star`, + url: (params) => + `https://api.github.com/gists/${githubPathSegment(params.gist_id, 'gist_id')}/star`, method: 'PUT', headers: (params) => ({ Accept: 'application/vnd.github.v3+json', diff --git a/apps/sim/tools/github/star_repo.ts b/apps/sim/tools/github/star_repo.ts index 0455c49b198..2eb914ad48d 100644 --- a/apps/sim/tools/github/star_repo.ts +++ b/apps/sim/tools/github/star_repo.ts @@ -1,3 +1,4 @@ +import { githubRepoPath } from '@/tools/github/request-path' import { GITHUB_CREDENTIAL_OAUTH } from '@/tools/shared/saved-credentials' import type { ToolConfig } from '@/tools/types' @@ -48,7 +49,8 @@ export const starRepoTool: ToolConfig = { }, request: { - url: (params) => `https://api.github.com/user/starred/${params.owner}/${params.repo}`, + url: (params) => + `https://api.github.com/user/starred/${githubRepoPath(params.owner, params.repo)}`, method: 'PUT', headers: (params) => ({ Accept: 'application/vnd.github.v3+json', diff --git a/apps/sim/tools/github/trigger_workflow.ts b/apps/sim/tools/github/trigger_workflow.ts index a40392104aa..d10e1d60a1b 100644 --- a/apps/sim/tools/github/trigger_workflow.ts +++ b/apps/sim/tools/github/trigger_workflow.ts @@ -1,4 +1,5 @@ import { isRecordLike } from '@sim/utils/object' +import { githubPathSegment, githubRepoPath } from '@/tools/github/request-path' import type { TriggerWorkflowParams, TriggerWorkflowResponse } from '@/tools/github/types' import { GITHUB_CREDENTIAL_OAUTH } from '@/tools/shared/saved-credentials' import type { ToolConfig } from '@/tools/types' @@ -75,7 +76,7 @@ export const triggerWorkflowTool: ToolConfig - `https://api.github.com/repos/${params.owner}/${params.repo}/actions/workflows/${params.workflow_id}/dispatches`, + `https://api.github.com/repos/${githubRepoPath(params.owner, params.repo)}/actions/workflows/${githubPathSegment(params.workflow_id, 'workflow_id')}/dispatches`, method: 'POST', headers: (params) => ({ Accept: 'application/vnd.github+json', diff --git a/apps/sim/tools/github/unstar_gist.ts b/apps/sim/tools/github/unstar_gist.ts index d2b5116bb4e..7c2f682bdae 100644 --- a/apps/sim/tools/github/unstar_gist.ts +++ b/apps/sim/tools/github/unstar_gist.ts @@ -1,3 +1,4 @@ +import { githubPathSegment } from '@/tools/github/request-path' import { GITHUB_CREDENTIAL_OAUTH } from '@/tools/shared/saved-credentials' import type { ToolConfig } from '@/tools/types' @@ -40,7 +41,8 @@ export const unstarGistTool: ToolConfig = }, request: { - url: (params) => `https://api.github.com/gists/${params.gist_id?.trim()}/star`, + url: (params) => + `https://api.github.com/gists/${githubPathSegment(params.gist_id, 'gist_id')}/star`, method: 'DELETE', headers: (params) => ({ Accept: 'application/vnd.github.v3+json', diff --git a/apps/sim/tools/github/unstar_repo.ts b/apps/sim/tools/github/unstar_repo.ts index ff3d772d307..896cb7282a2 100644 --- a/apps/sim/tools/github/unstar_repo.ts +++ b/apps/sim/tools/github/unstar_repo.ts @@ -1,3 +1,4 @@ +import { githubRepoPath } from '@/tools/github/request-path' import { GITHUB_CREDENTIAL_OAUTH } from '@/tools/shared/saved-credentials' import type { ToolConfig } from '@/tools/types' @@ -48,7 +49,8 @@ export const unstarRepoTool: ToolConfig = }, request: { - url: (params) => `https://api.github.com/user/starred/${params.owner}/${params.repo}`, + url: (params) => + `https://api.github.com/user/starred/${githubRepoPath(params.owner, params.repo)}`, method: 'DELETE', headers: (params) => ({ Accept: 'application/vnd.github.v3+json', diff --git a/apps/sim/tools/github/update_branch_protection.ts b/apps/sim/tools/github/update_branch_protection.ts index aa75d1036c7..394c294271a 100644 --- a/apps/sim/tools/github/update_branch_protection.ts +++ b/apps/sim/tools/github/update_branch_protection.ts @@ -1,3 +1,4 @@ +import { githubRefPath, githubRepoPath } from '@/tools/github/request-path' import type { BranchProtectionResponse, UpdateBranchProtectionParams } from '@/tools/github/types' import { BRANCH_PROTECTION_OUTPUT_PROPERTIES } from '@/tools/github/types' import { GITHUB_CREDENTIAL_OAUTH } from '@/tools/shared/saved-credentials' @@ -135,7 +136,7 @@ export const updateBranchProtectionTool: ToolConfig< request: { url: (params) => - `https://api.github.com/repos/${params.owner}/${params.repo}/branches/${params.branch}/protection`, + `https://api.github.com/repos/${githubRepoPath(params.owner, params.repo)}/branches/${githubRefPath(params.branch, 'branch')}/protection`, method: 'PUT', headers: (params) => ({ Accept: 'application/vnd.github+json', diff --git a/apps/sim/tools/github/update_comment.ts b/apps/sim/tools/github/update_comment.ts index b3c6e6644dc..e1cdc51c7b4 100644 --- a/apps/sim/tools/github/update_comment.ts +++ b/apps/sim/tools/github/update_comment.ts @@ -1,4 +1,5 @@ import { truncate } from '@sim/utils/string' +import { githubPathSegment, githubRepoPath } from '@/tools/github/request-path' import type { IssueCommentResponse, UpdateCommentParams } from '@/tools/github/types' import { COMMENT_OUTPUT_PROPERTIES, USER_OUTPUT } from '@/tools/github/types' import { GITHUB_CREDENTIAL_OAUTH } from '@/tools/shared/saved-credentials' @@ -46,7 +47,7 @@ export const updateCommentTool: ToolConfig - `https://api.github.com/repos/${params.owner}/${params.repo}/issues/comments/${params.comment_id}`, + `https://api.github.com/repos/${githubRepoPath(params.owner, params.repo)}/issues/comments/${githubPathSegment(params.comment_id, 'comment_id')}`, method: 'PATCH', headers: (params) => ({ Accept: 'application/vnd.github+json', diff --git a/apps/sim/tools/github/update_file.ts b/apps/sim/tools/github/update_file.ts index e26785a1e0a..d393cd64eab 100644 --- a/apps/sim/tools/github/update_file.ts +++ b/apps/sim/tools/github/update_file.ts @@ -1,4 +1,5 @@ import { encodeContentPath } from '@/tools/github/content-path' +import { githubRepoPath } from '@/tools/github/request-path' import type { FileOperationResponse, UpdateFileParams } from '@/tools/github/types' import { GITHUB_CREDENTIAL_OAUTH } from '@/tools/shared/saved-credentials' import type { ToolConfig } from '@/tools/types' @@ -64,7 +65,7 @@ export const updateFileTool: ToolConfig request: { url: (params) => - `https://api.github.com/repos/${params.owner}/${params.repo}/contents/${encodeContentPath(params.path)}`, + `https://api.github.com/repos/${githubRepoPath(params.owner, params.repo)}/contents/${encodeContentPath(params.path)}`, method: 'PUT', headers: (params) => ({ Accept: 'application/vnd.github+json', diff --git a/apps/sim/tools/github/update_gist.ts b/apps/sim/tools/github/update_gist.ts index 10cbd2f9aac..fb249be4b30 100644 --- a/apps/sim/tools/github/update_gist.ts +++ b/apps/sim/tools/github/update_gist.ts @@ -1,3 +1,4 @@ +import { githubPathSegment } from '@/tools/github/request-path' import { GITHUB_CREDENTIAL_OAUTH } from '@/tools/shared/saved-credentials' import type { ToolConfig } from '@/tools/types' @@ -63,7 +64,7 @@ export const updateGistTool: ToolConfig = }, request: { - url: (params) => `https://api.github.com/gists/${params.gist_id?.trim()}`, + url: (params) => `https://api.github.com/gists/${githubPathSegment(params.gist_id, 'gist_id')}`, method: 'PATCH', headers: (params) => ({ Accept: 'application/vnd.github.v3+json', diff --git a/apps/sim/tools/github/update_issue.ts b/apps/sim/tools/github/update_issue.ts index b3d3571194e..10d9865806d 100644 --- a/apps/sim/tools/github/update_issue.ts +++ b/apps/sim/tools/github/update_issue.ts @@ -1,3 +1,4 @@ +import { githubPathSegment, githubRepoPath } from '@/tools/github/request-path' import type { IssueResponse, UpdateIssueParams } from '@/tools/github/types' import { ISSUE_OUTPUT_PROPERTIES, @@ -74,7 +75,7 @@ export const updateIssueTool: ToolConfig = { request: { url: (params) => - `https://api.github.com/repos/${params.owner}/${params.repo}/issues/${params.issue_number}`, + `https://api.github.com/repos/${githubRepoPath(params.owner, params.repo)}/issues/${githubPathSegment(params.issue_number, 'issue_number')}`, method: 'PATCH', headers: (params) => ({ Accept: 'application/vnd.github.v3+json', diff --git a/apps/sim/tools/github/update_milestone.ts b/apps/sim/tools/github/update_milestone.ts index 9a5ffe3823c..7da7b772dfc 100644 --- a/apps/sim/tools/github/update_milestone.ts +++ b/apps/sim/tools/github/update_milestone.ts @@ -1,3 +1,4 @@ +import { githubPathSegment, githubRepoPath } from '@/tools/github/request-path' import { GITHUB_CREDENTIAL_OAUTH } from '@/tools/shared/saved-credentials' import type { ToolConfig } from '@/tools/types' @@ -90,7 +91,7 @@ export const updateMilestoneTool: ToolConfig - `https://api.github.com/repos/${params.owner}/${params.repo}/milestones/${params.milestone_number}`, + `https://api.github.com/repos/${githubRepoPath(params.owner, params.repo)}/milestones/${githubPathSegment(params.milestone_number, 'milestone_number')}`, method: 'PATCH', headers: (params) => ({ Accept: 'application/vnd.github.v3+json', diff --git a/apps/sim/tools/github/update_pr.ts b/apps/sim/tools/github/update_pr.ts index 38e12e346b0..adc7adb0afc 100644 --- a/apps/sim/tools/github/update_pr.ts +++ b/apps/sim/tools/github/update_pr.ts @@ -1,3 +1,4 @@ +import { githubPathSegment, githubRepoPath } from '@/tools/github/request-path' import type { PRResponse, UpdatePRParams } from '@/tools/github/types' import { GITHUB_CREDENTIAL_OAUTH } from '@/tools/shared/saved-credentials' import type { ToolConfig } from '@/tools/types' @@ -62,7 +63,7 @@ export const updatePRTool: ToolConfig = { request: { url: (params) => - `https://api.github.com/repos/${params.owner}/${params.repo}/pulls/${params.pullNumber}`, + `https://api.github.com/repos/${githubRepoPath(params.owner, params.repo)}/pulls/${githubPathSegment(params.pullNumber, 'pullNumber')}`, method: 'PATCH', headers: (params) => ({ Accept: 'application/vnd.github.v3+json', diff --git a/apps/sim/tools/github/update_release.ts b/apps/sim/tools/github/update_release.ts index 798b5b138d3..8d8d2d016de 100644 --- a/apps/sim/tools/github/update_release.ts +++ b/apps/sim/tools/github/update_release.ts @@ -1,3 +1,4 @@ +import { githubPathSegment, githubRepoPath } from '@/tools/github/request-path' import type { ReleaseResponse, UpdateReleaseParams } from '@/tools/github/types' import { RELEASE_ASSET_OUTPUT_PROPERTIES, @@ -80,7 +81,7 @@ export const updateReleaseTool: ToolConfig request: { url: (params) => - `https://api.github.com/repos/${params.owner}/${params.repo}/releases/${params.release_id}`, + `https://api.github.com/repos/${githubRepoPath(params.owner, params.repo)}/releases/${githubPathSegment(params.release_id, 'release_id')}`, method: 'PATCH', headers: (params) => ({ Accept: 'application/vnd.github+json',