Handling User Input (Safely)
The moment your app accepts input โ a form, a search box, an API call โ you're taking data from strangers. Handling it safely is what separates a real, trustworthy app from a vulnerable toy.
An app that only shows information is limited; the valuable ones let people do things - sign up, post, search, buy. That means accepting input, and input comes from strangers who may be careless or malicious. Handling it safely - validating it, and guarding against a couple of classic attacks - is exactly what separates a real, trustworthy app from a vulnerable toy. This is essential knowledge for anyone building for the web.
Forms and validation
Input usually arrives via an HTML form (submitted with POST) or an API request. The first rule: never trust it - always validate before you use it.
from larzvalidate import validate, Field, EMAIL, ValidationError
schema = {
"email": Field(str, required=True, pattern=EMAIL),
"age": Field(int, min=13, max=120),
}
try:
clean = validate(form_data, schema) # trust ONLY the cleaned result
except ValidationError as e:
show_errors(e.errors) # tell the user what's wrongThe security basics you must know
Two classic attacks target apps that mishandle input. You don't need to be a security expert, but you must know these exist.
| Attack | What it is | The defence |
|---|---|---|
| Injection (e.g. SQL) | Input crafted to run as a command | Never build commands by pasting input in |
| XSS (cross-site scripting) | Input that runs as script in others' browsers | Escape user content in HTML output |
| Trusting the client | Assuming front-end checks are enough | Always re-check on the server |
| Leaking secrets | Keys in code or error messages | Keep secrets in .env, show safe errors |
The through-line: treat all input as untrusted. Escape user content before putting it in a page (larztemplate's escape option, larzmarkdown's escaping), never build a database query or command by pasting raw input into it, and always validate on the server even if the browser checked too. These few habits stop the most common real attacks.
Trustworthy by habit
- Never trust input - validate everything against what you expect (larzvalidate).
- Escape user content in HTML output to prevent XSS.
- Never paste raw input into database queries or commands (injection).
- Re-check on the server - front-end validation is for convenience, not security.
๐งฉ Do it with LarzOS
Turn this lesson into action with the exact free tools and AI workflows for the job:
๐ฏ Key takeaways
- Valuable apps accept input (forms, search, APIs) - which means taking data from untrusted strangers.
- Never trust user input: always validate it against what you expect and use only the cleaned result (larzvalidate).
- Know the classic attacks: injection (never paste input into commands) and XSS (escape user content in HTML).
- Always re-validate on the server - front-end checks are for convenience, not security.
Frequently asked
Why can't I trust input if my form already checks it in the browser?
Because checks in the browser can be trivially bypassed, so they protect no one who doesn't want to be protected - they're a convenience for honest users, not a security measure. Front-end validation (the checks that happen in the browser via HTML attributes or JavaScript) is genuinely useful: it gives users instant feedback, catches honest mistakes before a round-trip to the server, and improves the experience. But it runs on the user's machine, entirely under their control, and anyone can disable JavaScript, edit the page, or - crucially - skip the browser entirely and send a request straight to your server using simple tools. When they do that, none of your front-end checks run at all. So an attacker (or even just a buggy client, or your own API being called by another program) can send whatever data they like directly to your server. If your server assumed 'the browser already checked this', it's now processing unchecked, possibly malicious data. That's why the ironclad rule is: always validate on the server, treating every incoming request as if the front-end checks never happened - because for a determined party, they didn't. The server is the only place you control and the only place validation actually protects you. Think of front-end validation as a helpful sign asking people to wipe their feet, and server-side validation as the actual locked door: you want both (the sign is polite and helpful), but only the door provides real security. So keep doing front-end validation for user experience, but never rely on it for correctness or safety - re-check everything on the server with something like larzvalidate, and only ever trust the cleaned, server-validated result. This isn't paranoia; it's the baseline reality of building anything that accepts input over the web.
This security stuff sounds scary and complex. How much do I really need to know as a beginner?
You don't need to become a security expert to build safely, but you do need to internalise a small set of habits and awareness - and the encouraging news is that a few simple practices prevent the large majority of common real-world problems. The single most important mindset is 'never trust input': treat all data from users, forms, APIs, files, and the web as potentially malformed or malicious, and validate it before use. From that flow the key habits this session covers: validate input against what you expect (using something like larzvalidate) and work only with the cleaned result; escape user-provided content when you put it into a web page, so someone can't sneak in content that runs as script in other users' browsers (this is the XSS defence, and tools like larztemplate's escaping and larzmarkdown's escaping handle it); never build a database query or system command by pasting raw user input into it (the injection defence - real frameworks and database tools give you safe ways to do this); keep your secrets (API keys, passwords) out of your code and version control, in a .env file (as GIT-101 and larzenv cover); and always re-check on the server rather than trusting the browser. That short list - validate everything, escape output, don't paste input into commands, protect secrets, re-check server-side - covers the most common ways beginner apps get compromised, and none of it is deeply technical; it's mostly discipline and awareness. You don't need to understand cryptography, advanced attack techniques, or defend against sophisticated targeted attacks to build a reasonably safe small app - those matter for high-stakes systems and are a specialist field, but they're not where you start. What's dangerous is not the complexity but the ignorance: developers who've never heard of these issues make the basic mistakes, while those who simply know 'validate input, escape output, protect secrets' avoid most of them. So learn and practise the handful of habits, use tools and frameworks that make the safe path the easy path, stay aware that input is untrusted, and deepen your security knowledge gradually (SEC-101 is a good next step). You can build genuinely safe-enough web apps as a relative beginner by consistently applying a small number of sensible practices - security is a skill you grow into, and the fundamentals are very learnable.