Web application security
لم يُترجم بعد؛ يُعرض باللغة الإنجليزية
عن هذا الدرس
Most web app flaws come from one mistake: letting untrusted input be read as code. Follow a real visitor named O'Brien through a nine-line Python search page. Gluing her name into the SQL text makes her apostrophe end the quoted text early, which is SQL injection in miniature. Then fix it with a parameterized query, escape the reply page so input cannot become page code (cross-site scripting), and check permission on the server for every request. It ends with the OWASP Top 10:2025, CISA's Secure by Design guidance and the NIST SSDF.
Three parts of a web app
A web app has three parts. The browser shows pages and sends requests. The app is code on a server that answers them. The database stores the data. This app lets visitors search its member list by name.
browser shows pages, sends requests web app code on a server (Python here) database stores the data (SQLite here) example search members.example by name
اقرأ الدرس خطوة بخطوة
Three parts of a web app
A web app has three parts. The browser shows pages and sends requests. The app is code on a server that answers them. The database stores the data. This app lets visitors search its member list by name.
browser shows pages, sends requests web app code on a server (Python here) database stores the data (SQLite here) example search members.example by nameNever trust input
Anything that arrives from the browser is untrusted input: form fields, links, cookies and headers. Anyone can send anything. Here a real visitor, O'Brien, types her own name into the search box. Its apostrophe is about to matter.
untrusted input anything from the browser form fields, links (URLs), cookies, headers line 4 name = "O'Brien" rule treat it as data, never as codeThe mistake: gluing text
Line 6 builds the query by gluing the name into the SQL text, between quote marks. The app sends one string, and the database has to work out for itself which part is code and which part is data.
line 6 bad = q + "'" + name + "'" gives SELECT name FROM users WHERE name = 'O'Brien' sent one string: code and data mixedThe quote breaks the query
The apostrophe ends the quoted text early, so the database reads Brien' as SQL code and stops with a syntax error. Here that is only an error, but the same mistake lets crafted input change what a query does: SQL injection.
reads 'O' data (the text O) Brien' code (not a name any more) result sqlite3.OperationalError: near "Brien": syntax error name SQL injection: input changes the queryThe fix: parameters
Line 7 sends the query with a ? placeholder, and the name separately as a value. The database settles the code first, then fills in the value as data only, whatever it contains. OWASP and CISA name this the main fix.
line 7 db.execute(q + "?", (name,)) code SELECT name FROM users WHERE name = ? value O'Brien (sent apart, data only) result no match, no error: rows = [] CISA separate SQL code from user dataEscape what you show
The page shows the name back. Pasting input into HTML raw is the same mistake: input could become page code, called cross-site scripting. html.escape turns ' into ' and < into <, so the browser shows plain text.
line 9 html.escape(name) prints No results for O'Brien ' → ' < → < browser shows: No results for O'Brien name cross-site scripting (XSS) if skippedCheck every request
Another common mistake is trusting the browser to hide what a user may not see. Anyone can edit a request, say to ask for someone else's order. So the server checks permission on every request and refuses: 403 Forbidden.
mistake hiding a button is not a check request an order that is not hers server may this user see it? no reply 403 Forbidden OWASP deny by default, check on the serverSecure by design
OWASP's Top 10 for 2025 ranks broken access control first and injection fifth. CISA asks makers to remove whole classes of flaws, with parameters and escaping as the default. NIST's SSDF adds code review and testing.
OWASP Top 10:2025 A01 Broken Access Control A05 Injection (SQL injection, XSS) CISA secure by design: safe by default NIST SP 800-218 SSDF: review and test code
أسئلة سريعة: 5
اختر إجابة واحدة لكل سؤال. يمكنك تغيير اختياراتك قبل التحقق منها.
01A visitor named O'Brien searches a site, and the page fails with a database syntax error. What is the most likely mistake in the code?
Glued text mixes code and data in one string. The apostrophe ends the quoted text early, so the rest of the name is read as SQL. The same mistake is what makes SQL injection possible.
02Which fix do OWASP and CISA recommend first against SQL injection?
With a ? placeholder the database settles the query's code first and treats the value as data only. Stripping characters would break real names like O'Brien and is easy to get wrong.
03A page shows a visitor's search term back to them. What stops that input from becoming part of the page's code?
Escaping turns characters such as < and ' into HTML entities (<, '), so the browser shows them as text. Skipping it allows cross-site scripting. Template engines usually escape by default.
04A shop hides the 'view order' button for orders that are not yours. Why is that not enough?
The browser is under the user's control. OWASP says access control only works in trusted server-side code, and should deny by default. Broken access control is A01 in the OWASP Top 10:2025.
05In the OWASP Top 10:2025, which category is ranked first?
A01:2025 is Broken Access Control. Injection, which includes SQL injection and cross-site scripting, is A05:2025.
المسرد
- Web application
- A program people use through a browser. The browser sends requests; code on a server (the app) answers them, often using a database.
- Untrusted input
- Anything that arrives from outside the server's control: form fields, links (URLs), cookies and headers. It must be handled as data, never run as code.
- SQL and SQL injection
- SQL is the language used to ask a database for data. SQL injection is the flaw where input glued into a query changes what the query does.
- Parameterized query
- A query sent with placeholders (such as ?) and the values sent separately, so the database always treats the values as data. Also called a prepared statement.
- Cross-site scripting (XSS)
- The flaw where input placed into a web page without escaping becomes part of the page's code in another user's browser.
- Escaping (output encoding)
- Turning characters that have a special meaning in HTML, such as < and ', into safe codes (<, ') so the browser shows them as text.
- Access control
- Deciding on the server, for every request, whether this user may see or change this thing. Deny by default; 403 Forbidden means 'not allowed'.
- Secure by design
- CISA's push for software makers to remove whole classes of flaws by default, for example by making parameterized queries and escaping the normal way to write code.
الملاحظات والمصادر
- The row is a picture of the idea, not of how SQLite really parses: the database splits the whole text into tokens. What it shows is true: with glued text, part of the name ends up outside the quotes and is read as SQL.
- Real apps seldom call html.escape by hand: template engines such as Jinja2 and frameworks such as Django escape output by default, and database libraries make parameters easy. CISA asks for exactly that: safe by default.
- Checking input (for example, a length limit) is a useful extra layer, but it is not the fix: a name like O’Brien is valid. OWASP calls parameters the primary defence and strongly discourages hand-made escaping of SQL.
- Some sites answer 404 Not Found instead of 403, so as not to reveal that the order exists. Either way the check happens on the server.
- This course is about defence. It explains how attacks work only at the level needed to stop them, and never how to attack a real system.
- OWASP Top 10:2025
- OWASP SQL Injection Prevention Cheat Sheet
- OWASP Cross Site Scripting Prevention Cheat Sheet
- OWASP Authorization Cheat Sheet
- CISA and FBI: Secure by Design Alert, Eliminating SQL Injection Vulnerabilities in Software (March 25, 2024)
- CISA and FBI: Secure by Design Alert, Eliminating Cross-Site Scripting Vulnerabilities (September 18, 2024)
- CISA: Secure by Design
- NIST SP 800-218: Secure Software Development Framework (SSDF) Version 1.1
- Python documentation: sqlite3, placeholders
- Python documentation: html.escape
- RFC 9110 HTTP Semantics: 403 Forbidden
اختبار يتاح بعد الخطوة الأخيرة.