Security and professional ethics
Đã giới thiệu
6:03
Kỹ thuật phần mềm
Software engineering is how systems outlive their authors. The first three units shape the product: requirements, architecture and interface design. The next…
Thuyết minh tiếng Anh · Có thể chuyển đổi phụ đề tiếng Anh và tiếng Trung
Trong bài học này
| English | Ý nghĩa |
|---|---|
| threat model/θret ˈmɒdl/ | mô hình mối đe dọa |
| entry point | |
| mitigation/ˌmɪtɪˈɡeɪʃn/ | a measure that reduces a risk |
| authorisation matrix/ˌɔːθəraɪˈzeɪʃn ˈmeɪtrɪks/ | a table of which roles may perform which actions |
| parameterised query/ˌpærəˈmetəraɪzd ˈkwɪərɪ/ | A database query whose data values are supplied separately as parameters rather than inserted into its command text. |
| authorisation/ˌɔːθəraɪˈzeɪʃn/ | The granting or checking of permission to access a resource or perform an action. |
| revoke and rotate/rɪˈvəʊk ænd rəʊˈteɪt/ | cancel a leaked secret and replace it with a new one |
| Defence in depth/dɪˈfens ɪn depθ/ | several independent layers of protection, so one failure is not enough |
| salted/ˈsɒltɪd/ | (of a password hash) combined with a random value before hashing, so equal passwords give different hashes |
| salt/sɒlt/ | A value combined with a password or other input before hashing so identical inputs need not produce identical stored hashes. |
Assets, entry points, attackers
- A threat model 威胁模型 lists the assets (member records, loan history), the entry points 入口点 (web form, API, reminder channel) and the attackers (opportunistic, insiders, automated bots).
- It then pairs each threat with a mitigation 缓解措施.

