Lesson 1 of 7
Signing in
Listen to an AI summary of Signing in
Authentication and authorisation
Authentication is checking who a person is, such as by a username and a password. Authorisation is deciding what a signed-in person is allowed to do.
The two checks work like a sports centre with a front desk, lockers and a staff room. Showing a pass at the front desk stands for authentication. The doors the pass opens stand for authorisation, such as a member's own locker and not the staff room.
Sam, a customer, signs in to the sports shop. The sports shop checks the password Sam types, and that check is authentication. Sam then asks to see order 1042. The sports shop checks that order 1042 belongs to Sam, and that check is authorisation.
Authentication
Who are you? Sam types a username and a password.
- then
Authorisation
What may you do? Sam may see Sam's own orders.
A website that forgets the second check lets any signed-in person see any order. Authorisation is covered in the lesson on object references and the lesson on function level checks.
What is a session
Sam signs in once and then opens many pages. A session is the period after sign-in during which the server remembers who the person is. The next lesson covers how the server remembers.
A website can ask for the password again before a risky action, such as a bank transfer.
The sign-in code of the sports shop
The function sign_up runs when a person makes a new account, and sign_in runs when a person signs in. Four numbered labels mark the lines a reviewer asks about.
The variable user holds the row for that username from the table of customers. When no row matches, user holds None, the Python word for no value. user.password is the password saved in the row.
How long a password must be
Label 1 marks the length rule, which accepts a password of 6 characters. A short password takes less time to guess than a long password. Current guidance asks for at least 15 characters where the password is the only proof a person gives. The same guidance asks a website to refuse a password that is on a list of common or leaked passwords.
A rule that accepts short passwords is the weakness named weak password requirements (CWE-521).
How a password is stored
Label 2 marks the line that saves the password in the table of customers as typed. An attacker who steals a copy of the table can read every password. The weakness is named plaintext storage of a password (CWE-256), and plaintext is text that anyone can read.
A hash is a value worked out from a password, and nobody can work out the password from the hash.
The password
purple-tiger-bus-42
- goes in
The hash function
- comes out
The hash
3cb08310e0c7faf9
Safer code saves the hash and never the password. At sign-in, the code works out the hash of the typed password and compares that hash with the saved hash. A fast hash function lets an attacker guess quickly, so a password needs a slow one (Hashing and salts).
The safer sign_up changes two lines. Look at the number 15 and at hash_password, the function that works out the hash. The example leaves out the check against a list of common passwords.
def sign_up(username, password): if len(password) < 15: return "Use at least 15 characters" save_user(username, hash_password(password))
Failed attempts
A brute-force attack is trying many passwords, one after another, until one works. A program written by the attacker sends thousands of sign-in requests.
Label 4 marks a wrong try, which the sports shop does not count, so the program can try without end. No limit on wrong tries is the weakness named improper restriction of excessive authentication attempts (CWE-307).
A rate limit is a cap on how many times one person or one computer can do something in a set time. Safer code counts the wrong tries for each username and refuses more tries after ten wrong tries in 15 minutes. A second count for each IP address, the number that identifies a computer, slows one computer trying many usernames.
Locking an account for ever is a fault too. An attacker can type ten wrong passwords for Sam's username on purpose, and then Sam cannot sign in.
The message for a failed sign-in
Label 3 marks two messages. No account with that username tells an attacker that a username is not real, and Wrong password tells the attacker that a username is real. The attacker then tries passwords on the real usernames only.
Safer code gives the same message and the same rate limit for an unknown username and for a wrong password. Two messages that differ are the weakness named observable response discrepancy (CWE-204), and a discrepancy is a difference.
The safer sign_in counts wrong tries and gives one message. Look at lines 2, 5, 6 and 7.
def sign_in(username, password): if wrong_tries(username) >= 10: return "Too many tries. Try again later" user = find_user(username) if user is None or not check_password(user, password): add_wrong_try(username) return "Username or password is incorrect" start_session(user)
wrong_tries gives the number of wrong tries for the username in the last 15 minutes. Line 2 refuses the try when the number is 10 or more, and line 6 adds one. Line 5 joins the two cases with or, so both cases get the message on line 7. check_password compares the hash of the typed password with the saved hash. The function still answers sooner for an unknown username, because line 5 then works out no hash. Safer code works out a hash for an unknown username as well, so both cases take the same time.
A password in a log
Safer code never writes a password to a log, the list software writes of what happened, including a wrong password. The weakness is named insertion of sensitive information into log file (CWE-532). A wrong password can be the person's password for another website, typed by mistake.
A second factor
A password can be guessed or stolen. Multi-factor authentication is asking for more than one kind of proof at sign-in, such as a password and a code from a phone. Each kind of proof is called a factor.
- Something you know
- A password.
- Something you have
- A phone that shows a code.
- Something you are
- A fingerprint.
An attacker who steals Sam's password still needs Sam's phone to sign in.
Phishing, a fake website that tricks a person, can collect a code too. Sam types the code into the fake website, and the attacker then uses the code. An attacker who takes over Sam's phone number also receives a code sent by text message.
Passkeys
A passkey is a way to sign in with a phone or a computer in place of a password. The phone or computer uses a passkey only on the website the passkey was made for. A fake website cannot collect a passkey.
Examples of what to look for
- A minimum length under 15 for a password that is the only proof, such as
len(password) < 6. - A new password accepted with no check against a list of common and leaked passwords.
- A password saved or compared as typed, such as
user.password != password. - One message for an unknown username and another for a wrong password.
- A sign-in function with no count of wrong tries, or an account locked for ever.
- A password written to a log, including a wrong password.
Check your understanding
State the difference between authentication and authorisation.
Show the answer
Authentication is checking who a person is. Authorisation is deciding what a signed-in person is allowed to do.
Identify two faults in the first
sign_infunction of the sports shop.Show the answer
The function does not count wrong tries. The function gives different messages for an unknown username and for a wrong password.
Give one reason why the message for a wrong password matches the message for an unknown username.
Show the answer
Two different messages tell an attacker which usernames are real.
Practise it
Challenges with a weakness this lesson teaches.
- Order lookupSwiftcart, IntermediateOpen
- Go live checklistSwiftcart, IntermediateOpen
- Erlang/OTP SSH remote code executionReal CVEs, IntermediateOpen