

0 / 2 embers
0 / 3000 xp
click for more info
Complete a lesson to start your streak
click for more info
Still calibrating
click for more info
Not enough gems
Cost: 6 gems
1: Injection
incomplete
2: Fixing SQL Injection
incomplete
3: Injection Beyond SQL
incomplete
4: Safe Validation and Sanitization
incomplete
5: When to Sanitize
incomplete
6: Unsafe Archive Extraction
incomplete
7: Safe Archive Extraction
incomplete
8: LLM Prompt Injection
incomplete
9: Limiting Tool Calls
incomplete
10: Narrow Tool Interfaces
incomplete
11: File Upload Security
incomplete
Back
ctrl+,
Next
ctrl+.
This lesson's interactive features are locked, please to keep using them
User input is untrusted. I know we've been over this... sorry to beat a dead horse... but it's worth repeating.
Two key practices here are validation and sanitization, and they solve different problems.
Sanitization is different from context-specific escaping or encoding. HTML escaping, shell argument handling, and similar defenses belong deeper in the logic where the application knows how the value is being used. Validation usually happens earlier, and complements those defenses; it does not replace them.
For most user-provided data, the safest pattern is explicit validation followed by a hard reject on failure.
Imagine a user registration endpoint that accepts a username, limited to 20 alphanumeric characters. The endpoint could "sanitize" the input by dropping any invalid characters and applying a length cap:
var invalidUsernameCharacters = regexp.MustCompile(`[^a-zA-Z0-9]`)
cleanedName := invalidUsernameCharacters.ReplaceAllString(username, "")
if len(cleanedName) > 20 {
cleanedName = cleanedName[:20]
}
But that doesn't make much sense... the user's input has been silently changed! You're going to create them a username they didn't type in?!? Bad. In this case, it would be simpler and safer to simply return an error on misshapen input:
var usernamePattern = regexp.MustCompile(`^[a-zA-Z0-9]{3,20}$`)
func isValidUsername(input string) bool {
return usernamePattern.MatchString(input)
}
username := request.FormValue("username")
if !isValidUsername(username) {
http.Error(responseWriter, "Invalid username", http.StatusBadRequest)
return
}
Bearly Secure parses cart quantities as floating-point numbers, so alternate numeric forms such as 1e1 can bypass the intended input format. Accept only canonical whole-number strings.
With Bearly Secure still running, run and submit the CLI tests from the project root.