dominikmifkovic/detoxss/1.0.0

Unavailable. Nobody can download this version any more, with elm or with the Lamdera compiler: github.com/dominikmifkovic/detoxss is gone (deleted or private); Software Heritage has not archived the repo; no fork with the same repo name found; no cached copy. Use another version.

Details

DetoXSS.Port

JSON decoders and encoders for values crossing Elm ports.

Ports are a trust boundary between Elm and JavaScript. Values received through ports should be treated as untrusted. This module provides capped decoders for HTML-like values, attribute values, URL values, and lists of sanitized HTML values.

The analyzed decoders combine port decoding with DetoXSS.Ast classification and an application-level analysis policy.

type Inbound
= InHtml SafeHtml
| InAttr ValidatedInput
| InUrl UrlResult
| InHtmlList (List SafeHtml)

Values that can be received through the generic inbound port decoder.

The constructors distinguish sanitized HTML-like values, validated attribute values, URL validation results, and lists of sanitized HTML-like values.

type alias UrlResult =
{ ok : Bool
, value : ValidatedInput
}

Result of validating a URL-like value from a port.

ok says whether the value was accepted. value contains either the accepted URL or a safe fallback.

= AllowAfterSanitize
| RejectDangerous
| RejectSuspiciousAndDangerous

Policy used by analyzed port decoders.

AllowAfterSanitize always allows the value after sanitization. RejectDangerous rejects values classified as Dangerous. RejectSuspiciousAndDangerous rejects values classified as Suspicious or Dangerous.

type alias HtmlResult =
{ ok : Bool
, risk : Risk
, value : SafeHtml
, reason : String
}

Result of analyzing and sanitizing an HTML-like value.

The record contains whether the value was accepted, the AST risk level, the sanitized value, and a human-readable reason.

inboundDecoder : State -> Decoder Inbound

Decode an inbound JSON value from a port.

The decoder expects an object with a type field and a value field. The type field decides whether the value is treated as HTML-like content, attribute content, URL content, or a list of HTML-like values.

Decode inbound port data and apply AST analysis for HTML-like values.

The provided policy controls whether suspicious or dangerous values are accepted or rejected. Non-HTML values are decoded using the same rules as inboundDecoder.

rawDecoderCapped : Decoder RawInput

Decode a raw string with length limiting.

The decoder fails if the value exceeds maxChars.

validatedAttrDecoder : Decoder ValidatedInput

Decode an attribute-like string and wrap it as validated input.

safeHtmlDecoderW : State -> Decoder SafeHtml

Decode an HTML-like string and sanitize it with a whitelist state.

safeHtmlDecoderAnalyzedW : AnalysisPolicy -> State -> Decoder SafeHtml

Decode a single HTML-like string with whitelist sanitization and AST policy.

If the policy rejects the detected risk level, decoding fails.

safeHtmlResultDecoderW : AnalysisPolicy -> State -> Decoder HtmlResult

Decode a single HTML-like string and return an analysis result.

Unlike safeHtmlDecoderAnalyzedW, this decoder returns a structured HtmlResult containing the risk and rejection reason instead of only returning the sanitized value.

safeHtmlListDecoderW : State -> Decoder (List SafeHtml)

Decode a list of HTML-like strings and sanitize every item.

The decoder fails if the list exceeds maxListLen.

safeHtmlListDecoderAnalyzedW : AnalysisPolicy -> State -> Decoder (List SafeHtml)

Decode a list of HTML-like strings with whitelist sanitization and AST policy.

If any item is rejected by the policy, the whole decoder fails.

urlDecoderW : State -> Decoder UrlResult

Decode a URL-like string and validate it with whitelist URL schemes.

analyzeRawHtml : String -> Risk

Analyze and sanitize a raw HTML-like string.

This helper is useful when an application already has a raw string and wants to apply the same AST policy and whitelist sanitization logic used by the analyzed port decoders.

encodeRisk : Risk -> Value

Encode an AST risk value as a JSON string.

The output is one of "safe", "suspicious", or "dangerous".

encodeHtmlResult : HtmlResult -> Value

Encode an HtmlResult as JSON.

This is useful for sending analysis results back to JavaScript through an outbound port.

encodeSafeHtml : SafeHtml -> Value

Encode sanitized HTML-like content for an outbound port.

encodeSafeHtmlList : List SafeHtml -> Value

Encode a list of sanitized HTML-like values for an outbound port.

encodeValidated : ValidatedInput -> Value

Encode validated input for an outbound port.

encodeValidatedList : List ValidatedInput -> Value

Encode a list of validated input values for an outbound port.

encodeUrlResult : UrlResult -> Value

Encode a URL validation result for an outbound port.

maxChars : Int

Maximum number of characters accepted by capped port decoders.

Maximum number of items accepted by list decoders.

normalizeNewlines : String -> String

Normalize newline characters.

This converts Windows and old Mac line endings to \n.

stripControl : String -> String

Remove control characters from a string.

Newline, carriage return, and tab characters are preserved.