diff --git a/.changeset/app-security-untrusted-git-config.md b/.changeset/app-security-untrusted-git-config.md new file mode 100644 index 00000000000..7a2f10d0077 --- /dev/null +++ b/.changeset/app-security-untrusted-git-config.md @@ -0,0 +1,6 @@ +--- +'@shopify/app': patch +'@shopify/cli': patch +--- + +`shopify app security check` only reads from Git: it no longer starts file-system monitors or fetches missing objects, and it doesn't use repositories committed inside the scanned directory diff --git a/packages/app/src/cli/services/app-security-engine/rules/secret-rules.ts b/packages/app/src/cli/services/app-security-engine/rules/secret-rules.ts index 6be8323e627..c6434afbfa1 100644 --- a/packages/app/src/cli/services/app-security-engine/rules/secret-rules.ts +++ b/packages/app/src/cli/services/app-security-engine/rules/secret-rules.ts @@ -1,5 +1,5 @@ +import {runGit} from '../scanners/git.js' import {basename, dirname} from '@shopify/cli-kit/node/path' -import {captureOutputWithExitCode} from '@shopify/cli-kit/node/system' import type {ScanContext, SourceFile} from './types.js' import type {AppSecurityScope, Issue} from '../types.js' @@ -161,7 +161,7 @@ type IgnoredFileScanReason = 'nested-repository' | 'listing-failed' | 'unknown' // A missing git binary resolves with exit code 0 and empty output. async function gitTopLevel(cwd: string): Promise { - const result = await runGit(cwd, ['rev-parse', '--show-toplevel']) + const result = await gitProbe(cwd, ['rev-parse', '--show-toplevel']) return result.exitCode === 0 && result.out !== '' ? result.out : undefined } @@ -352,20 +352,15 @@ interface GitFileStatus { evidence?: string[] } -async function runGit(cwd: string, args: string[]): Promise<{exitCode?: number; out: string}> { - try { - const result = await captureOutputWithExitCode('git', args, {cwd}) - return {exitCode: result.exitCode, out: result.stdout.trim()} - // Missing Git or a failed probe is unknown status, not proof the file is safe. - // eslint-disable-next-line no-catch-all/no-catch-all - } catch { - return {exitCode: undefined, out: ''} - } +/** Missing Git or a failed probe is unknown status, not proof the file is safe. */ +async function gitProbe(cwd: string, args: string[]): Promise<{exitCode?: number; out: string}> { + const result = await runGit(cwd, args) + return {exitCode: result?.exitCode, out: result?.stdout.trim() ?? ''} } /** Asks the file's own repository, which may differ from the app's: a scan directory can be another repository. */ export async function gitStatusFor(file: Pick): Promise { - const run = async (args: string[]) => runGit(dirname(file.absolutePath), args) + const run = async (args: string[]) => gitProbe(dirname(file.absolutePath), args) const name = basename(file.absolutePath) // Is this even a git repo? If not, we cannot confirm anything. diff --git a/packages/app/src/cli/services/app-security-engine/scanners/git.ts b/packages/app/src/cli/services/app-security-engine/scanners/git.ts new file mode 100644 index 00000000000..8b94cf3f3b2 --- /dev/null +++ b/packages/app/src/cli/services/app-security-engine/scanners/git.ts @@ -0,0 +1,52 @@ +import {captureOutputWithExitCode} from '@shopify/cli-kit/node/system' + +export interface GitProtection { + args?: string[] + env?: Record +} + +/** + * The scanned directory is untrusted, and Git reads settings from it that name programs to run. A repository + * delivered with its `.git`, or a bare repository committed as ordinary files and so carried by a plain clone, + * can set them. `runGit` applies every protection to every Git command in the engine. + */ +export const UNTRUSTED_REPOSITORY_PROTECTIONS = { + // Reading the index runs the file-system monitor program. The empty value turns it off on every Git version; + // older versions take `false` as the program's name. + fileSystemMonitor: {args: ['-c', 'core.fsmonitor=']}, + // Refuses a bare repository Git would otherwise find in the working directory, such as one committed as files. + bareRepository: {args: ['-c', 'safe.bareRepository=explicit']}, + // A partial clone fetches a missing object on demand, running the repository's transport commands. + lazyFetch: {env: {GIT_NO_LAZY_FETCH: '1'}}, + // An empty allow-list refuses every transport, for Git versions that predate GIT_NO_LAZY_FETCH. + transports: {env: {GIT_ALLOW_PROTOCOL: ''}}, +} satisfies {[name: string]: GitProtection} + +export type GitProtectionName = keyof typeof UNTRUSTED_REPOSITORY_PROTECTIONS + +const protections: GitProtection[] = Object.values(UNTRUSTED_REPOSITORY_PROTECTIONS) +const protectionArguments = protections.flatMap((protection) => protection.args ?? []) + +/** Extends the inherited environment. A fresh object each time, because the runner may add to it. */ +function protectionEnvironment(): Record { + return Object.fromEntries(protections.flatMap((protection) => Object.entries(protection.env ?? {}))) +} + +interface GitResult { + exitCode: number + stdout: string +} + +/** Runs Git in the directory. Undefined when Git can't be started. */ +export async function runGit(directory: string, args: string[]): Promise { + try { + const result = await captureOutputWithExitCode('git', [...protectionArguments, ...args], { + cwd: directory, + env: protectionEnvironment(), + }) + return {exitCode: result.exitCode, stdout: result.stdout} + // eslint-disable-next-line no-catch-all/no-catch-all + } catch { + return undefined + } +} diff --git a/packages/app/src/cli/services/app-security-engine/scanners/path-rules.ts b/packages/app/src/cli/services/app-security-engine/scanners/path-rules.ts index dfabefa5424..32c25749101 100644 --- a/packages/app/src/cli/services/app-security-engine/scanners/path-rules.ts +++ b/packages/app/src/cli/services/app-security-engine/scanners/path-rules.ts @@ -1,8 +1,8 @@ import {findRepositoryMarker} from './repository-marker.js' import {isMissingFilesystemEntry} from './filesystem-errors.js' +import {runGit} from './git.js' import {matchGlob} from '@shopify/cli-kit/node/fs' import {outputDebug} from '@shopify/cli-kit/node/output' -import {captureOutputWithExitCode} from '@shopify/cli-kit/node/system' import {basename, cwd, dirname, joinPath, relativePath, resolvePath} from '@shopify/cli-kit/node/path' import {lstatSync, realpathSync} from 'node:fs' @@ -190,13 +190,3 @@ export async function listNestedRepository(directory: string): Promise path !== '') } - -async function runGit(directory: string, args: string[]): Promise<{exitCode: number; stdout: string} | undefined> { - try { - const result = await captureOutputWithExitCode('git', args, {cwd: directory}) - return {exitCode: result.exitCode, stdout: result.stdout} - // eslint-disable-next-line no-catch-all/no-catch-all - } catch { - return undefined - } -} diff --git a/packages/app/src/cli/services/app-security-engine/tests/dependency-automation.test.ts b/packages/app/src/cli/services/app-security-engine/tests/dependency-automation.test.ts index d8e076d0b28..ab2f07190db 100644 --- a/packages/app/src/cli/services/app-security-engine/tests/dependency-automation.test.ts +++ b/packages/app/src/cli/services/app-security-engine/tests/dependency-automation.test.ts @@ -143,9 +143,10 @@ describe('dependency automation scanner integration', () => { expect(securityExitCode({...execution, elapsedMilliseconds: 0}, 'low')).toBe(0) expect(JSON.stringify(execution.deterministicFindings)).not.toContain('local>org/renovate-config') // Outside any repository, gathering stops after asking whether the directory is ignored and probing for a repository. + const untrusted = ['-c', 'core.fsmonitor=', '-c', 'safe.bareRepository=explicit'] expect(vi.mocked(captureOutputWithExitCode).mock.calls.map(([command, args]) => [command, args])).toEqual([ - ['git', ['check-ignore', '--no-index', '-q', '--', basename(root)]], - ['git', ['rev-parse', '--is-inside-work-tree']], + ['git', [...untrusted, 'check-ignore', '--no-index', '-q', '--', basename(root)]], + ['git', [...untrusted, 'rev-parse', '--is-inside-work-tree']], ]) expect(fetch).not.toHaveBeenCalled() }) diff --git a/packages/app/src/cli/services/app-security-engine/tests/untrusted-repository.test.ts b/packages/app/src/cli/services/app-security-engine/tests/untrusted-repository.test.ts new file mode 100644 index 00000000000..49dfcc30344 --- /dev/null +++ b/packages/app/src/cli/services/app-security-engine/tests/untrusted-repository.test.ts @@ -0,0 +1,194 @@ +/* eslint-disable no-restricted-imports -- the scanned repositories are real temporary git repositories */ +import {git, isolateGitConfig} from './git-test-helpers.js' +import {scanDirectory as scan} from './scan-directory.js' +import {UNTRUSTED_REPOSITORY_PROTECTIONS} from '../scanners/git.js' +import {afterEach, beforeEach, describe, expect, test} from 'vitest' +import {spawnSync} from 'node:child_process' +import {chmodSync, existsSync, mkdirSync, mkdtempSync, realpathSync, rmSync, writeFileSync} from 'node:fs' +import {tmpdir} from 'node:os' +import {join} from 'node:path' +import type {GitProtection, GitProtectionName} from '../scanners/git.js' + +/** + * A scanned repository can name programs for Git to run. Every fixture is first shown to run its program under + * plain Git, so a fixture that has quietly stopped being dangerous fails instead of passing. + */ + +const temporaryDirectories: string[] = [] +let restoreGitConfig: (() => void) | undefined + +beforeEach(() => { + restoreGitConfig = isolateGitConfig() +}) + +afterEach(() => { + restoreGitConfig?.() + for (const directory of temporaryDirectories.splice(0)) rmSync(directory, {recursive: true, force: true}) +}) + +function makeDirectory(): string { + const directory = realpathSync(mkdtempSync(join(tmpdir(), 'app-security-untrusted-'))) + temporaryDirectories.push(directory) + return directory +} + +function writeFiles(root: string, files: Record): void { + for (const [path, content] of Object.entries(files)) { + const fullPath = join(root, path) + mkdirSync(join(fullPath, '..'), {recursive: true}) + writeFileSync(fullPath, content) + } +} + +// Assembled at runtime so secret scanners pointed at this repository don't flag a literal. +const environmentSecret = () => `SHOPIFY_API_SECRET=${['shp', 'ss_', '0123456789abcdef'.repeat(2)].join('')}\n` + +const appFiles = () => ({ + 'shopify.app.toml': 'name = "untrusted"\nclient_id = "abc123"\napplication_url = "https://example.com"\n', + '.env': environmentSecret(), +}) + +/** A shell command Git would run for the repository; it only records that it ran. Git for Windows runs it in sh. */ +function recordingCommand(marker: string, label: string): string { + return `echo ${label} >> '${marker.replace(/\\/g, '/')}'; false` +} + +/** A repository, the program it plants, and a Git command that runs that program. */ +interface Fixture { + directory: string + marker: string + probe: string[] +} + +/** A repository delivered with its `.git`, whose own configuration names a file-system monitor. */ +function deliveredWithGitDirectory(): Fixture { + const directory = makeDirectory() + const marker = join(makeDirectory(), 'ran') + git(directory, ['init', '-q', '.']) + writeFiles(directory, appFiles()) + git(directory, ['add', 'shopify.app.toml']) + git(directory, ['commit', '-qm', 'init']) + git(directory, ['config', 'core.fsmonitor', recordingCommand(marker, 'fsmonitor')]) + return {directory, marker, probe: ['ls-files', '--cached']} +} + +/** A bare repository committed as ordinary files, which a plain clone checks out. */ +function committedBareRepository(): Fixture { + const parent = makeDirectory() + const marker = join(makeDirectory(), 'ran') + const published = join(parent, 'published') + mkdirSync(published) + git(published, ['init', '-q', '.']) + writeFiles(published, { + 'README.md': 'A starter.\n', + ...Object.fromEntries(Object.entries(appFiles()).map(([path, content]) => [`app/${path}`, content])), + // None of these is named .git, so Git tracks them. + 'app/HEAD': 'ref: refs/heads/main\n', + 'app/config': `[core]\n\trepositoryformatversion = 0\n\tbare = false\n\tworktree = .\n\tfsmonitor = "${recordingCommand(marker, 'fsmonitor')}"\n`, + 'app/objects/info/packs': '', + 'app/refs/heads/.keep': '', + }) + git(published, ['add', '-A']) + git(published, ['commit', '-qm', 'init']) + git(parent, ['clone', '-q', published, 'clone']) + return {directory: join(parent, 'clone', 'app'), marker, probe: ['ls-files', '--cached']} +} + +/** A partial clone missing a skip-worktree .gitignore, which Git would fetch through the named transport command. */ +function partialCloneMissingObject(): Fixture { + const directory = makeDirectory() + const marker = join(makeDirectory(), 'ran') + git(directory, ['init', '-q', '.']) + writeFiles(directory, {...appFiles(), 'web/.gitignore': 'build/\n', 'web/.env': environmentSecret()}) + git(directory, ['add', 'shopify.app.toml', 'web/.gitignore']) + git(directory, ['commit', '-qm', 'init']) + const blob = git(directory, ['rev-parse', ':web/.gitignore']).trim() + git(directory, ['update-index', '--skip-worktree', 'web/.gitignore']) + rmSync(join(directory, 'web', '.gitignore')) + const object = join(directory, '.git', 'objects', blob.slice(0, 2), blob.slice(2)) + // Git writes objects read-only, which Windows won't delete. + chmodSync(object, 0o644) + rmSync(object) + git(directory, ['config', 'core.repositoryformatversion', '1']) + git(directory, ['config', 'extensions.partialClone', 'origin']) + git(directory, ['config', 'remote.origin.url', 'ssh://example.invalid/app']) + git(directory, ['config', 'remote.origin.promisor', 'true']) + git(directory, ['config', 'core.sshCommand', recordingCommand(marker, 'ssh')]) + return {directory, marker, probe: ['ls-files', '-z', '--others', '--ignored', '--exclude-standard', '--directory']} +} + +/** Runs Git without the engine; a protection may make it refuse, so the exit status isn't checked. */ +function plainGit(fixture: Fixture, protection: GitProtection = {}): void { + spawnSync('git', [...(protection.args ?? []), ...fixture.probe], { + cwd: fixture.directory, + env: {...process.env, ...protection.env}, + stdio: 'ignore', + }) +} + +function expectPlainGitRunsProgram(fixture: Fixture): void { + plainGit(fixture) + expect(existsSync(fixture.marker)).toBe(true) + rmSync(fixture.marker) +} + +function committedSecret(result: Awaited>) { + return result.issues.find((issue) => issue.id === 'COMMITTED_SECRET') +} + +// Typed by protection name, so a protection added without a fixture fails type-checking. +const fixtureFor: Record Fixture> = { + fileSystemMonitor: deliveredWithGitDirectory, + bareRepository: committedBareRepository, + lazyFetch: partialCloneMissingObject, + transports: partialCloneMissingObject, +} + +describe('each untrusted repository protection, on its own', () => { + test.each(Object.keys(fixtureFor) as GitProtectionName[])('%s stops the program its fixture plants', (name) => { + const fixture = fixtureFor[name]() + expectPlainGitRunsProgram(fixture) + + plainGit(fixture, UNTRUSTED_REPOSITORY_PROTECTIONS[name]) + + expect(existsSync(fixture.marker)).toBe(false) + }) +}) + +describe('scanning an untrusted repository', () => { + test('does not run the file-system monitor named by a repository delivered with its .git', async () => { + const fixture = deliveredWithGitDirectory() + expectPlainGitRunsProgram(fixture) + + const result = await scan(fixture.directory) + + expect(existsSync(fixture.marker)).toBe(false) + // Git still answers: the secret is confirmed untracked and not ignored. + expect(committedSecret(result)?.detection_evidence).toEqual([ + 'git ls-files --error-unmatch .env → untracked', + 'git check-ignore -q .env → not ignored', + ]) + }) + + test('refuses a bare repository committed as ordinary files and carried by a clone', async () => { + const fixture = committedBareRepository() + expectPlainGitRunsProgram(fixture) + + const result = await scan(fixture.directory) + + expect(existsSync(fixture.marker)).toBe(false) + // Git refuses the committed repository rather than answering from it, so the secret stays reported. + expect(committedSecret(result)?.detection_evidence).toEqual([ + 'git rev-parse --is-inside-work-tree → not a work tree', + ]) + }) + + test('does not fetch a missing object through the transport command a partial clone names', async () => { + const fixture = partialCloneMissingObject() + expectPlainGitRunsProgram(fixture) + + await scan(fixture.directory) + + expect(existsSync(fixture.marker)).toBe(false) + }) +})