This document covers StreetJS’s built-in security features and best practices.
Authentication
JWT Tokens
StreetJS’s JwtService uses HMAC-SHA256 by default with configurable expiry.
1
2
3
4
5
import{JwtService}from'streetjs';constjwt=newJwtService(process.env.JWT_SECRET,{expiresIn:'1h'});consttoken=jwt.sign({userId:'123',roles:['user']});constpayload=jwt.verify(token);// throws if invalid or expired
JWT secrets must be at least 256 bits (32 bytes). Set via JWT_SECRET environment variable.
Refresh Tokens
The RefreshTokenService implements single-use refresh tokens with replay detection.
1
2
3
4
5
import{RefreshTokenService}from'streetjs';consttokenService=newRefreshTokenService({pool,jwtService});const{accessToken,refreshToken}=awaittokenService.issue(userId);constrefreshed=awaittokenService.refresh(refreshToken);// throws TokenReplayError on reuse
Audit logs use an HMAC-SHA256 hash chain to detect tampering.
XSS Protection
1
2
3
4
5
6
import{xssMiddleware,sanitizeString}from'streetjs';app.use(xssMiddleware);// Sanitizes all string body fields// Manual sanitizationconstsafe=sanitizeString(userInput);
Security Checklist
Set JWT_SECRET to a cryptographically random 256-bit value
Set KEK for field-level encryption
Set SESSION_KEY for session signing
Enable HTTPS in production
Configure ALLOWED_ORIGINS for CORS
Enable rate limiting on public endpoints
Enable CSRF protection for state-changing endpoints
Review @Classify annotations on all entity fields
Run npm audit --audit-level=high before each release