This repository has been archived on 2026-11-17. You can view files and clone it, but cannot push or open issues or pull requests.
gitgram-ai/aitimemachine/gitnexus/test/unit/taint/python-model-match.test.ts
caelum0x 49b183818c Unify gitgram + aitimemachine into the monorepo (de-submodule) and merge vendored engines
Convert gitgram/ and aitimemachine/ from embedded git repos (which showed as
opaque submodule gitlinks on GitHub) into fully-tracked first-party content.

AITM now merges its 7 vendored repos as first-party engines:
- memory: beads (Go task DAG) + context7 (docs SDK), unified into saved_contexts
- git: vendored C git (bundled binary) + gitnexus (in-process code intelligence)
- agentic: a2a (@a2a-js/sdk) + pal (AITM-managed Python subprocess)

Includes the full session's product build: Gitgram web/mobile/backend features,
AITM website/CLI/MCP, the unified memory layer, engine build tooling
(scripts/build-engines.sh, Dockerfile, CI), docs (README, ENGINES.md, ARCHITECTURE.md),
and extensive test suites.

Build artifacts, node_modules, external/, and gemini-cli are gitignored.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-23 14:14:09 +03:00

163 lines
4.9 KiB
TypeScript

/**
* Python taint model (#2204) over real Python CFG and import capture output.
*/
import Python from 'tree-sitter-python';
import type { ParsedImport } from 'gitnexus-shared';
import { describe, expect, it } from 'vitest';
import { createPythonCfgVisitor } from '../../../src/core/ingestion/cfg/visitors/python.js';
import { emitPythonScopeCaptures } from '../../../src/core/ingestion/languages/python/captures.js';
import { interpretPythonImport } from '../../../src/core/ingestion/languages/python/interpret.js';
import { PYTHON_TAINT_MODEL } from '../../../src/core/ingestion/taint/python-model.js';
import { hasTaintSafeSites } from '../../../src/core/ingestion/taint/site-safety.js';
import {
buildTaintImportIndex,
matchFunctionSites,
type FunctionSiteMatches,
type MatchedSinkCall,
type MatchedSourceRead,
} from '../../../src/core/ingestion/taint/match.js';
import { makeCfgHarness } from '../../helpers/cfg-harness.js';
const harness = makeCfgHarness(Python, createPythonCfgVisitor(), 'fixture.py');
function importsFor(src: string): ParsedImport[] {
return emitPythonScopeCaptures(src, 'fixture.py')
.filter((m) => m['@import.statement'] !== undefined)
.map((m) => interpretPythonImport(m))
.filter((p): p is ParsedImport => p !== null);
}
function matchesOf(code: string, fnIndex = 0): FunctionSiteMatches {
const cfg = harness.cfgOf(code, fnIndex);
expect(hasTaintSafeSites(cfg)).toBe(true);
return matchFunctionSites(cfg, PYTHON_TAINT_MODEL, buildTaintImportIndex(importsFor(code)));
}
const allSinks = (m: FunctionSiteMatches): MatchedSinkCall[] =>
m.statements.flatMap((s) => [...s.sinks]);
const allSources = (m: FunctionSiteMatches): MatchedSourceRead[] =>
m.statements.flatMap((s) => [...s.sources]);
describe('Python taint model (#2204)', () => {
it('matches Flask/FastAPI-style request member reads as remote-input sources', () => {
const m = matchesOf(`
def f(request, req, other):
a = request.args
b = req.json
c = other.args
`);
expect(allSources(m).map((s) => s.entry.kind)).toEqual(['remote-input', 'remote-input']);
expect(m.hasSource).toBe(true);
});
it('resolves named stdlib imports for command sinks', () => {
const m = matchesOf(`
from os import system
def f(request):
system(request.args)
`);
const sinks = allSinks(m);
expect(sinks).toHaveLength(1);
expect(sinks[0].entry.kind).toBe('command-injection');
expect(sinks[0].entry.name).toBe('system');
expect(allSources(m)).toHaveLength(1);
});
it('resolves namespace stdlib imports for subprocess sinks', () => {
const m = matchesOf(`
import subprocess as sp
def f(request):
sp.run(request.args)
`);
expect(allSinks(m).map((s) => s.entry.name)).toEqual(['run']);
});
it('does not guess positional sink slots for keyword arguments', () => {
const m = matchesOf(`
import subprocess as sp
def f(request):
sp.run(shell=request.args, args="safe")
`);
expect(allSinks(m)).toHaveLength(0);
expect(allSources(m)).toHaveLength(1);
});
it('matches global code and path sinks at argument position zero', () => {
const m = matchesOf(`
def f(request):
eval(request.args)
exec(request.form)
open(request.path_params)
`);
const sinks = allSinks(m);
expect(sinks.map((s) => s.entry.kind)).toEqual([
'code-injection',
'code-injection',
'path-traversal',
]);
expect(sinks.map((s) => [...s.argPositions])).toEqual([[0], [0], [0]]);
expect(allSources(m)).toHaveLength(3);
});
it('matches conventional database calls at argument position zero', () => {
const m = matchesOf(`
def f(request, cursor, db):
cursor.execute(request.args)
db.query(request.form)
cursor.executemany(request.json)
cursor.executescript(request.data)
`);
const sinks = allSinks(m);
expect(sinks.map((s) => s.entry.name)).toEqual([
'execute',
'query',
'executemany',
'executescript',
]);
expect(sinks.map((s) => [...s.argPositions])).toEqual([[0], [0], [0], [0]]);
expect(allSources(m)).toHaveLength(4);
});
it('does not match a locally shadowed stdlib sink name', () => {
const m = matchesOf(`
from os import system
def f(request):
def system(value):
return value
system(request.args)
`);
expect(allSinks(m)).toHaveLength(0);
expect(allSources(m)).toHaveLength(1);
});
it('does not let methods in a nested class shadow an enclosing import', () => {
const m = matchesOf(`
from os import system
def f(request):
class Helpers:
def system(self, value):
return value
system(request.args)
`);
expect(allSinks(m)).toHaveLength(1);
expect(allSources(m)).toHaveLength(1);
});
it('treats a nested class declaration as a local sink-name binding', () => {
const m = matchesOf(`
def f(request):
class open:
pass
open(request.args)
`);
expect(allSinks(m)).toHaveLength(0);
expect(allSources(m)).toHaveLength(1);
});
});