Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { repoOpenSessionInputSchema } from './repo-shared.ts'

const mockModule = vi.hoisted(() => ({
getActiveRepoSessionByConversation: vi.fn(),
getEntitySourceById: vi.fn(),
getEntitySourceByIdForUser: vi.fn(),
getSavedPackageByKodyId: vi.fn(),
repoSessionRpc: vi.fn(),
}))
Expand All @@ -18,8 +18,8 @@ vi.mock('#worker/repo/repo-sessions.ts', () => ({
}))

vi.mock('#worker/repo/entity-sources.ts', () => ({
getEntitySourceById: (...args: Array<unknown>) =>
mockModule.getEntitySourceById(...args),
getEntitySourceByIdForUser: (...args: Array<unknown>) =>
mockModule.getEntitySourceByIdForUser(...args),
}))

vi.mock('#worker/package-registry/repo.ts', () => ({
Expand Down Expand Up @@ -139,7 +139,7 @@ function createOpenSessionResult() {

function resetMocks() {
mockModule.getActiveRepoSessionByConversation.mockReset()
mockModule.getEntitySourceById.mockReset()
mockModule.getEntitySourceByIdForUser.mockReset()
mockModule.getSavedPackageByKodyId.mockReset()
mockModule.repoSessionRpc.mockReset()
}
Expand Down Expand Up @@ -193,7 +193,7 @@ test('repo_open_session enforces the repo sessions entitlement for plan users op
mockModule.getSavedPackageByKodyId.mockResolvedValueOnce(
createSavedPackageRow(userId),
)
mockModule.getEntitySourceById.mockResolvedValueOnce(
mockModule.getEntitySourceByIdForUser.mockResolvedValueOnce(
createPackageSourceRow(userId),
)
const openRpc = createRepoRpc()
Expand Down Expand Up @@ -257,7 +257,7 @@ test('repo_open_session resumes an existing active session without enforcing the
mockModule.getSavedPackageByKodyId.mockResolvedValueOnce(
createSavedPackageRow(userId),
)
mockModule.getEntitySourceById.mockResolvedValueOnce(
mockModule.getEntitySourceByIdForUser.mockResolvedValueOnce(
createPackageSourceRow(userId),
)
const resumeRpc = createRepoRpc()
Expand Down Expand Up @@ -309,7 +309,7 @@ test('repo_open_session allows below-max usage and denies at the max plan ceilin
mockModule.getSavedPackageByKodyId.mockResolvedValueOnce(
createSavedPackageRow(userId),
)
mockModule.getEntitySourceById.mockResolvedValueOnce(
mockModule.getEntitySourceByIdForUser.mockResolvedValueOnce(
createPackageSourceRow(userId),
)
const belowMaxRpc = createRepoRpc()
Expand Down Expand Up @@ -347,7 +347,7 @@ test('repo_open_session allows below-max usage and denies at the max plan ceilin
mockModule.getSavedPackageByKodyId.mockResolvedValueOnce(
createSavedPackageRow(userId),
)
mockModule.getEntitySourceById.mockResolvedValueOnce(
mockModule.getEntitySourceByIdForUser.mockResolvedValueOnce(
createPackageSourceRow(userId),
)
const deniedRpc = createRepoRpc()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ import { expect, test, vi } from 'vitest'
import { McpCallerError } from '#mcp/caller-error.ts'

const mockModule = vi.hoisted(() => ({
getEntitySourceById: vi.fn(),
getEntitySourceByIdForUser: vi.fn(),
getSavedPackageById: vi.fn(),
getSavedPackageByKodyId: vi.fn(),
}))

vi.mock('#worker/repo/entity-sources.ts', () => ({
getEntitySourceById: (...args: Array<unknown>) =>
mockModule.getEntitySourceById(...args),
getEntitySourceByIdForUser: (...args: Array<unknown>) =>
mockModule.getEntitySourceByIdForUser(...args),
}))

vi.mock('#worker/package-registry/repo.ts', () => ({
Expand All @@ -22,11 +22,11 @@ vi.mock('#worker/package-registry/repo.ts', () => ({
const { resolveRepoSourceReference } = await import('./repo-resolve-target.ts')

test('resolveRepoSourceReference throws McpCallerError for missing source and package', async () => {
mockModule.getEntitySourceById.mockReset()
mockModule.getEntitySourceByIdForUser.mockReset()
mockModule.getSavedPackageById.mockReset()
mockModule.getSavedPackageByKodyId.mockReset()

mockModule.getEntitySourceById.mockResolvedValue(null)
mockModule.getEntitySourceByIdForUser.mockResolvedValue(null)
const missingSource = resolveRepoSourceReference({
db: {} as D1Database,
userId: 'user-1',
Expand All @@ -49,4 +49,34 @@ test('resolveRepoSourceReference throws McpCallerError for missing source and pa
await expect(missingPackage).rejects.toThrow(
'Saved package "pkg-missing" was not found.',
)

const missingIdentity = resolveRepoSourceReference({
db: {} as D1Database,
userId: 'user-1',
args: {},
})

await expect(missingIdentity).rejects.toThrow(McpCallerError)
await expect(missingIdentity).rejects.toThrow(
'Repo source identity is required.',
)
})

test('resolveRepoSourceReference scopes the source lookup to the caller', async () => {
mockModule.getEntitySourceByIdForUser.mockReset()
mockModule.getEntitySourceByIdForUser.mockResolvedValue(null)

await expect(
resolveRepoSourceReference({
db: {} as D1Database,
userId: 'user-1',
args: { source_id: 'source-1' },
}),
).rejects.toThrow('Repo source was not found for this user.')

// The user predicate belongs in the query, not in a post-read comparison.
expect(mockModule.getEntitySourceByIdForUser).toHaveBeenCalledWith(
expect.anything(),
{ id: 'source-1', userId: 'user-1' },
)
})
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
getSavedPackageById,
getSavedPackageByKodyId,
} from '#worker/package-registry/repo.ts'
import { getEntitySourceById } from '#worker/repo/entity-sources.ts'
import { getEntitySourceByIdForUser } from '#worker/repo/entity-sources.ts'
import { type EntitySourceRow } from '#worker/repo/types.ts'
import {
type repoOpenSessionInputSchema,
Expand All @@ -21,8 +21,11 @@ async function requireOwnedEntitySource(input: {
userId: string
sourceId: string
}): Promise<EntitySourceRow> {
const source = await getEntitySourceById(input.db, input.sourceId)
if (!source || source.user_id !== input.userId) {
const source = await getEntitySourceByIdForUser(input.db, {
id: input.sourceId,
userId: input.userId,
})
if (!source) {
throw new McpCallerError('Repo source was not found for this user.')
}
return source
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { expect, test, vi } from 'vitest'

const mockModule = vi.hoisted(() => ({
getEntitySourceById: vi.fn(),
getEntitySourceByIdForUser: vi.fn(),
resolveOwnedPackageSource: vi.fn(),
readPublishGitNoteFromArtifactsRepo: vi.fn(),
}))

vi.mock('#worker/repo/entity-sources.ts', () => ({
getEntitySourceById: (...args: Array<unknown>) =>
mockModule.getEntitySourceById(...args),
getEntitySourceByIdForUser: (...args: Array<unknown>) =>
mockModule.getEntitySourceByIdForUser(...args),
}))

vi.mock('#mcp/capabilities/packages/resolve-package-source.ts', () => ({
Expand Down Expand Up @@ -84,7 +84,7 @@ const sampleNote = {

test('repo_show_publish_note reads notes by source or package identity and rejects cross-user access', async () => {
resetMocks()
mockModule.getEntitySourceById.mockResolvedValue(sampleSource)
mockModule.getEntitySourceByIdForUser.mockResolvedValue(sampleSource)
mockModule.readPublishGitNoteFromArtifactsRepo.mockResolvedValue({
found: true,
commit: 'commit-published',
Expand Down Expand Up @@ -127,15 +127,18 @@ test('repo_show_publish_note reads notes by source or package identity and rejec
expect(byPackage.found).toBe(false)
expect(byPackage.commit).toBe('commit-old')

// Another user's source is filtered out by the query itself, so the lookup
// returns nothing rather than returning a row we then have to reject.
resetMocks()
mockModule.getEntitySourceById.mockResolvedValue({
...sampleSource,
user_id: 'other-user',
})
mockModule.getEntitySourceByIdForUser.mockResolvedValue(null)
await expect(
repoShowPublishNoteCapability.handler(
{ source_id: 'source-1' },
createContext(),
),
).rejects.toThrow('Repo source was not found for this user.')
expect(mockModule.getEntitySourceByIdForUser).toHaveBeenCalledWith(
expect.anything(),
expect.objectContaining({ id: 'source-1' }),
)
Comment on lines +140 to +143

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔒 Security & Privacy | 🟡 Minor | ⚡ Quick win

Assert the caller’s userId in this isolation test.

The test only verifies id, so it would still pass if the lookup received an omitted or incorrect userId. Assert the complete scoped lookup contract.

As per coding guidelines, every Kody multi-user read path must be scoped by userId.

Proposed test fix
 expect(mockModule.getEntitySourceByIdForUser).toHaveBeenCalledWith(
 	expect.anything(),
-	expect.objectContaining({ id: 'source-1' }),
+	{ id: 'source-1', userId: 'user-1' },
 )
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
expect(mockModule.getEntitySourceByIdForUser).toHaveBeenCalledWith(
expect.anything(),
expect.objectContaining({ id: 'source-1' }),
)
expect(mockModule.getEntitySourceByIdForUser).toHaveBeenCalledWith(
expect.anything(),
{ id: 'source-1', userId: 'user-1' },
)
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In
`@packages/worker/src/mcp/capabilities/repo/repo-show-publish-note.node.test.ts`
around lines 140 - 143, Update the assertion for getEntitySourceByIdForUser in
the isolation test to verify the complete lookup input, including the caller’s
expected userId alongside source-1. Preserve the existing expectation that the
first argument is accepted and ensure the assertion fails when userId is omitted
or incorrect.

Source: Coding guidelines

})
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { capabilityDomainNames } from '#mcp/capabilities/domain-metadata.ts'
import { requireMcpUser } from '#mcp/capabilities/meta/require-user.ts'
import { resolveOwnedPackageSource } from '#mcp/capabilities/packages/resolve-package-source.ts'
import { McpCallerError } from '#mcp/caller-error.ts'
import { getEntitySourceById } from '#worker/repo/entity-sources.ts'
import { getEntitySourceByIdForUser } from '#worker/repo/entity-sources.ts'
import {
kodyPublishGitNoteSchema,
readPublishGitNoteFromArtifactsRepo,
Expand Down Expand Up @@ -75,7 +75,10 @@ export const repoShowPublishNoteCapability = defineDomainCapability(
const user = requireMcpUser(ctx.callerContext)
const source =
args.source_id !== undefined
? await getEntitySourceById(ctx.env.APP_DB, args.source_id)
? await getEntitySourceByIdForUser(ctx.env.APP_DB, {
id: args.source_id,
userId: user.userId,
})
: (
await resolveOwnedPackageSource({
db: ctx.env.APP_DB,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { createMcpCallerContext } from '#mcp/context.ts'

const mockModule = vi.hoisted(() => ({
getActiveRepoSessionByConversation: vi.fn(),
getEntitySourceById: vi.fn(),
getEntitySourceByIdForUser: vi.fn(),
getSavedPackageById: vi.fn(),
getSavedPackageByKodyId: vi.fn(),
repoSessionRpc: vi.fn(),
Expand All @@ -15,8 +15,8 @@ vi.mock('#worker/repo/repo-sessions.ts', () => ({
}))

vi.mock('#worker/repo/entity-sources.ts', () => ({
getEntitySourceById: (...args: Array<unknown>) =>
mockModule.getEntitySourceById(...args),
getEntitySourceByIdForUser: (...args: Array<unknown>) =>
mockModule.getEntitySourceByIdForUser(...args),
}))

vi.mock('#worker/package-registry/repo.ts', () => ({
Expand Down Expand Up @@ -77,7 +77,7 @@ function createRepoRpc(overrides?: Partial<Record<string, unknown>>) {

function resetMocks() {
mockModule.getActiveRepoSessionByConversation.mockReset()
mockModule.getEntitySourceById.mockReset()
mockModule.getEntitySourceByIdForUser.mockReset()
mockModule.getSavedPackageById.mockReset()
mockModule.getSavedPackageByKodyId.mockReset()
mockModule.repoSessionRpc.mockReset()
Expand Down Expand Up @@ -123,7 +123,9 @@ test('repo open → run commands → publish session workflow', async () => {
mockModule.getSavedPackageByKodyId.mockResolvedValueOnce(
createSavedPackageRow(),
)
mockModule.getEntitySourceById.mockResolvedValueOnce(createPackageSourceRow())
mockModule.getEntitySourceByIdForUser.mockResolvedValueOnce(
createPackageSourceRow(),
)
const openRpc = createRepoRpc()
openRpc.openSession.mockResolvedValueOnce({
id: 'session-1',
Expand Down Expand Up @@ -175,7 +177,7 @@ test('repo open → run commands → publish session workflow', async () => {
mockModule.getSavedPackageByKodyId.mockResolvedValueOnce(
createSavedPackageRow(),
)
mockModule.getEntitySourceById.mockResolvedValueOnce({
mockModule.getEntitySourceByIdForUser.mockResolvedValueOnce({
...createPackageSourceRow(),
id: 'source-other',
entity_id: 'package-other',
Expand Down Expand Up @@ -208,7 +210,7 @@ test('repo open → run commands → publish session workflow', async () => {
mockModule.getSavedPackageById
.mockResolvedValueOnce(createSavedPackageRow())
.mockResolvedValueOnce(createSavedPackageRow())
mockModule.getEntitySourceById
mockModule.getEntitySourceByIdForUser
.mockResolvedValueOnce(createPackageSourceRow())
.mockResolvedValueOnce(createPackageSourceRow())
const resumeRpc = createRepoRpc()
Expand Down Expand Up @@ -309,7 +311,7 @@ test('repo open → run commands → publish session workflow', async () => {
createSavedPackageRow(),
)
mockModule.getSavedPackageById.mockResolvedValueOnce(createSavedPackageRow())
mockModule.getEntitySourceById
mockModule.getEntitySourceByIdForUser
.mockResolvedValueOnce(createPackageSourceRow())
.mockResolvedValueOnce(createPackageSourceRow())
const failedChecksRpc = createRepoRpc()
Expand Down Expand Up @@ -428,7 +430,7 @@ test('repo open → run commands → publish session workflow', async () => {
mockModule.getSavedPackageById
.mockResolvedValueOnce(createSavedPackageRow())
.mockResolvedValueOnce(createSavedPackageRow())
mockModule.getEntitySourceById
mockModule.getEntitySourceByIdForUser
.mockResolvedValueOnce(createPackageSourceRow())
.mockResolvedValueOnce(createPackageSourceRow())
const publishRpc = createRepoRpc()
Expand Down
Loading