-
-
Notifications
You must be signed in to change notification settings - Fork 38
refactor(app): resolve projects within team context on nested routes #757
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "@shelve/app": patch | ||
| --- | ||
|
|
||
| Align nested project API handlers with the team from the URL path. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,4 @@ | ||
| import { projectIdParamsSchema } from '~~/server/db/zod' | ||
|
|
||
| export default eventHandler(async (event) => { | ||
| const slug = await getTeamSlugFromEvent(event) | ||
| await requireUserTeam(event, slug) | ||
| const { projectId } = await getValidatedRouterParams(event, projectIdParamsSchema.parse) | ||
| return await new ProjectsService().getProject(projectId) | ||
| const { project } = await requireUserTeamProject(event) | ||
| return project | ||
| }) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,4 @@ | ||
| import { projectIdParamsSchema } from '~~/server/db/zod' | ||
|
|
||
| export default eventHandler(async (event) => { | ||
| const slug = await getTeamSlugFromEvent(event) | ||
| await requireUserTeam(event, slug) | ||
| const { projectId } = await getValidatedRouterParams(event, projectIdParamsSchema.parse) | ||
|
|
||
| return await new VariableGroupsService().getGroups(projectId) | ||
| const { project } = await requireUserTeamProject(event) | ||
| return await new VariableGroupsService().getGroups(project.id) | ||
| }) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -23,9 +23,12 @@ export class ProjectsService { | |
|
|
||
| const [updatedProject] = await db.update(schema.projects) | ||
| .set(input) | ||
| .where(eq(schema.projects.id, input.id)) | ||
| .where(and( | ||
| eq(schema.projects.id, input.id), | ||
| eq(schema.projects.teamId, input.teamId), | ||
| )) | ||
| .returning() | ||
| if (!updatedProject) throw createError({ statusCode: 422, message: 'Failed to update project' }) | ||
| if (!updatedProject) throw createError({ statusCode: 404, message: `Project not found with id ${input.id}` }) | ||
|
Comment on lines
+26
to
+31
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Scope the pre-update lookup to the caller's team.
Suggested fix async updateProject(input: ProjectUpdateInput): Promise<Project> {
- const existingProject = await this.getProject(input.id)
- if (!existingProject) throw createError({ statusCode: 404, message: `Project not found with id ${input.id}` })
+ const existingProject = await this.getProjectForTeam(input.id, input.teamId)
if (existingProject.name !== input.name)
- await this.validateProjectName(input.name, existingProject.teamId, input.id)
+ await this.validateProjectName(input.name, input.teamId, input.id)
const [updatedProject] = await db.update(schema.projects)🤖 Prompt for AI Agents |
||
| await clearCache('Projects', updatedProject.teamId) | ||
| await clearCache('Project', updatedProject.id) | ||
|
|
||
|
|
@@ -40,6 +43,15 @@ export class ProjectsService { | |
| return project | ||
| }) | ||
|
|
||
| /** Resolves a project only when it belongs to the given team (prevents cross-tenant IDOR). */ | ||
| async getProjectForTeam(projectId: number, teamId: number): Promise<Project> { | ||
| const project = await this.getProject(projectId) | ||
| if (project.teamId !== teamId) { | ||
| throw createError({ statusCode: 404, message: `Project not found with id ${projectId}` }) | ||
| } | ||
| return project | ||
| } | ||
|
|
||
| getProjects = withCache<Project[]>('Projects', (teamId: number) => { | ||
| return db.query.projects.findMany({ | ||
| where: eq(schema.projects.teamId, teamId), | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧩 Analysis chain
🏁 Script executed:
Repository: HugoRCD/shelve
Length of output: 3421
🏁 Script executed:
Repository: HugoRCD/shelve
Length of output: 101
🏁 Script executed:
Repository: HugoRCD/shelve
Length of output: 101
🏁 Script executed:
Repository: HugoRCD/shelve
Length of output: 4459
Make
deleteGroupenforceprojectIdin the delete query (atomic scope)VariableGroupsService.deleteGroup(id: number)deletes solely byvariableGroups.id(noprojectId), while the handler deletes via baregroupIdafter a separategetGroupForProject(...)read. That two-step, non-atomic flow weakens the project-scoped write contract.File:
apps/shelve/server/api/teams/[slug]/projects/[projectId]/variable-groups/[groupId]/index.delete.ts(lines 8-9)Prefer a project-scoped delete API (e.g.
deleteGroup(groupId, project.id)withWHERE id = ... AND projectId = ...) and call it directly.🤖 Prompt for AI Agents