DEV Community

RESK
RESK

Posted on

Build a Bitmask-Based LLM Security Firewall with reskSecure

Links:


Most LLM safety approaches filter output text after generation. By then, the harmful token has already been sampled and inference resources wasted.

reskSecure flips this: it intercepts at the logits level, before token selection. Using a capability bitmask system, it makes dangerous tokens statistically impossible to generate.

Quick Start

pip install resksecure
Enter fullscreen mode Exit fullscreen mode

Basic Usage

Create a policy file policy.yaml:

rules:
  - name: block-sql-injection
    severity: HARD
    patterns:
      - "DROP TABLE"
      - "UNION SELECT"
      - "OR 1=1"
  - name: discourage-pii
    severity: BIAS
    penalty: -5.0
    patterns:
      - "credit card"
      - "social security"
Enter fullscreen mode Exit fullscreen mode

Integrate with your Python application:

from resksecure import Firewall

fw = Firewall.from_yaml("policy.yaml")
fw.start_watcher()  # hot-reload on file change

# In your inference loop:
def secure_generate(prompt: str, model, tokenizer):
    logits = model.forward(prompt)
    adjusted = fw.process_logits(logits, prompt)
    return sample_from(adjusted)
Enter fullscreen mode Exit fullscreen mode

Key Architecture

reskSecure operates on a capability bitmask. Each rule assigns a bit position. When a prompt or tool call activates a bit, the corresponding token receives a penalty determined by the severity mode:

  • HARD mode: logit set to -infinity. The model can never pick that token.
  • BIAS mode: configurable negative penalty. The token can still be selected if context strongly warrants it.

Bitmasks compose with AND/OR logic, enabling complex policies from simple building blocks.

Hot-Reload in Production

The YAML policy watcher monitors your policy file. Change rules on the fly with zero downtime:

fw.start_watcher(interval=5.0)  # check every 5 seconds
Enter fullscreen mode Exit fullscreen mode

Why Logits-Level?

Output filtering is reactive. Logits-level filtering is preventive:

  • Tool calls are blocked at the first token — the model can never start a disallowed function signature
  • No post-generation parsing overhead
  • Compatible with any sampling strategy

Check it out on GitHub or PyPI and let me know what security patterns you would block.

https://github.com/Resk-Security/reskSecure
https://pypi.org/project/resksecure
https://resk.fr

Top comments (0)