Language traps: what to catch in PHP, Python, Kotlin and TypeScript
Each language has sharp edges. Knowing them turns a skim of the code into a real review.
Remember: For each value, ask what its type really is, and what the language does with null, NaN, empty input and errors.
Most review findings in a multi-language codebase come from the same few language traps. The rules below are the ones that cause real incidents. Read them as questions to ask of each line, not as a list to memorise.
PHP
- == between strings and numbers compares loosely (type juggling). Use === or hash_equals() for secrets and tokens.
- include, require and file functions with request data: use an allowlist, never a path built from input.
- echo of request values without context-aware encoding: htmlspecialchars with ENT_QUOTES for text and attributes.
- unserialize() on user data can instantiate objects with side effects (object injection).
- String-built SQL instead of prepared statements, and md5() or sha1() for passwords.
- extract() and variable variables, which turn request keys into local variables.
if ($given == $expected) {
// passes for '0e123' and '0e456'
}
Python
- pickle.loads, marshal, yaml.load without SafeLoader, and eval or exec on untrusted data.
- subprocess with shell=True, or os.system with an f-string: input becomes part of a shell command.
- os.path.join with request data: an absolute second argument discards the base directory.
- Mutable default arguments (def f(x=[])): state is shared between calls.
- Bare except: or except Exception: pass, which hides failures.
- == to compare secrets and tokens: use hmac.compare_digest.
- debug=True in Flask, or verify=False in requests, left in production code.
subprocess.run(f"convert {name} out.png", shell=True)
Kotlin
- !! asserts non-null and throws NullPointerException. On a request path this becomes a 500, sometimes with details.
- Java platform types (String!) from Java libraries are nullable in practice. Check them at the boundary.
- Binding @RequestBody directly to a JPA entity or data class: mass assignment.
- data class copy() and equals() used for security decisions.
- runCatching or catch (e: Exception) returning a default: a failure turns into a success.
- Secrets in log messages built with concatenation or templates.
- java.util.Random for tokens: use SecureRandom.
TypeScript and JavaScript
- == coercion, and parseInt without a radix. Number('') is 0.
- NaN fails every comparison. 'if (amount > 0)' is false for NaN, but 'if (!(amount <= 0))' accepts NaN.
- Floating-point money. Use integer minor units.
- forEach with an async callback does not wait, and its rejections escape try/catch.
- JSON.parse of untrusted input followed by a deep merge: prototype pollution.
- Regular expressions with nested quantifiers on user input: ReDoS.
- dangerouslySetInnerHTML, eval, new Function and javascript: URLs.
if (!(amount <= 0)) {
await transfer(amount); // amount can be NaN here
}
Read each language's failure mode, not only its syntax. For every value ask: what is its real type here, what happens on null, NaN, empty and exception, and which library call behaves dangerously with untrusted input?
Questions to ask yourself
- What is the type of this value, really, at the point it is used?
- What happens on null, NaN, an empty string, and an exception?
- Which library functions in this language are dangerous with untrusted input?
Practise this
- Password reset token compared with == · Authentication & cryptography
- Profile update that binds the whole entity · Authorization
- Login handler that logs its input · Authentication & cryptography
- Adding a fee with floating-point euros · Business logic