E07943: 'sanitized' cannot be used on variable declarations - sanitize at the function/method entry point

Category: E07xxx Full resolution · Reported in phase: EXPLICIT_TYPE_SYMBOL_DEFINITION · Classification: SANITIZED_NOT_ALLOWED_IN_VARIABLE_DECLARATION · All error codes

What went wrong

The 'sanitized' keyword CANNOT be used on variable declarations or assignments. Sanitization belongs at function/method entry points, not at assignment sites.

Worked examples

Compilable examples demonstrating this error and the correct fix:

  • ek9 -q 950 (Where can the 'sanitized' keyword NOT be used in EK9?)

Rationale

"Rejection at the Source" Philosophy: By the time you're assigning to a variable, the data is already inside the trust boundary. Either:

  1. The data came from a sanitized parameter (already safe)
  2. The data was computed internally (already trusted)
  3. The data came from an external source via a function that should have sanitized its parameters

Sanitizing at assignment creates the wrong mental model:

  • It suggests sanitization is the caller's responsibility
  • It fragments security logic across the codebase
  • It's easy to forget one assignment site

Trace data back to where it entered the system. That entry point (function/method parameter) is where 'sanitized' belongs.

How to fix it

  1. REMOVE: 'sanitized' from the variable declaration
  2. TRACE: Where did this data originally enter the system?
  3. ADD: 'sanitized' to that original function/method parameter
  4. All downstream code automatically receives sanitized data

The [EXAMPLE] section below shows 'sanitized' on each kind of declaration ('<-', ':=', '=', ':~:'), and the parameters made 'sanitized' at the entry points instead.

See also

  • E07910: sanitized on wrong type (must be String)
  • E07920: sanitized in wrong location (must be incoming parameter)
  • E07941: sanitized on capture
  • E07942: sanitized at call site
  • E07943: sanitized on variable declaration (this error)

https://ek9.io/sanitized.html https://ek9.io/forAI.json#$.sanitizedTypes

Examples

Every example is compiled by the EK9 compiler: the first fails exactly as captioned, the others fix it.

Fails with E07943: 'sanitized' cannot be used on variable declarations - sanitize at the function/method entry point

#!ek9
defines module error.e07943.bad

  defines function

    //Helper function returning a string
    getValue()
      <- output as String: "test"

    //ERROR: sanitized on arrow declaration
    badArrowDeclaration()
      -> userInput as String
      //ERROR: Cannot sanitize at variable declaration
      name <- sanitized getValue()
      stdout <- Stdout()
      stdout.println(name)

    //ERROR: sanitized on typed declaration with := operator
    badTypedDeclarationAssign()
      -> userInput as String
      //ERROR: Cannot sanitize on typed variable declaration
      data as sanitized String := userInput
      stdout <- Stdout()
      stdout.println(data)

    //ERROR: sanitized on typed declaration with = operator
    badTypedDeclarationAssign2()
      -> userInput as String
      //ERROR: Cannot sanitize on typed variable declaration
      data as sanitized String = userInput
      stdout <- Stdout()
      stdout.println(data)

    //ERROR: sanitized on typed declaration with :~: operator (merge)
    badTypedDeclarationMerge()
      -> userInput as String
      //ERROR: Cannot sanitize with merge operator
      data as sanitized String :~: userInput
      stdout <- Stdout()
      stdout.println(data)

    //CORRECT: Sanitize at entry point, not at variable declaration
    correctSanitizedParameter()
      -> userInput as sanitized String
      //Assignment receives already-sanitized data
      name <- String(userInput)
      data as String := userInput
      stdout <- Stdout()
      stdout.println(name + data)

Fixed: Sanitize at the entry points

#!ek9
defines module error.e07943.good

  defines function

    //Sanitize the source function's parameter...
    getValue()
      -> input as sanitized String
      <- output as String: input

    //...or this function's entry point
    process()
      -> userInput as sanitized String
      <- rtn as String: getValue(userInput) + String(userInput)

Found a problem?

If EK9 reported E07943 for a program you think is valid, or did not report it where it should have, use the compiler form with the smallest program that shows it. If this page is wrong or unclear, use the documentation form. Both are at github.com/ek9lang/ek9-issues.