JSON Security Best Practices β Interview Questions
β‘ Short Answer
Treat all inbound JSON as untrusted: never eval() it (use JSON.parse), validate against a schema, cap body size and nesting depth (to stop DoS), allow-list fields to prevent mass assignment, and never echo it into SQL/HTML/OS commands without escaping. On output, don't leak sensitive fields (password hashes, tokens, internal ids).
βCoffee Chat Question
Concept Made Simple
βWhat are JSON security best practices?β
π§ Mind Map Answer
Remember It Faster
β¨οΈHands-on Keyboard
Learn by Doing
// Express: cap body size to blunt DoS via giant payloads
app.use(express.json({ limit: "100kb" }));
// Allow-list fields β never spread req.body onto the entity
const { name, email } = req.body; // ignore id, role, isAdmin
await users.create({ name, email });π₯What If?
Think Beyond the Expected
How can a maliciously crafted JSON payload cause a denial of service?
Several ways: an enormous body exhausts memory/bandwidth; extremely deep nesting can blow the parser's stack; and huge arrays/strings spike CPU during parse. Defenses: enforce a max request size, limit nesting depth, set parse timeouts, and reject oversized/over-deep payloads with a 413/400 before processing.
πReal World
Mass assignment (binding req.body straight onto a model so a user sets isAdmin=true) and 'parse an HTML page as JSON via eval' are real, repeatedly-exploited bugs. Size/depth limits and schema validation are standard hardening at the API edge.
π―Interviewer's Expectation
Keywords they're listening for:
β οΈCommon Mistakes
- βUsing eval() to parse JSON
- βSpreading req.body directly onto entities
- βNo size/depth limits on request bodies
β Best Practices
- βValidate, size-limit, and depth-limit input
- βAllow-list fields; own privileged fields server-side
- βEscape on reuse; never leak secrets in responses
πFollow-up Questions
- 1What is a mass-assignment vulnerability?
- 2How does deep nesting enable a DoS?
- 3Why must you escape JSON before putting it in HTML/SQL?
π§©Related Technologies
Continue Learning with AI
Take this question deeper with your favourite AI assistant. Pick a depth, copy the prompt, or open it directly β AI is your learning companion, not a shortcut.
Plain-language foundations
I'm preparing for a software engineering interview and want to understand this from scratch, as a beginner. Topic: Security (JSON) Interview question: "What are JSON security best practices?" Please: 1. Explain the core idea in simple, plain language, using an everyday analogy. 2. Define any technical terms you use. 3. Walk through one small, concrete example. 4. Finish with a single sentence I can easily remember. Keep the tone friendly and assume I'm new to this topic.
Was this answer helpful?
β Featured Products
Support our platform by exploring our recommended products.
As an Amazon affiliate, purchases through these links may earn us a small commission β at no extra cost to you. It helps keep Full Stack Interview Guru free.