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
5 changes: 5 additions & 0 deletions .changeset/fruity-ghosts-brake.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@tanstack/intent': patch
---

Refuse to load a Yarn PnP runtime resolved from within node_modules; only a project root or ancestor .pnp.cjs is trusted.
10 changes: 10 additions & 0 deletions packages/intent/src/discovery/scanner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@ function findPnpFile(start: string): string | null {
return null
}

export function isPnpRuntimeWithinNodeModules(pnpPath: string): boolean {
return resolve(pnpPath).split(sep).includes('node_modules')
}

function assertLocalNodeModulesSupported(root: string): void {
if (
existsSync(join(root, 'deno.json')) &&
Expand All @@ -106,6 +110,12 @@ function loadPnpApi(root: string): LoadedPnp | null {
const pnpPath = findPnpFile(root)
if (!pnpPath) return null

if (isPnpRuntimeWithinNodeModules(pnpPath)) {
throw new Error(
`Refusing to load a Yarn PnP runtime resolved from within node_modules: ${pnpPath}. Only a project root or ancestor .pnp.cjs is trusted.`,
)
}

const moduleApi = requireFromHere('node:module') as NodeModuleInternals
const originalResolveFilename = moduleApi._resolveFilename
// Capture `fs` before setup(). Yarn's `setup()` patches the `fs` module in
Expand Down
82 changes: 82 additions & 0 deletions packages/intent/tests/cli-output.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import {
ALLOW_ALL_NOTICE,
printNotices,
printWarnings,
} from '../src/shared/cli-output.js'

const OTHER_NOTICE = 'intent.skills is empty — no skill sources are permitted.'

describe('printNotices — ALLOW_ALL_NOTICE is non-suppressible', () => {
let errorSpy: ReturnType<typeof vi.spyOn>
let logSpy: ReturnType<typeof vi.spyOn>
const previousEnv = process.env.INTENT_NO_NOTICES

beforeEach(() => {
delete process.env.INTENT_NO_NOTICES
errorSpy = vi.spyOn(console, 'error').mockImplementation(() => {})
logSpy = vi.spyOn(console, 'log').mockImplementation(() => {})
})

afterEach(() => {
errorSpy.mockRestore()
logSpy.mockRestore()
if (previousEnv === undefined) delete process.env.INTENT_NO_NOTICES
else process.env.INTENT_NO_NOTICES = previousEnv
})

function stderr(): string {
return errorSpy.mock.calls
.map((call: Array<unknown>) => call.join(' '))
.join('\n')
}

it('prints the permit-all banner even when noNotices is set, and suppresses the rest', () => {
printNotices([OTHER_NOTICE, ALLOW_ALL_NOTICE], { noNotices: true })

const output = stderr()
expect(output).toContain(ALLOW_ALL_NOTICE)
expect(output).not.toContain(OTHER_NOTICE)
})

it('prints the permit-all banner even when INTENT_NO_NOTICES=1', () => {
process.env.INTENT_NO_NOTICES = '1'

printNotices([OTHER_NOTICE, ALLOW_ALL_NOTICE])

const output = stderr()
expect(output).toContain(ALLOW_ALL_NOTICE)
expect(output).not.toContain(OTHER_NOTICE)
})

it('prints every notice when suppression is off', () => {
printNotices([OTHER_NOTICE, ALLOW_ALL_NOTICE])

const output = stderr()
expect(output).toContain(ALLOW_ALL_NOTICE)
expect(output).toContain(OTHER_NOTICE)
})

it('prints nothing when only non-banner notices are suppressed', () => {
printNotices([OTHER_NOTICE], { noNotices: true })

expect(errorSpy).not.toHaveBeenCalled()
})

it('prints nothing for an empty notice list', () => {
printNotices([], { noNotices: true })

expect(errorSpy).not.toHaveBeenCalled()
})

it('routes the banner through the notices (stderr) channel, never the warnings channel', () => {
printNotices([ALLOW_ALL_NOTICE], { noNotices: true })
printWarnings([])

expect(stderr()).toContain(ALLOW_ALL_NOTICE)
const stdout = logSpy.mock.calls
.map((call: Array<unknown>) => call.join(' '))
.join('\n')
expect(stdout).not.toContain(ALLOW_ALL_NOTICE)
})
})
40 changes: 40 additions & 0 deletions packages/intent/tests/pnp-trust-boundary.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { sep } from 'node:path'
import { describe, expect, it } from 'vitest'
import { isPnpRuntimeWithinNodeModules } from '../src/discovery/scanner.js'

const abs = (...segments: Array<string>): string => sep + segments.join(sep)

describe('isPnpRuntimeWithinNodeModules', () => {
it('trusts a project-root .pnp.cjs', () => {
expect(isPnpRuntimeWithinNodeModules(abs('app', '.pnp.cjs'))).toBe(false)
})

it('trusts an ancestor .pnp.cjs above the project', () => {
expect(isPnpRuntimeWithinNodeModules(abs('.pnp.cjs'))).toBe(false)
expect(
isPnpRuntimeWithinNodeModules(abs('workspace', 'app', '..', '.pnp.cjs')),
).toBe(false)
})

it('rejects a .pnp.cjs resolved from within node_modules', () => {
expect(
isPnpRuntimeWithinNodeModules(
abs('app', 'node_modules', 'evil-pkg', '.pnp.cjs'),
),
).toBe(true)
})

it('rejects a nested-node_modules .pnp.cjs', () => {
expect(
isPnpRuntimeWithinNodeModules(
abs('app', 'node_modules', 'a', 'node_modules', 'b', '.pnp.cjs'),
),
).toBe(true)
})

it('does not match a directory whose name merely contains node_modules', () => {
expect(
isPnpRuntimeWithinNodeModules(abs('app', 'my-node_modules', '.pnp.cjs')),
).toBe(false)
})
})
Loading