ENع
→Web app security
الدرس 12 · الأنظمة الآمنة

Web application security

لم يُترجم بعد؛ يُعرض باللغة الإنجليزية

12 دقيقة8 خطوات5 أسئلةIntermediate
عن هذا الدرس

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.

ستتعلّم

حدّد ما تشعر بأنك أتقنته. هذا التقييم الذاتي لا يؤثر في درجة الدرس.

شكل 12 · تفاعلي
01 / 08

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
جارٍ تحميل الرسم…
أو ← →
اقرأ الدرس خطوة بخطوة
  1. 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
  2. Never 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 code
  3. The 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 mixed
  4. The 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 query
  5. The 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 data
  6. Escape 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 &#x27; and < into &lt;, so the browser shows plain text.

    line 9   html.escape(name)
    prints   No results for O&#x27;Brien
             '  →  &#x27;      <  →  &lt;
    browser  shows: No results for O'Brien
    name     cross-site scripting (XSS) if skipped
  7. Check 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 server
  8. Secure 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

اختبار يتاح بعد الخطوة الأخيرة.