Write me a pure function that takes a sanitized String parameter and returns a different status string for safe input versus blocked malicious input.

← Security and Sanitization · Ref: Q1237

The 'sanitized' modifier on a parameter declares that the value is treated as untrusted external input and must pass the sanitization pipeline before the function body sees it. Inside the function, an unset value indicates the input was rejected and a set value indicates the input is safe to use.

FUNCTION WITH SANITIZED PARAMETER

  processInput()
    -> input as sanitized String
    <- result as String: "unset"
    if input?
      result: "safe"
    else
      result: "blocked"

The 'sanitized' keyword is part of the parameter declaration. The compiler enforces that the function only sees sanitized values — malicious inputs like SQL injection or XSS payloads result in an unset parameter, NOT a thrown exception.

DEMONSTRATING SAFE AND BLOCKED INPUTS

  // Safe input — passes through
  result1 <- processInput("Hello World")
  stdout.println("Safe: " + result1)        // "Safe: safe"
  // SQL injection — blocked
  result2 <- processInput("'; DROP TABLE users--")
  stdout.println("SQL: " + result2)         // "SQL: blocked"
  // XSS — blocked
  result3 <- processInput("<script>alert('xss')</script>")
  stdout.println("XSS: " + result3)         // "XSS: blocked"

KEY POINT

The function does not check for malicious patterns itself. The 'sanitized' modifier is the contract — the compiler and runtime ensure the parameter is either safe or unset. The function body simply checks 'input?' to know which case it is in.

See Q215 for sanitized parameters basics. See Q269 for input validation patterns. See Q272 for defense in depth.

Example

defines module qa.security.sanitizedfunction

  defines function

    processInput()
      -> input as sanitized String
      <- result as String: "unset"
      if input?
        result: "safe"
      else
        result: "blocked"

  defines program

    SanitizedFunctionDemo()
      stdout <- Stdout()

      stdout.println("=== Sanitized Function Param Demo ===")

      safe <- "Hello World"
      result1 <- processInput(safe)
      stdout.println("Safe: " + result1)

      sqlInject <- "'; DROP TABLE users--"
      result2 <- processInput(sqlInject)
      stdout.println("SQL: " + result2)

      xss <- "<script>alert('xss')</script>"
      result3 <- processInput(xss)
      stdout.println("XSS: " + result3)

      safeName <- "John O'Brien"
      result4 <- processInput(safeName)
      stdout.println("Name: " + result4)

      stdout.println("=== Complete ===")

Common mistakes

E50001 — The 'sanitized' modifier comes BEFORE the type, not after. The correct form is 'input as sanitized String'. See ek9 -h E50001 for details.

Incorrect:

-> input as String sanitized

Correct:

-> input as sanitized String
Other ways to ask this
  • Create a function with a sanitized String parameter and demonstrate it on SQL injection and XSS inputs.
  • Show me how the sanitized keyword works on a function parameter in EK9.
  • Implement an input-checking function that uses the sanitized modifier.
  • Build a processInput function that rejects malicious payloads.

Coming from another language?

Java: manual validation with OWASP ESAPI or Bean Validation annotations. Python: html.escape and re.sub patterns or bleach library. Rust: newtype pattern with manual validation in the constructor. Go: explicit if-err checks after every input. EK9: 'sanitized' modifier delegates input validation to the runtime; the function only sees safe values.

Keywords: input, function, sanitized, untrusted, security, SQL injection, XSS, validation, parameter