A threat model pairs —
Độ khó 1/5 · 100 xu + phần thưởng nhỏ khi hoàn thành lần đầu tiên hôm nay
Assets x entry points x attackers, then a mitigation per threat.
Put the steps of building a threat model in order.
Độ khó 3/5 · 200 xu + phần thưởng nhỏ khi hoàn thành lần đầu tiên hôm nay
You cannot judge an entry point until you know what it leads to, and a mitigation without a test is only a hope.
Least privilege, counted
- BookShare's authorisation matrix 授权矩阵: 3 roles × 6 actions = 18 cells, of which 4 are allowed. The other 14 are denied by default, so least privilege becomes something you can count.
- Each cell is a test: log in as that role, try that action, and check the answer. A denied cell must be refused by the server, not just hidden in the interface.
The authorisation matrix allows — of 18 cells
Độ khó 1/5 · 100 xu + phần thưởng nhỏ khi hoàn thành lần đầu tiên hôm nay
3 roles x 6 actions, 4 allowed: 14 denials by default.
SQL injection, and the fix
- The search form builds its query by gluing the user's text into SQL. An attacker types
' OR '1'='1, and the text becomes part of the query itself:
# unsafe: input becomes SQL
q = ("SELECT * FROM members "
"WHERE name = '" + name + "'")
# runs: ... WHERE name = '' OR '1'='1'
'1'='1'is true for every row, so every member's record leaks. The fix is a parameterised query 参数化查询: the SQL is fixed, and the input is passed separately as a value, so it can never change the query's meaning.- Run on three members, the unsafe query returns all three; the safe one returns none.
# safe: input is only ever a value
cur.execute(
"SELECT * FROM members "
"WHERE name = ?",
(name,))
Why does a parameterised query stop the input ' OR '1'='1 from leaking every row?
Độ khó 1/5 · 100 xu + phần thưởng nhỏ khi hoàn thành lần đầu tiên hôm nay
The SQL text is fixed; the input fills the ? only as data. So ' OR '1'='1 is just a strange name that matches no member.
The ethics call
- The product owner asks to sell members' reading history to advertisers. The professional codes put privacy harm and the public interest above what the employer prefers.
- The answer is a written refusal that offers alternatives (anonymised totals, with consent). Ethics is a decision with a written record, not a feeling.
- "It's an internal tool" is not a reason to skip security. Hiding a link is not authorisation 授权, because the page behind it still answers.
The professional answer to the data-sale request is —
Độ khó 1/5 · 100 xu + phần thưởng nhỏ khi hoàn thành lần đầu tiên hôm nay
Codes put privacy and public interest above employer preference - the paper trail is the point.
Hiding the admin link from ordinary members stops them reaching the admin pages.
Độ khó 1/5 · 100 xu + phần thưởng nhỏ khi hoàn thành lần đầu tiên hôm nay
The endpoint still answers anyone who types its address. Authorisation must be checked on the server for every request.
Secrets and dependencies
- A secret committed to git stays in the history even after a later commit deletes the file. The fix is to revoke and rotate 吊销并更换 the key at once, then clean the history. Keep secrets in environment variables or a vault, and scan commits for keys before they are pushed.
- Most code is other people's code. Pin dependency versions, keep a list of every component (a software bill of materials), and scan it for known weaknesses.
- Defence in depth 纵深防御 layers several controls, such as input checks, least privilege and logging, so one failure does not become a breach.
Deleting a leaked key file in a new commit removes the key from the repository's history.
Độ khó 1/5 · 100 xu + phần thưởng nhỏ khi hoàn thành lần đầu tiên hôm nay
Every earlier commit still contains it; rotate the key, then rewrite or purge the history.
An API key is found in a public repository. What comes first?
Độ khó 1/5 · 100 xu + phần thưởng nhỏ khi hoàn thành lần đầu tiên hôm nay
Assume it is already copied: only revoking it stops misuse.
Storing passwords
A per-user salt defeats precomputed hash tables.
Độ khó 1/5 · 100 xu + phần thưởng nhỏ khi hoàn thành lần đầu tiên hôm nay
A table would be needed for every salt value.
Storing passwords
- Never store passwords, only a salted 加盐 slow hash. The salt is random for each user: it makes tables computed in advance useless and gives equal passwords different hashes.
- A slow hash such as bcrypt or Argon2 has a work factor. At cost 12, bcrypt runs $2^{12}$ rounds, so each guess costs the attacker milliseconds instead of nanoseconds.
Tất cả $26^8$ lowercase eight-letter passwords at $10^{10}$ hashes a second: time (nearest second)?
Độ khó 2/5 · 140 xu + phần thưởng nhỏ khi hoàn thành lần đầu tiên hôm nay
$2.09 \times 10^{11}/10^{10} \approx 21$ s.
The same search at $10^4$ bcrypt guesses a second: time (nearest day)?
Độ khó 2/5 · 140 xu + phần thưởng nhỏ khi hoàn thành lần đầu tiên hôm nay
$2.09 \times 10^7$ s $/86\,400 \approx 242$ days.
Worked example: guessing eight lowercase letters
- There are $26^8 \approx 2.09 \times 10^{11}$ such passwords. A GPU computing $10^{10}$ fast hashes a second tries them all in about $\mathbf{21}$ s.
- At $10^4$ bcrypt guesses a second it needs $2.09 \times 10^7$ s, about 242 days, for one account, and the salt 盐值 means the attacker must start again for each account. Longer passwords help more: each extra letter multiplies the work by 26.
Marks that slip away
- "Internal" as an exemption. An internal tool is still an entry point 入口点.
- A secret in the repository. A committed key stays in the history for ever: rotate it and keep secrets in a secret store.
- SQL built from strings.
' OR '1'='1returns every row; use parameters.
Recap
- A threat model pairs each asset and entry point with a mitigation:
| Threat | Mitigation |
|---|---|
SQL injection: ' OR '1'='1 |
parameterised queries |
| A hidden admin link | a server-side check on every request |
| A secret committed to git | revoke and rotate 吊销并更换 the key, then clean the history |
| Stolen passwords | salted, slow hashes |
- Least privilege can be counted: $18$ cells, $4$ allowed, $14$ denied by default.
- Ethics is a decision with a written record; hiding a link is not authorisation.
Đã xảy ra vấn đề gì? Chỉ đội ngũ trang web nhìn thấy thông tin này.