Easier to read with cream paper, a tint, more contrast or larger text? Choose your colours and text, once, for every page.

Free lesson

ESP task 2 (2 of 2): Make code safer

The whole lesson, free. Read it and have a go at the activities. To work the tasks and labs in the real lesson, press Start lab, with no account needed.

40 minutes

A quick check that you are not a bot, then your own lab opens on the interactive lesson.

Already have an account? Sign in

A phone number on a big screen

The desk screen at the club is in the waiting room. It shows every member's full phone number.

A visitor reads the screen and writes the numbers down.

Nothing in the code is "broken". It does what it was told. But it is not safe. Safe code is part of good code.

Key words

WordWhat it meansExample
Security riskA way that data or a system could be harmed.a visible phone number
ValidationChecking that input is sensible before you use it.a name has only letters
Personal dataFacts about a real person.name, phone number
MaskHide part of a value.*******0123
HardcodedTyped straight into the code so anybody who reads it can see it.password = "club123"
CharacterOne letter, digit (0 to 9) or sign in a text.a, 7, !
HashingA one-way scramble. You cannot turn it back into the original.sha256

The cells students fill in are left blank in this preview.

Try it: hide most of a number

Code: python
phone = "07700900123"
shown = ""
for i in range(len(phone)):
    if i < len(phone) - 4:
        shown = shown + "*"
    else:
        shown = shown + phone[i]
print(shown)

# phone[i] is the character at place i.
# Only the last 4 places are shown.
# Change the phone number and run it again.

Make a free account to save your work and track your progress.

Four common risks and fixes

RiskExampleFix
Bad inputA name that holds code or is 5000 letters longValidate: check length and allowed characters
Data that everyone can seeA full phone number on a big screenMask it, or do not show it
Secrets in codepassword = "club123" in the fileDo not hardcode. Keep secrets outside the code
Weak passwordsThe password is 1234Check the length and the kinds of character

The cells students fill in are left blank in this preview.

Match the unsafe habit to the fix

Have a go

Loading…

Your answers are marked here and are not kept. A college account keeps every student’s marks.

About hashing

Note

Real systems use a special slow method and add a random extra value. A plain hash is only a first idea. Your lecturer will tell you how far to go.

Try it: a hash

Code: python
import hashlib

first = hashlib.sha256("badger2024".encode()).hexdigest()
second = hashlib.sha256("badger2025".encode()).hexdigest()
print(first)
print(second)

# One character changes. The whole hash changes.

Make a free account to save your work and track your progress.

Check: safe or not?

Have a go

Check: safe or not?

4 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

Stage 2: make the code safer

Make three small functions that protect the Hillside tool. clean_name removes spaces from the start and end of a name and refuses a bad one. mask hides all but the last 4 characters. is_strong_password checks a password is at least 8 characters with a letter and a digit (0 to 9). Read the comments in the starter. Press Run Tests to check.

Code: python
def clean_name(text):
    # Make a member name safe to store.
    # Remove spaces at the start and end.
    # Give back None if the name is empty, longer than 30 characters,
    # or has anything except letters, spaces, hyphens and apostrophes.
    return text


def mask(text):
    # Hide all but the last 4 characters with * so a phone number is not shown in full.
    # Text of 4 characters or fewer is given back as it is.
    return text


def is_strong_password(password):
    # True if it is at least 8 characters long, has a letter and has a digit.
    return None

Make a free account to save your work, or press Start lab to work through the whole lesson.

A check that is too strict blocks people

A check that is too strict can block real people. A name check that refuses an apostrophe (the ' mark) cannot take "O'Neil". A check that allows only A to Z cannot take "Zoë".

Test your checks with real names from many languages.

Key words crossword

Have a go

Loading…

Your answers are marked here and are not kept. A college account keeps every student’s marks.

Final check

Have a go

Final check

5 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

Teach this lesson with your class

On a college account your students work the tasks and the labs as well, and every mark is kept for you.