@cjtecho/cjshield
v0.2.1
Published
Policy-driven request security for Node.js: XSS, HTML sanitization, SQL injection, command injection and path traversal detection with Express and Fastify integrations.
Maintainers
Readme
@cjtecho/cjshield
Policy-driven request security for Node.js, with Express and Fastify integrations.
CJShield inspects incoming HTTP requests for XSS, SQL injection, command injection and path traversal, sanitizes the HTML fields you declare, and blocks or reports suspicious requests according to a JSON policy.
CJShield is an additional defensive layer. It does not replace parameterized queries, safe process APIs, output encoding or safe file-path handling, and it does not provide complete protection against any attack category.
Installation
npm install @cjtecho/cjshieldRequires Node.js 22.12 or later. Express (4.22+ or 5.2+) and Fastify (5.12.6+) are optional peer dependencies: install the framework you use.
Express
import express from 'express';
import { createShield, loadPreset } from '@cjtecho/cjshield';
import { cjshield } from '@cjtecho/cjshield/express';
const app = express();
const shield = createShield({ policy: loadPreset('default') });
app.use(express.json()); // body parsers first
app.use(cjshield({ shield })); // then CJShield, before your routes
app.post('/comments', (req, res) => {
res.json({ saved: req.body });
});Fastify
import Fastify from 'fastify';
import { createShield, loadPreset } from '@cjtecho/cjshield';
import { cjshield } from '@cjtecho/cjshield/fastify';
const app = Fastify();
const shield = createShield({ policy: loadPreset('default') });
app.addHook('preValidation', cjshield({ shield }));
app.post('/comments', async (request) => ({ saved: request.body }));What happens to each request
| Result | What CJShield does |
| --------------------------------- | ---------------------------------------------------------------------------------------------- |
| Nothing suspicious | The request continues unchanged. |
| Suspicious, policy says monitor | The request continues; the finding is reported to your onEvent callback. |
| Suspicious, policy says block | CJShield responds 403 {"error":"Request blocked","requestId":"…"} and your route never runs. |
| HTML field declared in the policy | The field is sanitized before your route sees it. |
Responses never contain the payload or detection details, and events never contain request values, passwords, tokens or session cookies.
To log what CJShield finds, pass a callback:
cjshield({ shield, onEvent: (event) => logger.info(event) });Policies
Start from a preset:
| Preset | Use it for |
| --------- | ------------------------------------------------------------------------------------------- |
| monitor | Rollout: reports findings, never blocks. Start here to check for false positives. |
| default | Blocks high-confidence command injection and path traversal; reports XSS and SQL injection. |
| strict | Blocks high-confidence findings for every detector, and fails closed on internal errors. |
const shield = createShield({ policy: loadPreset('monitor') });For your own policy (per-route rules, HTML fields, exceptions, limits), write a JSON file and
load it with await loadPolicyFile('./cjshield.policy.json'). The format is described by the
policy schema, which ships in the package as @cjtecho/cjshield/spec/policy.v1.schema.json.
Rule IDs (for events and bypassRules exceptions) are listed in
@cjtecho/cjshield/spec/rules.v1.json.
Security
Please report vulnerabilities privately, as described in SECURITY.md in the package.
Support
For support, queries, or feedback: Email: [email protected]
License
Proprietary. Free to install and use in your applications; copying, modifying,
redistributing and reverse engineering are not permitted. See LICENSE in the package.
