Input Validation and Injection Prevention
A rename request contains a proposed title. First decide which values the application accepts. Then pass the accepted value to the database as data, keeping it separate from query instructions.
On this page
Validate type, length, and business rules on the server
The browser can give quick feedback, but the server validates all inputs before saving. A title may contain an apostrophe. Trim surrounding whitespace and apply the product length rule. Also validate relationships, such as an end date following a start date. Accept only fields the operation can change.
function readTitle(value) {
if (typeof value !== "string") return null;
const title = value.trim();
const length = Array.from(title).length;
return length >= 1 && length <= 120 ? title : null;
}
Bind SQL values and allowlist structural choices
With a PostgreSQL client, placeholders keep the query structure fixed even when the title contains punctuation. Column names and sort directions must come from trusted choices in your code. For other interpreters, choose APIs that accept structured arguments. Prefer a filesystem API to constructing shell commands for file operations.
const result = await pool.query(
"SELECT id, title FROM documents WHERE owner_id = $1 AND title = $2",
[session.userId, "Editor's notes"]
);