

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
Of course, there are times when sanitization makes sense. For example, you might want to remove trailing and leading whitespace from the username input silently, as most users expect that behavior. But you still should have a validation step that rejects invalid ones.
For example, you might use it when:
\n with <br> in HTML)Here's a "create comment" handler that validates input, sanitizes it to a small allowlist of HTML tags, and saves it:
content := request.FormValue("content")
if utf8.RuneCountInString(content) > 280 {
http.Error(responseWriter, "Invalid content", http.StatusBadRequest)
return
}
sanitizedContent := richTextPolicy.Sanitize(content)
if strings.TrimSpace(sanitizedContent) == "" {
http.Error(responseWriter, "Empty content", http.StatusBadRequest)
return
}
saveComment(sanitizedContent) // Error handling omitted for brevity.
responseWriter.WriteHeader(http.StatusCreated)
Notice that validation happens first, and sanitization is tightly scoped.
Bearly Secure validates review bodies before removing surrounding whitespace. Trim reviews before checking whether they are empty or longer than 1,000 characters.
With Bearly Secure still running, run and submit the CLI tests from the project root.