feat(python-sdk): @c_rule / @cpp_rule decorators with language scoping#678
Open
shivasurya wants to merge 2 commits intoshiva/cpp-cfg-scanfrom
Open
feat(python-sdk): @c_rule / @cpp_rule decorators with language scoping#678shivasurya wants to merge 2 commits intoshiva/cpp-cfg-scanfrom
shivasurya wants to merge 2 commits intoshiva/cpp-cfg-scanfrom
Conversation
Mirrors the @go_rule contract for C and C++ security rules. The decorators
inject language="c"/"cpp" into dataflow IR so DataflowExecutor scopes
flows() to the right language; pure calls() rules remain language-agnostic
(same Gap 1 / Gap 4 documented contract as @go_rule).
- codepathfinder.{c,cpp}_decorators with metadata dataclasses, atexit
auto-output, registry helpers, and clear_*_rules for test isolation.
- codepathfinder.{c,cpp}_ir compilers emit JSON IR with the language tag
in both rule metadata and matcher dict.
- python-sdk/rules/ shims preserve the existing import path style.
- dsl/loader.go decorator detector now recognises @c_rule / @cpp_rule
alongside @go_rule for early file filtering.
- Unit tests cover registration, metadata, language injection contract
(dataflow vs call_matcher), JSON serialisation, and registry isolation
between the C and C++ registries.
Verified end-to-end against tiny C/C++ smoke projects, sglang
(337 C++ functions, 9.6k call sites) and proxygen (11.8k C++ functions,
66k call sites): @c_rule / @cpp_rule rule files load via the DSL loader,
rules execute on parsed C/C++ functions, and detections include correct
file/line/class-qualified function names.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
SafeDep Report SummaryNo dependency changes detected. Nothing to scan. This report is generated by SafeDep Github App |
Code Pathfinder Security ScanNo security issues detected.
Powered by Code Pathfinder |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## shiva/cpp-cfg-scan #678 +/- ##
===================================================
Coverage 85.40% 85.40%
===================================================
Files 187 187
Lines 27276 27278 +2
===================================================
+ Hits 23296 23298 +2
Misses 3087 3087
Partials 893 893 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.



Summary
Adds C and C++ rule decorators that mirror the existing
@go_rule/@python_rulecontract. Rules can now be authored with@c_rule(...)/@cpp_rule(...); the decorators injectlanguage="c"/language="cpp"into dataflow IR soDataflowExecutorscopesflows()rules to the right language. Purecalls()matchers stay language-agnostic — same documented contract as@go_rule.This is the SDK glue half of the C/C++ language support track. Example security rules will land in a follow-up PR.
What's in this PR
codepathfinder.{c,cpp}_decorators—@c_rule/@cpp_rulewith full metadata (id/name/severity/cwe/cve/owasp/tags/message), atexit auto-output forpython3 rule.pysmoke runs, andclear_*_rules()helpers for test isolation.codepathfinder.{c,cpp}_ir—compile_c_rules()/compile_cpp_rules()emit JSON IR with"language"in both rule metadata and matcher dict.python-sdk/rules/shims preserve the existing import path style (from rules.c_decorators import c_rulekeeps working).dsl/loader.godecorator detector now recognises@c_rule(and@cpp_rule(so the loader can early-filter rule files like it does for@go_rule(.Validation
End-to-end smoke runs (binary + locally-installed
pip install -e python-sdk):sgl-kernel/csrc/cpuHTTP1xCodec::generateBody,CAresResolver::Query::queryCallback,TestAsyncTransport::WriteEvent::newEvent,H3DatagramAsyncSocket::deliverDatagram.*memcpy) correctly match qualified C++ calls (std::memcpy) — same contract as@go_rule.Test plan
python -m pytest python-sdk/tests/— 441 passedgo test ./sast-engine/dsl/— passgolangci-lint run ./dsl/...— 0 issuesgradle buildGo— clean@c_rule+calls()) produces detections with correct file/line/function@cpp_rulerules load and detect, function FQNs accurate