codefence 1.0.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,299 @@
1
+ # CodeFence
2
+
3
+ **A tiny offline policy gate for code.**
4
+
5
+ Check AI-generated code before it reaches Git.
6
+
7
+ ---
8
+
9
+ ## What it does
10
+
11
+ CodeFence is a single-file, zero-dependency Python CLI that scans
12
+ source code for selected dangerous patterns before you commit. It
13
+ runs a curated set of rules and reports findings with:
14
+
15
+ - severity (critical / high / medium / low / info)
16
+ - exact location (file:line:column)
17
+ - code snippet
18
+ - short remediation
19
+ - typical before/after fix example
20
+
21
+ It is designed around the risks commonly encountered in AI-assisted
22
+ development, but scans ordinary source code regardless of how it
23
+ was written.
24
+
25
+ **The gate workflow:**
26
+
27
+ AI writes code
28
+ |
29
+ v
30
+ CodeFence runs on staged files
31
+ |
32
+ v
33
+ policy evaluation (allow / warn / block)
34
+ |
35
+ v
36
+ PASS or BLOCKED
37
+ |
38
+ v
39
+ git commit
40
+
41
+ ## What it does NOT do
42
+
43
+ - Not an autofixer. It shows a typical fix; it does not edit files.
44
+ - Not a security audit. It is a pattern-based sanity checker.
45
+ - Not a replacement for Semgrep, CodeQL, or professional review.
46
+ - Not interprocedural. No dataflow, no taint analysis.
47
+ - Not a guarantee. A clean scan does not mean the code is secure.
48
+
49
+ ## Install
50
+
51
+ pip install codefence
52
+
53
+ Or run it directly:
54
+
55
+ python3 codefence.py app.py
56
+
57
+ ## Quick start
58
+
59
+ **One-time setup in a git repository:**
60
+
61
+ codefence init
62
+
63
+ This installs a pre-commit hook and creates .codefence/config.json.
64
+ Every commit is then gated automatically.
65
+
66
+ **Daily usage:**
67
+
68
+ codefence --staged # scan only what is staged
69
+ codefence --staged --diff # only NEW findings since baseline
70
+ codefence baseline # snapshot current findings
71
+
72
+ **One-off scans:**
73
+
74
+ codefence src/
75
+ codefence --format json --output report.json src/
76
+ codefence --format html --output report.html src/
77
+ codefence --format sarif --output results.sarif .
78
+
79
+ **Policy-as-code:**
80
+
81
+ codefence --policy company-policy.json --staged
82
+ codefence policy validate company-policy.json
83
+
84
+ **Explanation:**
85
+
86
+ codefence explain R002
87
+ ## The 30 rules
88
+
89
+ Rules are shipped in rules.json (human-readable JSON). You can
90
+ inspect them, and you can add or disable rules for your own use.
91
+
92
+ ### Secrets
93
+ - R001 - Hardcoded API keys / secrets
94
+ - R011 - Hardcoded database credentials
95
+ - R028 - Env var with hardcoded fallback secret
96
+
97
+ ### Injection
98
+ - R002 - SQL injection via string concatenation (Python)
99
+ - R003 - eval() / exec() on untrusted input (Python)
100
+ - R004 - Command injection via shell=True (Python)
101
+ - R005 - innerHTML with unsanitized data (JavaScript)
102
+ - R010 - Path traversal
103
+ - R014 - Prototype pollution (JavaScript)
104
+ - R019 - Unsafe deserialization (pickle, yaml.load)
105
+
106
+ ### Authentication
107
+ - R006 - Missing authentication on endpoints
108
+ - R015 - Missing rate limiting on public endpoints
109
+ - R018 - Open redirect
110
+ - R020 - Never-expiring tokens / JWT
111
+
112
+ ### Cryptography
113
+ - R008 - Insecure random for security tokens (Python)
114
+ - R009 - Disabled TLS/SSL verification
115
+ - R016 - MD5 / SHA1 for password hashing
116
+
117
+ ### Configuration
118
+ - R007 - Overly permissive CORS (*)
119
+ - R017 - Debug mode left enabled
120
+ - R021 - Missing security headers (Express)
121
+
122
+ ### Reliability
123
+ - R022 - Race conditions (check-then-act)
124
+ - R026 - HTTP request without timeout
125
+ - R027 - Naive datetime (no timezone, context-aware)
126
+
127
+ ### Quality
128
+ - R012 - Broad except: pass
129
+ - R013 - Mutable default arguments
130
+ - R023 - Logging sensitive data
131
+ - R024 - Unused imports / dead code
132
+ - R025 - Promise rejection ignored (JavaScript)
133
+
134
+ ### AI-specific
135
+ - R030 - Typosquatted package imports
136
+ - R031 - TODO / FIXME / HACK in new code
137
+
138
+ Each rule carries a confidence level and a stable rule_version +
139
+ fingerprint for baseline stability.
140
+
141
+ ## Configuration
142
+
143
+ Options can be passed as CLI flags or through a JSON config file
144
+ (auto-discovered at .codefence/config.json after 'codefence init').
145
+
146
+ Example .codefence/config.json:
147
+
148
+ {
149
+ "schema": "codefence/config-v1",
150
+ "format": "json",
151
+ "severity": "medium",
152
+ "include": ["*.py", "*.js"],
153
+ "exclude": ["node_modules", ".git", "venv", "samples"],
154
+ "max_size": 2097152,
155
+ "cache": false
156
+ }
157
+
158
+ Priority: CLI flags > config file > built-in defaults.
159
+
160
+ ### Optional cache
161
+
162
+ Pass --cache to enable a local cache at ~/.cache/codefence/. The
163
+ cache is keyed by SHA-256 of the file content plus a fingerprint
164
+ of rules.json. It is off by default. See SECURITY.md for how the
165
+ cache is protected.
166
+
167
+ ### Optional history
168
+
169
+ Pass --history to record this scan in a local SQLite database at
170
+ ~/.local/share/codefence/history.db. Off by default.
171
+
172
+ Set CODEFENCE_NO_HISTORY=1 to disable history entirely, even when
173
+ --history is set.
174
+
175
+ Query history:
176
+
177
+ codefence history
178
+ codefence stats
179
+ ## Security and privacy
180
+
181
+ - Zero network calls. Zero.
182
+ - Zero telemetry.
183
+ - Zero data upload.
184
+ - Writes only to --output, and optionally to:
185
+ ~/.cache/codefence/ (only when --cache is set)
186
+ ~/.local/share/codefence/ (only when --history is set)
187
+ - Never writes to the files you scan.
188
+ - Never executes the code you scan.
189
+
190
+ The full threat model is documented in SECURITY.md.
191
+
192
+ ## Verifying the download
193
+
194
+ Every shipped file is hashed in CHECKSUMS.txt. To verify:
195
+
196
+ sha256sum codefence.py rules.json
197
+
198
+ Compare the output with the corresponding lines in CHECKSUMS.txt.
199
+ If the hashes do not match, do not use the file.
200
+
201
+ ## Payment and pricing
202
+
203
+ **$12 USD, one-time.**
204
+
205
+ Includes the current major version (v1.x) and its maintenance
206
+ releases. No subscription. No support. No account.
207
+
208
+ Payments are available in cryptocurrency only:
209
+
210
+ - USDT (TRC20 or BEP20)
211
+ - USDC
212
+ - BTC
213
+ - TRX
214
+ - XRP
215
+
216
+ Official purchase channels:
217
+
218
+ - Getly
219
+ - SilkRoadx402
220
+ - ctlx.cc
221
+
222
+ If you find CodeFence on any other site claiming to sell it,
223
+ treat that site as unofficial.
224
+
225
+ ## License
226
+
227
+ Source-available. Not open source.
228
+
229
+ Summary:
230
+
231
+ - Personal use on up to 3 devices that you own or control.
232
+ - Read, study, and privately modify the source.
233
+ - No redistribution. No resale. No bundling.
234
+ - No scanning-as-a-service on a commercial basis.
235
+
236
+ For plain-language answers to common questions, see LICENSE_FAQ.md.
237
+ For the full legal terms, see LICENSE.txt and TERMS_OF_USE.md.
238
+
239
+ **Governing law:** England and Wales. Mandatory consumer
240
+ protections in your country of residence remain fully applicable.
241
+
242
+ ## Support
243
+
244
+ There is no support.
245
+
246
+ - No email. No chat. No issue tracker.
247
+ - No guaranteed updates.
248
+ - No bug-fix commitments.
249
+
250
+ If you need a product with ongoing support, please look elsewhere.
251
+
252
+ ## FAQ
253
+
254
+ **Q: Will this find every security issue in my code?**
255
+ A: No. It finds a curated set of dangerous patterns. It is a
256
+ sanity check, not a security audit.
257
+
258
+ **Q: Can I use it in CI?**
259
+ A: Yes. JSON and SARIF outputs are pure on stdout. Exit codes are
260
+ stable.
261
+
262
+ **Q: Does it phone home?**
263
+ A: No. Zero network calls. You can verify by reading the source
264
+ or by running it under strace.
265
+
266
+ **Q: Does it fix my code?**
267
+ A: No. It shows a typical before/after fix. You apply it yourself.
268
+
269
+ **Q: Does it support TypeScript?**
270
+ A: No. Python, JavaScript (.js, .mjs, .cjs).
271
+
272
+ **Q: What about false positives?**
273
+ A: Pattern-based scanners always produce some. Rules with lower
274
+ confidence are flagged as such. You can disable any rule in
275
+ rules.json or via policy overrides.
276
+
277
+ **Q: Can I modify the source?**
278
+ A: Yes, privately. See LICENSE_FAQ.md.
279
+
280
+ **Q: Can my team use it?**
281
+ A: Each developer using CodeFence on their own machine needs
282
+ their own license. One license covers one individual on up to
283
+ three devices.
284
+
285
+ **Q: How do I get updates?**
286
+ A: There are no guaranteed updates.
287
+
288
+ **Q: Can I get a refund?**
289
+ A: See REFUND_POLICY.md.
290
+
291
+ ## A note on honesty
292
+
293
+ This product is built to do a specific, limited thing well. It
294
+ does not overstate what it can do. Every claim in this README is
295
+ traceable to a line of code or a test. If you find a claim that
296
+ is not supported by what the tool actually does, please report
297
+ it.
298
+
299
+ *End of README.*
@@ -0,0 +1,131 @@
1
+ # Refund Policy
2
+
3
+ **CodeFence — v1.0.0**
4
+ Effective 2026-09-16
5
+
6
+ ---
7
+
8
+ ## Plain summary
9
+
10
+ - Digital product, delivered instantly as a downloadable file.
11
+ - **EU / UK consumers:** 14-day right of withdrawal, **if the file
12
+ has not been downloaded**.
13
+ - Once downloaded, the withdrawal right is waived, as permitted by
14
+ EU Consumer Rights Directive 2011/83/EU, Article 16(m).
15
+ - Statutory consumer protections always apply and are never waived.
16
+
17
+ ---
18
+
19
+ ## 1. Right of Withdrawal (EU and UK consumers)
20
+
21
+ If you are a consumer resident in the European Union or the United
22
+ Kingdom, you have the right to withdraw from your purchase within
23
+ **fourteen (14) days** of the transaction date, **without giving any
24
+ reason**, **provided that you have not downloaded the digital
25
+ content**.
26
+
27
+ To exercise this right, you must:
28
+
29
+ 1. Contact the platform through which you purchased the Software
30
+ (Getly, SilkRoadx402, or ctlx.cc) before the 14-day period
31
+ expires.
32
+ 2. State clearly your intention to withdraw.
33
+ 3. Not have downloaded the Software file.
34
+
35
+ The refund will be processed via the same payment method used for
36
+ the purchase, within a reasonable time.
37
+
38
+ ### Waiver after download
39
+
40
+ By downloading the Software, you expressly agree and acknowledge
41
+ that:
42
+
43
+ - The digital content has been supplied to you.
44
+ - You lose your right of withdrawal with respect to the downloaded
45
+ content.
46
+
47
+ This waiver is permitted under Article 16(m) of Directive 2011/83/EU
48
+ and equivalent UK law (Consumer Contracts Regulations 2013).
49
+
50
+ **If you have not downloaded the Software and want a refund, do not
51
+ download the file first.**
52
+
53
+ ---
54
+
55
+ ## 2. Conformity Guarantee
56
+
57
+ If the Software does not conform to its description at the time of
58
+ purchase, you may be entitled to a remedy under applicable EU/UK
59
+ consumer protection law. This includes, where applicable:
60
+
61
+ - repair or replacement of the non-conforming digital content,
62
+ - a proportionate price reduction,
63
+ - termination of the contract and full refund.
64
+
65
+ This right is separate from the 14-day withdrawal right and is not
66
+ limited by it.
67
+
68
+ ---
69
+
70
+ ## 3. Non-Refundable Cases (after download)
71
+
72
+ After the file has been downloaded, refunds are **not provided**
73
+ for:
74
+
75
+ - Change of mind.
76
+ - The Software did not detect a specific issue you hoped it would
77
+ detect. (The Software is a pattern-based checker; its scope is
78
+ documented in `README.md` and `SECURITY.md`.)
79
+ - False positives or false negatives produced by the pattern rules.
80
+ - Incompatibility with your specific environment that is outside the
81
+ documented supported platforms.
82
+ - The Software's stated limitations (no autofix, no TypeScript, no
83
+ interprocedural analysis, etc.). These limitations are documented
84
+ publicly before purchase.
85
+ - Unwillingness to use the Software after reading its documentation.
86
+
87
+ ---
88
+
89
+ ## 4. Fraud and Abuse
90
+
91
+ Refunds will not be processed if:
92
+
93
+ - The purchase was made with a fraudulent payment method.
94
+ - The Software was redistributed or resold in violation of
95
+ `LICENSE.txt`.
96
+ - The refund request is part of a pattern of abuse.
97
+
98
+ ---
99
+
100
+ ## 5. Statutory Rights
101
+
102
+ Nothing in this policy limits any statutory right or remedy available
103
+ to you under mandatory consumer protection law in your country of
104
+ residence. If any part of this policy conflicts with such mandatory
105
+ law, the mandatory law prevails.
106
+
107
+ ---
108
+
109
+ ## 6. How to Request a Refund
110
+
111
+ Refund requests must be made through the platform where the purchase
112
+ was made:
113
+
114
+ - **Getly** — use the platform's refund mechanism.
115
+ - **SilkRoadx402** — use the platform's dispute mechanism.
116
+ - **ctlx.cc** — use the platform's refund mechanism.
117
+
118
+ There is no direct email or contact channel for refund requests. This
119
+ is intentional and consistent with the product's stated design.
120
+
121
+ ---
122
+
123
+ ## 7. Timeframe
124
+
125
+ Refund processing times depend on the platform and the payment
126
+ network. Cryptocurrency refunds are typically processed within a few
127
+ business days once approved by the platform.
128
+
129
+ ---
130
+
131
+ *End of Refund Policy.*
@@ -0,0 +1,142 @@
1
+ # Security Model — CodeFence
2
+
3
+ This document describes, honestly and completely, what this tool can
4
+ and cannot do with respect to security. It is written for users and
5
+ security reviewers.
6
+
7
+ ## What this tool is
8
+
9
+ CodeFence is a **pattern-based sanity checker**. It reads source
10
+ files, runs pre-compiled regular expressions and a bounded AST walk,
11
+ and reports findings. It is a *helper*, not a security guarantee.
12
+
13
+ ## Threat model
14
+
15
+ ### What the tool does NOT do
16
+
17
+ - It does **not** execute the scanned code.
18
+ - It does **not** import user modules.
19
+ - It does **not** make network connections.
20
+ - It does **not** send telemetry or data anywhere.
21
+ - It does **not** write into user source files.
22
+ - It does **not** create or modify anything outside:
23
+ - stdout, or the file passed to `--output`
24
+ - (optionally, if `--cache` is enabled) `~/.cache/codefence/`
25
+ - (optionally, if `--history` is enabled) `~/.local/share/codefence/`
26
+
27
+ ### Attack surfaces and how they are handled
28
+
29
+ 1. **Malicious `rules.json`**
30
+ - Schema is validated on load: rule IDs must match `R###`, severity
31
+ and language values must be from the allowed sets, regex patterns
32
+ are compiled eagerly and rejected on error.
33
+ - Per-pattern length is capped (`MAX_REGEX_PATTERN_LEN` = 500).
34
+ - Per-rule pattern count is capped (`MAX_REGEX_PATTERNS_PER_RULE` = 20).
35
+ - Every regex call is run line-by-line, with a hard per-call timeout
36
+ (`REGEX_TIMEOUT_SEC` = 0.5 s) and per-line cap
37
+ (`MAX_REGEX_LINE_LEN` = 8192 chars). This bounds catastrophic
38
+ backtracking (ReDoS).
39
+
40
+ 2. **Malicious source file**
41
+ - File size is capped by `--max-size` (default 2 MiB).
42
+ - Files are read as text with `errors="replace"`.
43
+ - Python files are parsed by CPython's own `ast.parse`. The AST
44
+ parser is memory-safe in supported Python versions.
45
+ - JavaScript files are tokenized by our own lexer, written in pure
46
+ Python. It never executes input.
47
+
48
+ 3. **Cache poisoning (only when `--cache` is used)**
49
+ - Cache directory is created with mode `0o700`.
50
+ - Cache files are created with mode `0o600` via `mkstemp` +
51
+ `os.replace` (atomic).
52
+ - Cache entries are keyed by SHA-256 of (version, language, rules
53
+ fingerprint, file content). A modified file produces a different
54
+ key.
55
+ - On load, the cache file is checked: must be a regular file (no
56
+ symlinks), JSON must parse, and each finding must validate. Any
57
+ failure results in a silent cache miss and a fresh scan.
58
+ - Cache never contains executable content; it is plain JSON.
59
+
60
+ 4. **History database (only when `--history` is used)**
61
+ - SQLite database at `~/.local/share/codefence/history.db`.
62
+ - Directory mode `0o700`, file mode `0o600`.
63
+ - Contains: scan timestamp, tool version, file count, finding
64
+ counts, rule IDs, file paths, line numbers, and message text.
65
+ - Contains **no source code** and **no secret values**. Only
66
+ metadata about scans.
67
+ - Disable with `--history` omitted (default) or with
68
+ `CODEFENCE_NO_HISTORY=1` in the environment.
69
+ - Delete the directory at any time to clear history.
70
+
71
+ 5. **Distribution tampering**
72
+ - Published package includes `CHECKSUMS.txt` with SHA-256 of every
73
+ shipped file. Users are advised to verify after download.
74
+
75
+ ## Platform limitations and safe fallbacks
76
+
77
+ - **Signal-based regex timeout.** On Unix-like systems and the main
78
+ thread, CodeFence enforces a hard 0.5-second timeout on every
79
+ regex call. On platforms where signals are unavailable (Windows)
80
+ or when running in a non-main thread (e.g. an async worker), the
81
+ timeout cannot be enforced by the OS. In that case CodeFence:
82
+ 1. Prints a one-time warning to stderr.
83
+ 2. Falls back to a strict input cap of 1024 characters per regex
84
+ call.
85
+ 3. Continues the scan; results may be less complete on very long
86
+ lines.
87
+ This behavior is intentional: silent failure of ReDoS protection
88
+ would be worse than a visible warning.
89
+
90
+ - **Baseline fingerprint includes line and column.** A finding's
91
+ fingerprint is derived from rule ID, file path, line, column, and
92
+ normalized snippet text. This means:
93
+ - Two findings of the same rule on different lines are distinct
94
+ (correct behavior — no collision).
95
+ - If code shifts up or down within a file (e.g. after adding
96
+ lines above), existing findings may appear as "new" in
97
+ `--diff`. This is intentional: catching new instances of a
98
+ dangerous pattern matters more than avoiding re-reporting a
99
+ shifted one.
100
+ - To suppress a known false positive permanently, use
101
+ `# noqa: RXXX` on that line, which is not affected by line
102
+ shifts.
103
+
104
+ - **Policy self-modification.** A pull request can modify
105
+ `.codefence/policy.json` in the same commit as the code it
106
+ affects. This allows a PR to weaken its own gate. CodeFence
107
+ prints a warning when the policy or config file is staged in the
108
+ current commit, but does not block. Review policy changes
109
+ carefully in code review.
110
+
111
+ ## Supply chain
112
+
113
+ This tool has **zero third-party dependencies**. It only uses modules
114
+ from the Python standard library (`argparse`, `ast`, `hashlib`, `html`,
115
+ `json`, `os`, `re`, `signal`, `stat`, `sys`, `tempfile`, `time`,
116
+ `dataclasses`, `datetime`, `enum`, `pathlib`, `typing`,
117
+ `unicodedata`).
118
+
119
+ There is no `pip install`, no `npm install`, no vendored code, no
120
+ build step, and no runtime download.
121
+
122
+ ## Reporting a security issue
123
+
124
+ This is a one-time, as-is product. There is no official support
125
+ channel. If you find a vulnerability you believe is worth sharing,
126
+ please publish it responsibly — the maintainers do not operate a
127
+ private disclosure inbox.
128
+
129
+ ## Honest limitations
130
+
131
+ - A clean scan **does not mean the code is secure**.
132
+ - The tool detects a curated set of dangerous **patterns**, not
133
+ vulnerabilities in general.
134
+ - The tool does not perform dataflow or taint analysis.
135
+ - False positives and false negatives are inherent to the approach.
136
+ - Do not rely on this tool as the only security check before shipping
137
+ code. It is designed to be a fast, offline, first-pass filter.
138
+
139
+ ## License of the security model
140
+
141
+ This document is provided for transparency. It is not a warranty.
142
+ See `TERMS_OF_USE.md` for the legal terms.