mhndayesh commited on
Commit
e475aff
Β·
verified Β·
1 Parent(s): 0326284

Upload README.md with huggingface_hub

Browse files
Files changed (1) hide show
  1. README.md +66 -100
README.md CHANGED
@@ -14,125 +14,91 @@ tags:
14
  - secure-coding
15
  - appsec
16
  - cwe
17
- - retrieval
18
- - edge
19
  ---
20
 
21
  # gemma-4-E2B-security-expert (GGUF)
22
 
23
- **A ~2B edge model (`gemma-4-E2B-it`) that writes *secure code by default* β€” via an
24
- application-security *FactBank* baked into its chat-template.** When you ask for code, a searchable
25
- bank of security "landmine facts" fires **inside the inference engine** and steers the model off the
26
- insecure-by-default pattern it would otherwise emit. **Weights are untouched**; there is no external
27
- RAG service, no fine-tuning. Runs on a laptop.
28
 
29
- > A stock 2B model writes insecure code about half the time β€” so do much larger models. The bank
30
- > supplies the *security knowledge* the weights lack, so the small model behaves like one taught to
31
- > code securely. On the results below, this 2B **beats DeepSeek-V4** at secure-code generation.
32
 
33
- > πŸ”— **Full project β€” all experts, methodology, transcripts, benchmarks:**
34
- > **[github.com/mhndayesh/experts-models](https://github.com/mhndayesh/experts-models)**
 
 
 
35
 
36
- ## What it does
37
- Given a normal coding request, it silently corrects the common insecure defaults, e.g.:
38
-
39
- | you ask for… | base 2B tends to write | **this model writes** |
40
- |---|---|---|
41
- | load a PyTorch checkpoint | `torch.load(path)` | `torch.load(path, weights_only=True)` *(post-cutoff default)* |
42
- | parse an XML file | `etree.parse(f)` | `XMLParser(resolve_entities=False, no_network=True)` *(XXE)* |
43
- | load a YAML config | `yaml.load(...)` | `yaml.safe_load(...)` |
44
- | generate a token / nonce | `random.random()` | `secrets.token_hex()` / `os.urandom` |
45
- | a DB credential | `password = "hunter2"` | `os.environ.get("DB_PASSWORD")` |
46
- | run a SQL query with user input | string-concatenated SQL | parameterized query |
47
- | evaluate a user expression | `eval(...)` | `ast.literal_eval(...)` |
48
- | compare an HMAC signature | `a == b` | constant-time compare |
49
-
50
- You **don't have to name the vulnerability** β€” the bank is indexed by the *coding task*
51
- ("verify a signed URL", "load a model", "parse untrusted XML"), so it fires on benign prompts.
52
-
53
- ## Coverage β€” what's in the bank
54
- - **258 security concepts** (backed by the **MITRE CWE** taxonomy) β†’ **3,984 language/framework
55
- variants** β€” the same weakness written the way it actually appears in each ecosystem. (This bank is
56
- organized by **weakness/CWE**, not by a short library list β€” it spans the whole secure-coding surface.)
57
- - **10+ languages:** Python, Java, JavaScript, C / C++, Swift, Go, C#, Rust, Ruby, and more.
58
- - **Frameworks & platforms:** Android, iOS / SwiftUI / WKWebView, Flask, Django, Express, Node.js,
59
- ASP.NET / .NET, Spring, Java EE, Rails, Laravel.
60
- - **Mined from 7 permissive security sources:** MITRE **CWE**, GitHub **CodeQL** queries, SAST rules
61
- (**Bandit** + **gosec**), OWASP **MASTG** (mobile), **RustSec** advisories, **NIST / RFC / Mozilla**
62
- crypto-&-network guidance, and **OWASP**. Every fact was quote-grounded to its source, then put
63
- through an **adversarial correctness audit** (a second model tried to refute each fact; ~3.8%
64
- wrong/stale were removed β€” grounding proves a fact is *real*, the audit checks it's *right*).
65
-
66
- ## Benchmarks β€” hand-scored (the project rule is to read outputs, not trust a scorer)
67
-
68
- ### External benchmark: SecurityEval (s2e-lab, MSR 2022)
69
- 121 Python CWE-tagged insecure-by-default function-completion tasks; complete each stub securely.
70
-
71
- | arm | secure (pattern-checkable subset, 28 tasks) |
72
- |---|---|
73
- | e2b **base** (2B) | 14 / 28 |
74
- | **e2b + this bank** | **25 / 28 (+11, 0 regressions)** |
75
 
76
- Standout: **XXE (CWE-611) 0/6 β†’ 6/6**, plus wins on deserialization, hardcoded creds, code-injection,
77
- and weak-random. (The rest of the 121 are rarer CWEs / cases no pattern resolves; no covered CWE
78
- regressed.)
 
 
 
 
79
 
80
- ### Vs. a strong cloud model β€” same 24 tasks, identical scoring
81
- The cloud baseline is **DeepSeek-V4 (`deepseek-v4-flash`)**, run on the exact same prompts.
 
 
 
82
 
83
- | model | secure |
 
 
 
 
 
 
 
 
 
 
 
 
84
  |---|---|
85
- | e2b base (2B) | 12 / 24 |
86
- | **DeepSeek-V4 (`deepseek-v4-flash`, cloud)** | 13 / 24 |
87
- | **e2b + this bank (2B)** | **22 / 24** |
88
 
89
- **The 2B + bank *strictly dominates* the cloud model** on this subset: secure on **9 tasks where
90
- DeepSeek is not**, and secure on **every task DeepSeek got right** (0 losses). Scale doesn't supply
91
- this knowledge; the bank does.
92
-
93
- > **Honest caveats.** Hand-scored on the pattern-checkable subset (the rest weren't machine-judgeable).
94
- > `deepseek-v4-flash` is a strong, large cloud model but not literally GPT-5 / Claude-Opus (those
95
- > weren't callable in the harness). Numbers are a secure-code delta, not a security guarantee.
96
 
97
  ## How to run
98
 
99
- ### llama.cpp / `llama-server` β€” recommended, full bank (the bank is embedded in this GGUF)
 
 
 
 
 
 
100
  ```bash
101
- llama-server -m gemma-4-E2B-security-expert-Q4_K_M.gguf --jinja --port 8080 --ctx-size 8192
 
 
 
102
  ```
103
- Then query normally β€” the bank fires automatically for security-relevant requests. Exposes an
104
- **OpenAI-compatible** API at `http://localhost:8080/v1`, so any OpenAI SDK / server client works
105
- unchanged. **Sampling β€” Gemma-native:** `temperature 1.0, top_k 64, top_p 0.95, min_p 0.01` (the
106
- `min_p` floor prevents reasoning-loop empty answers). Don't quantize the KV cache on a model this
107
- small. Ships **thinking-OFF** β€” the correct config for a baked expert (chain-of-thought reverts a
108
- habit-reversal fact to the model's trained prior).
109
-
110
- ### LM Studio β€” use the Hub model (delivers the full bank)
111
- The bank template is **4.18 MB**, over LM Studio's ~980 KB raw-GGUF metadata cap β€” so if you load
112
- *this GGUF* directly in LM Studio the template is silently truncated and the bank won't fire. For LM
113
- Studio, install the **virtual-model** edition, which hands the full template to LM Studio's own engine
114
- (no cap):
115
-
116
- **➑ https://lmstudio.ai/mhndayesh/gemma-4-e2b-security-expert** β€” open it and click *"Use Model in
117
- LM Studio"* (or `lms get mhndayesh/gemma-4-e2b-security-expert`).
118
-
119
- Both editions carry the **same** bank; only the delivery differs. This GGUF is for llama.cpp and
120
- OpenAI-compatible servers; the Hub edition is for LM Studio.
121
 
122
  ## Limitations
123
- - **Scoped to application-security / secure coding.** Outside that, it's the base model.
124
- - **Supplies knowledge, not reasoning.** A 2B still misses some multi-step fixes (e.g. stripping
125
- newlines for log-injection).
126
- - **Keyword + faceted retrieval, not embeddings.** A wholly unexpected phrasing can miss; there is no
127
- "I don't know" signal β€” a miss looks like a normal answer.
128
- - **Grounded & audited, but research-grade** β€” review generated code; this is an assistant, not a
129
- substitute for review.
130
- - Hand-scored security tests, not a general coding benchmark.
131
 
132
  ## Provenance & license
133
- - **Base:** `lmstudio-community/gemma-4-E2B-it-GGUF` (Q4_K_M, weights untouched). This model = that
134
- GGUF with `tokenizer.chat_template` rewritten to embed an inverted-index retriever + the bank
135
- (`factbank.version 0.4.0`). The standalone template ships alongside as `chat-template.jinja`.
136
- - **License:** Google **Gemma Terms of Use** (`license: gemma`) β€” a gemma-4 derivative. The fact bank
137
- is FactBank-project content (repo `LICENSE`); mined sources keep their own licenses.
138
  - **Source & method:** [github.com/mhndayesh/experts-models](https://github.com/mhndayesh/experts-models).
 
14
  - secure-coding
15
  - appsec
16
  - cwe
17
+ - reasoning
 
18
  ---
19
 
20
  # gemma-4-E2B-security-expert (GGUF)
21
 
22
+ **A ~2B (E2B) model that writes *secure code by default* β€” an application-security *FactBank* is baked into
23
+ its chat-template and fires **inside the inference engine** to steer the model off insecure-by-default
24
+ patterns.** Weights untouched; no external RAG. This repo ships **two editions** (see below).
 
 
25
 
26
+ > πŸ”— **Full project:** [github.com/mhndayesh/experts-models](https://github.com/mhndayesh/experts-models)
 
 
27
 
28
+ ## Two editions β€” pick one (files in this repo)
29
+ | edition | file | when to use | notes |
30
+ |---|---|---|---|
31
+ | **thinking-OFF** (default) | `gemma-4-E2B-security-expert-Q4_K_M.gguf` | production, speed, reliability | 0 empty answers, fast, secure. No reasoning trace. **The safe default.** |
32
+ | **thinking-ON** | `gemma-4-E2B-security-expert-thinking-Q4_K_M.gguf` | you want a visible reasoning trace | Shows its work and is strong on most tasks, **but ~10% of the hardest-reasoning prompts can run long and return a *blank* answer** (fails safe β€” it does not emit insecure code; just retry). Needs a large context + generous output budget. |
33
 
34
+ Both editions carry the **same** bank. They differ only in whether chain-of-thought is on. We ship both so
35
+ you know exactly what you're getting β€” a landmine fact *reverses a trained habit*, and with thinking on the
36
+ model sometimes over-reasons back toward its prior, so the thinking edition trades some reliability for a
37
+ reasoning trace. Thinking-ON has a **known llama.cpp Gemma-4 fix applied** (the generation prompt opens the
38
+ thought channel) + **strong authority framing** so facts hold; the residual ~10% is inherent to reasoning-on.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
39
 
40
+ ## Settings that work (baked into the Hub editions; set these yourself on llama.cpp)
41
+ - **Sampling (gemma-native):** `temperature 1.0, top_k 64, top_p 0.95, min_p 0.01`. The **`min_p 0.01`** floor
42
+ is important β€” it stops the low-temperature repetition loop that otherwise yields empty answers.
43
+ - **Context length:** **β‰₯ 16k, 32k recommended** (`contextLength 32768` is baked). The bank injects ~3,400
44
+ tokens per query, so a small context truncates the answer β€” especially with thinking on.
45
+ - **Output budget (thinking-ON):** allow **generous `max_tokens`** (reasoning traces run ~2–3k tokens); too
46
+ small a cap truncates before the answer.
47
 
48
+ ## What it does
49
+ Given a normal coding request it silently corrects insecure defaults, e.g. `torch.load(weights_only=True)`
50
+ (post-cutoff), XXE `resolve_entities=False`, `yaml.safe_load`, `secrets` over `random`, `os.environ` creds,
51
+ parameterized SQL, `ast.literal_eval` over `eval`, constant-time HMAC compare. You **don't name the
52
+ vulnerability** β€” the bank is indexed by the coding task, so it fires on benign prompts.
53
 
54
+ ## Coverage β€” what's in the bank
55
+ - **258 CWE concepts β†’ 3,984 language/framework variants** (organized by weakness, not a short lib list).
56
+ - **10+ languages:** Python, Java, JavaScript, C/C++, Swift, Go, C#, Rust, Ruby, and more.
57
+ - **Frameworks:** Android, iOS/SwiftUI/WKWebView, Flask, Django, Express, Node.js, ASP.NET/.NET, Spring,
58
+ Java EE, Rails, Laravel.
59
+ - **7 mined sources:** MITRE **CWE**, GitHub **CodeQL**, SAST (**Bandit**+**gosec**), OWASP **MASTG**,
60
+ **RustSec**, **NIST/RFC/Mozilla** crypto-net, **OWASP** β€” each fact quote-grounded then adversarially
61
+ correctness-audited (~3.8% wrong/stale removed).
62
+
63
+ ## Benchmarks (E2B, hand-scored β€” read the outputs, don't trust a scorer)
64
+ External **SecurityEval** (s2e-lab, MSR 2022, 121 Python CWE tasks), pattern-checkable subset:
65
+
66
+ | arm | secure |
67
  |---|---|
68
+ | base (2B) | 14/28 |
69
+ | **+ this bank** | **25/28 (+11, 0 regressions; XXE 0/6β†’6/6)** |
 
70
 
71
+ And vs a strong cloud model on the same 24 tasks: base 12 Β· **DeepSeek-V4 (`deepseek-v4-flash`) 13** Β·
72
+ **+bank 22** β€” the 2B+bank *strictly dominates* the cloud model (9 wins, 0 losses). These are FactBank
73
+ retrieval measurements; the shipped GGUFs bake the same bank in-engine.
 
 
 
 
74
 
75
  ## How to run
76
 
77
+ ### LM Studio (Hub editions β€” the full bank is delivered via model.yaml)
78
+ The bank template is 4.2 MB β€” over LM Studio's ~980 KB raw-GGUF cap, so for LM Studio use the **Hub virtual
79
+ models** (they hand the full template to the engine, no cap), which also carry the settings above:
80
+ - **thinking-OFF:** https://lmstudio.ai/mhndayesh/gemma-4-e2b-security-expert
81
+ - **thinking-ON:** https://lmstudio.ai/mhndayesh/gemma-4-e2b-security-expert-thinking
82
+
83
+ ### llama.cpp / any OpenAI-compatible server (this GGUF, full bank embedded)
84
  ```bash
85
+ # thinking-OFF (recommended)
86
+ llama-server -m gemma-4-E2B-security-expert-Q4_K_M.gguf --jinja --ctx-size 32768 --port 8080
87
+ # thinking-ON
88
+ llama-server -m gemma-4-E2B-security-expert-thinking-Q4_K_M.gguf --jinja --ctx-size 32768 --port 8080
89
  ```
90
+ Sampling: `temperature 1.0, top_k 64, top_p 0.95, min_p 0.01`. Don't quantize the KV cache on small models.
91
+ Exposes an OpenAI-compatible API at `/v1`.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
92
 
93
  ## Limitations
94
+ - **Scoped to secure coding.** Outside it, base model.
95
+ - **Supplies knowledge, not reasoning** β€” still misses some multi-step fixes.
96
+ - **thinking-ON:** ~10% of hard prompts run long / blank (fails safe). Use thinking-OFF for reliability.
97
+ - **Keyword + faceted retrieval, not embeddings** β€” an unusual phrasing can miss. Research-grade; review output.
 
 
 
 
98
 
99
  ## Provenance & license
100
+ - **Base:** `lmstudio-community/gemma-4-E2B-it-GGUF` (weights untouched). Only `tokenizer.chat_template` is rewritten to embed an
101
+ inverted-index retriever + the bank (`factbank.version 0.4.0`).
102
+ - **License:** Google **Gemma Terms of Use** (`license: gemma`). Fact bank = FactBank-project content; mined
103
+ sources keep their own licenses.
 
104
  - **Source & method:** [github.com/mhndayesh/experts-models](https://github.com/mhndayesh/experts-models).