Metadata-Version: 2.4
Name: cobraphp
Version: 0.1.0.dev3
Summary: Placeholder for the Python interface of CobraPHP, a static taint analyzer for PHP projects.
License-Expression: GPL-2.0-only
Requires-Python: >=3.11
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: cpg2py>=1.2.0
Requires-Dist: php-parser-py>=2.0.3
Requires-Dist: semgrep==1.85.0
Requires-Dist: setuptools<82
Dynamic: license-file

# bindings-python-cobraphp

Python interface of [CobraPHP](https://github.com/jhu-seclab-cobra/CobraPHP), the Cobra static taint analyzer for PHP web applications. The package `cobraphp` returns plain Python data: dataclasses, enums, `pathlib.Path` and built-in containers.

## Status

**Placeholder.** The Python binding is not fully implemented. Interfaces and results may change between versions.

The subpackage `cobraphp.analysis` builds a call graph and a data flow from a PHP project parsed with [php-parser-py](https://pypi.org/project/php-parser-py/). The version is a pre-release. The name follows the pattern `bindings-<language>-<analyzer>`.

## Install

From a checkout, with [uv](https://docs.astral.sh/uv/):

```
uv sync
```

`uv sync` installs the package, its dependencies at the versions of `uv.lock` and the check tools. Run commands through `uv run` so that the programs the dependencies install are on `PATH`.

The planned release wheel will bundle the CobraPHP jar. Which Java runtime it uses is undecided ([open gaps](docs/design.md#open-gaps)). Python 3.11 or later.

## Examples

Analyze a project and read the result:

```python
from pathlib import Path
from cobraphp import AnalysisConfig, Finding, Unanalyzed, analyze_project

outputs = analyze_project(Path("/srv/project"), AnalysisConfig(context_depth=3))
for output in outputs:
    print(output)  # a Finding prints its dataclass; an Unanalyzed prints `file:line function(): reason`
complete = not any(isinstance(o, Unanalyzed) for o in outputs)
findings = [o for o in outputs if isinstance(o, Finding)]
```

Build once, analyze several times, and export the graphs:

```python
from dataclasses import replace
from cobraphp import build_project

with build_project(Path("/srv/project")) as built:
    more = built.analyze(replace(built.config, extra_paths_per_sink=3))
    built.export_graphs(Path("graphs.jsonl"))
```

Build the call graph and the data flow of a project with `cobraphp.analysis`:

```python
from pathlib import Path
from php_parser_py import PHPVersion, parse_project
from cobraphp.analysis import USER_INPUT, backward_slice, build_call_graph, build_data_flow, make_app_file_filter

root = Path("/srv/project")
ast = parse_project(str(root), target_php=PHPVersion(8, 5), file_filter=make_app_file_filter(root))
calls = [e for e in build_call_graph(ast).edges if e.type == "CCG_SITE_CALL"]
flow = build_data_flow(ast, timeout_seconds=600)
for node_id in flow.tainted[USER_INPUT]:
    print(node_id, backward_slice(flow, node_id, max_depth=3))
```

Apply the same file exclusions outside `parse_project`:

```python
from cobraphp.analysis import app_file_filter, framework_excluded_dirs

framework_dirs = framework_excluded_dirs(root.resolve())
app_files = [p for p in root.resolve().rglob("*.php")
             if app_file_filter(p) and not any(p.is_relative_to(d) for d in framework_dirs)]
```

Handle a failure:

```python
from cobraphp import ProjectNotFoundError

try:
    analyze_project(Path("/missing"))
except ProjectNotFoundError as error:
    print(error)
```

## Design

[docs/design.md](docs/design.md) states the types, functions, errors, lifecycle and open items. Companions: [mapping to Kotlin](docs/design-mapping.md), [backend boundary](docs/design-backend.md), [graph access](docs/design-graphs.md), [extension points](docs/design-extension.md), [query languages](docs/design-queries.md) and [analysis interface](docs/design-analysis-interface.md), the last four proposed, and [static analysis subpackage](docs/design-analysis-subpackage.md). The implementation plan is [docs/todo.md](docs/todo.md). Terms are those of the [glossary](docs/glossary.md).

## Checks

```
python -m compileall src
python -m pytest
python -m mypy
```

Run them inside the environment, for example as `uv run python -m pytest`. The tests marked `integration` run an external program; `python -m pytest -m "not integration"` skips them.

## License

GPL-2.0-only, the licence of CobraPHP; see [LICENSE](LICENSE).
