Package sk.iway.iwcm.doc.showdoc
Class NonceHelper
java.lang.Object
sk.iway.iwcm.doc.showdoc.NonceHelper
Helper class for Content-Security-Policy (CSP) nonce functionality.
Handles nonce injection into HTML tags, inline style/event handler migration,
and CSP configuration parsing.
-
Method Summary
Modifier and TypeMethodDescriptionstatic StringinjectCspNonceIntoTags(String htmlContent, String nonce, boolean injectIntoScripts, boolean injectIntoStyles) Injects a CSP nonce into<script>,<style>, and<link rel="stylesheet">tags in a single pass.static booleanisDirectiveAllowsUnsafeInline(String cspValue, String directiveName) Checks if a specific CSP directive allows 'unsafe-inline'.static StringprocessInlineEventHandlers(String htmlContent, String nonce) Processes inline event handlers by replacing them with data attributes and injecting JavaScript with nonce.static StringprocessInlineStyles(String htmlContent, String nonce) Processes inline styles by replacing them with data attributes and injecting CSS rules with nonce.protected static StringprocessTagForNonce(String tagContent, String nonce, Pattern scriptPattern, Pattern stylePattern, Pattern linkPattern, boolean injectIntoScripts, boolean injectIntoStyles) Processes a single tag (script, style, or link) to inject nonce if not already present.
-
Method Details
-
injectCspNonceIntoTags
public static String injectCspNonceIntoTags(String htmlContent, String nonce, boolean injectIntoScripts, boolean injectIntoStyles) Injects a CSP nonce into<script>,<style>, and<link rel="stylesheet">tags in a single pass. Optimized for memory efficiency: uses single StringBuilder, processes all tag types in one pass.- Parameters:
htmlContent- The HTML content to processnonce- The CSP nonce valueinjectIntoScripts- Whether to inject nonce into<script>tags (false if script-src allows unsafe-inline)injectIntoStyles- Whether to inject nonce into<style>and<link>tags (false if style-src allows unsafe-inline)- Returns:
- HTML content with nonce injected into eligible tags
-
processTagForNonce
protected static String processTagForNonce(String tagContent, String nonce, Pattern scriptPattern, Pattern stylePattern, Pattern linkPattern, boolean injectIntoScripts, boolean injectIntoStyles) Processes a single tag (script, style, or link) to inject nonce if not already present. Skips injection for tag types whose corresponding CSP directive allows unsafe-inline.- Parameters:
tagContent- The matched tag stringnonce- The CSP nonce valuescriptPattern- Pre-compiled pattern for script tagsstylePattern- Pre-compiled pattern for style tagslinkPattern- Pre-compiled pattern for link tagsinjectIntoScripts- Whether to inject nonce into script tagsinjectIntoStyles- Whether to inject nonce into style/link tags- Returns:
- Processed tag string
-
processInlineStyles
Processes inline styles by replacing them with data attributes and injecting CSS rules with nonce. For elements with inline style="...", replaces with data-inline-style="counter" and generates CSS rules like [data-inline-style="1"] { property: value !important; }.- Parameters:
htmlContent- The HTML contentnonce- The CSP nonce- Returns:
- Processed HTML with inline styles replaced and CSS injected
-
processInlineEventHandlers
Processes inline event handlers by replacing them with data attributes and injecting JavaScript with nonce. For elements with inline event handlers (onclick, onmouseover, etc.), replaces with data-inline-onclick="counter" and generates JavaScript code to restore the handlers.- Parameters:
htmlContent- The HTML contentnonce- The CSP nonce- Returns:
- Processed HTML with inline event handlers replaced and JavaScript injected
-
isDirectiveAllowsUnsafeInline
Checks if a specific CSP directive allows 'unsafe-inline'. Parses using indexOf to find the directive start and end (next ';' or end of string), then checks if 'unsafe-inline' exists within that range.- Parameters:
cspValue- The full CSP configuration stringdirectiveName- The directive name to check (e.g., "script-src", "style-src")- Returns:
- true if the directive allows 'unsafe-inline'
-