Skip to content

Commit 9682fd6

Browse files
authored
Merge pull request #25 from Paca-AI/fix/jsonbody-tinygo-collision
Fix cross-project GitHub task IDOR; scope webhooks per project; add github.manage permission
2 parents efa23f9 + 1352733 commit 9682fd6

6 files changed

Lines changed: 481 additions & 50 deletions

File tree

‎backend/branches.go‎

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ type createBranchResponse struct {
2727
func (p *githubPlugin) createBranch(req *plugin.Request, res *plugin.Response) {
2828
projectID := req.Caller.ProjectID
2929
taskID := req.PathParam("taskId")
30+
if !p.taskBelongsToProject(taskID, projectID, res) {
31+
return
32+
}
3033

3134
type createBranchBody struct {
3235
RepoID string `json:"repo_id"`
@@ -112,6 +115,9 @@ func (p *githubPlugin) createBranch(req *plugin.Request, res *plugin.Response) {
112115
func (p *githubPlugin) linkBranchToTask(req *plugin.Request, res *plugin.Response) {
113116
projectID := req.Caller.ProjectID
114117
taskID := req.PathParam("taskId")
118+
if !p.taskBelongsToProject(taskID, projectID, res) {
119+
return
120+
}
115121

116122
type linkBranchToTaskBody struct {
117123
RepoID string `json:"repo_id"`
@@ -199,24 +205,48 @@ func (p *githubPlugin) linkBranchToTask(req *plugin.Request, res *plugin.Respons
199205
// ─── GET /tasks/:taskId/github/branches ───────────────────────────────────────
200206

201207
func (p *githubPlugin) listTaskBranches(req *plugin.Request, res *plugin.Response) {
208+
projectID := req.Caller.ProjectID
202209
taskID := req.PathParam("taskId")
210+
if !p.taskBelongsToProject(taskID, projectID, res) {
211+
return
212+
}
203213

204-
result, err := p.db.Query(`
205-
SELECT id, task_id, repo_id, branch_name, created_at
206-
FROM github_task_branches WHERE task_id = $1 ORDER BY created_at ASC
207-
`, taskID)
214+
result, err := p.db.Query(
215+
`SELECT id, task_id, repo_id, branch_name, created_at FROM github_task_branches WHERE task_id = $1 ORDER BY created_at ASC`,
216+
taskID,
217+
)
208218
if err != nil {
209219
apiError(res, 500, "INTERNAL_ERROR", err.Error())
210220
return
211221
}
212222

223+
// github_task_branches has no project_id column of its own — only
224+
// repo_id, which links to github_repositories (which does). Re-verify
225+
// each branch's repo against the caller's project as defense-in-depth:
226+
// taskBelongsToProject above already closes the main vector (a foreign
227+
// taskId), but this also protects against any row a pre-fix caller
228+
// might have already linked across projects. No SQL JOIN here (kept
229+
// consistent with resolvePRForTask's style elsewhere in this plugin,
230+
// which also resolves through separate single-table queries).
213231
items := make([]taskBranchResponse, 0, len(result.Rows))
214232
for _, row := range result.Rows {
215233
sc := newRowScanner(result.Columns, row)
234+
repoID := sc.str("repo_id")
235+
repoResult, rErr := p.db.Query(
236+
`SELECT id FROM github_repositories WHERE id = $1 AND project_id = $2`,
237+
repoID, projectID,
238+
)
239+
if rErr != nil {
240+
apiError(res, 500, "INTERNAL_ERROR", rErr.Error())
241+
return
242+
}
243+
if len(repoResult.Rows) == 0 {
244+
continue
245+
}
216246
items = append(items, taskBranchResponse{
217247
ID: sc.str("id"),
218248
TaskID: sc.str("task_id"),
219-
RepoID: sc.str("repo_id"),
249+
RepoID: repoID,
220250
BranchName: sc.str("branch_name"),
221251
CreatedAt: sc.str("created_at"),
222252
})

‎backend/plugin.go‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,30 @@ func apiError(res *plugin.Response, code int, errCode, message string) {
9898
})
9999
}
100100

101+
// taskBelongsToProject verifies taskID exists, is not deleted, and belongs
102+
// to projectID — writing a 404 and returning false otherwise. Every handler
103+
// that accepts a :taskId path param and uses it to read or write
104+
// project-scoped GitHub data (PRs, branches) must call this first: the
105+
// host's route-level permission check only verifies the caller belongs to
106+
// the project in the URL, it has no way to also verify an arbitrary path
107+
// param like :taskId belongs to that same project — that's this plugin's
108+
// job, the same role resolvePRForTask plays for PR-specific resources.
109+
func (p *githubPlugin) taskBelongsToProject(taskID, projectID string, res *plugin.Response) bool {
110+
result, err := p.db.Query(
111+
`SELECT id FROM tasks WHERE id = $1 AND project_id = $2 AND deleted_at IS NULL`,
112+
taskID, projectID,
113+
)
114+
if err != nil {
115+
apiError(res, 500, "INTERNAL_ERROR", err.Error())
116+
return false
117+
}
118+
if len(result.Rows) == 0 {
119+
apiError(res, 404, "TASK_NOT_FOUND", "Task not found")
120+
return false
121+
}
122+
return true
123+
}
124+
101125
// ─── event handlers ──────────────────────────────────────────────────────────
102126

103127
// handleTaskDeleted cleans up branches and PR links when a task is deleted.

0 commit comments

Comments
 (0)