The __safeEncode__ function is a context-aware security encoder engineered to neutralize user input before rendering it in potentially vulnerable output states. It is critical for defending against injection vulnerabilities such as XSS, SQLi, and Command Injection.
import { __safeEncode__ } from "strulink";
const userInput = "<script>alert('pwned')</script>";
const safeHtml = __safeEncode__(userInput, "html");function __safeEncode__(
input: string,
context: RWA_TYPES,
options?: {
doubleEncode?: boolean;
encodeSpaces?: boolean;
preserveNewlines?: boolean;
},
): string;Proper defense requires matching the encoding context (context parameter) effectively with where the input will be rendered:
html: Defenses against Standard XSS payload in HTML bodies viahtmlEntityencode.htmlAttr: ApplieshtmlEntitycontext and forcefully replaces double quotes globally.js/jsString: Designed explicitly for escaping JavaScript strings.jsStringenforces Unicode representations for maximum reliability.css/cssSelector: Exits CSS contexts smoothly.cssSelectorspecifically defends:and..url/urlParam: AppliespercentEncodingorurlSafeBase64to prepare input for HTTP transfers.command: Ensures variables injected into shell arguments bypass critical operators[&;'"\\|*?~<>^()[]{}$\n\r\t#]`.xml/json: Framework-level compatibility conversions for XML properties and API-ready representations.
doubleEncode: Recommendedtrueif WAF systems or backend frameworks natively trigger a decode sequentially before analysis.preserveNewlines: Retains the format specifically designed for standardemailoperations.