ENع
←Least privilege
Lesson 05 · Foundations

Least privilege

10 min10 steps5 questionsBeginner
About this lesson

Least privilege means giving every person and every program only the access its job needs, and nothing more. Follow one small online shop: Sam in support and a report bot both get one permission, written down in a seven-line Python rule. Watch one access check say yes once and no four times (a page Sam does not need, an unknown name, a stolen account and a buggy program), then see why access must be taken back when jobs change.

You'll learn to

Tick what you feel confident about. These self-checks do not affect your lesson score.

Fig 5 · Interactive
01 / 10

What least privilege means

Least privilege means giving each person and each program only the access its job needs, and nothing more. Our example is a small online shop: Sam, a report bot, and the orders, payroll and settings they could touch.

least privilege   only the access the job needs
                  nothing more, for people and programs
NIST              "the minimum system resources and
                  authorizations that the entity needs
                  to perform its function"
here              Sam, a report bot
                  orders · payroll · settings
Loading figure…
or ← →
Read the lesson step by step
  1. What least privilege means

    Least privilege means giving each person and each program only the access its job needs, and nothing more. Our example is a small online shop: Sam, a report bot, and the orders, payroll and settings they could touch.

    least privilege   only the access the job needs
                      nothing more, for people and programs
    NIST              "the minimum system resources and
                      authorizations that the entity needs
                      to perform its function"
    here              Sam, a report bot
                      orders · payroll · settings
  2. Start from the job

    Begin with what each job really needs. Sam works in customer support and reads orders to answer questions. The report bot, a small program, reads orders to count sales. Neither one needs payroll or the shop's settings.

    job                 needs            does not need
    Sam (support)       read orders      payroll, settings
    report bot          read orders      payroll, settings,
                                         changing orders
    a permission        one action on one thing
                        written  thing:action  e.g. orders:read
  3. Write the rule down

    In Python, a dictionary called GRANTS maps each name to a set of allowed actions. Sam gets exactly one permission, "orders:read", and so does the bot. Anything not written here is not allowed.

    GRANTS = {
        "sam": {"orders:read"},   # one permission
        "bot": {"orders:read"},   # the same one
    }
    dictionary   name → value  (here: name → set)
    set          a collection with no repeats;
                 `in` asks "is this in it?"
  4. One check on every request

    Every request passes one check: the function allowed(). It finds the name in GRANTS and asks if the action is in its set. Sam asks to read an order: True. The lamp shows 1 and the request goes through.

    def allowed(who, act):
        mine = GRANTS.get(who, set())
        return act in mine
    
    >>> allowed("sam", "orders:read")
    True            # the request reaches the orders
  5. A page Sam does not need

    Now Sam clicks the payroll page. "payroll:read" is not in Sam's set, so allowed() returns False. The lamp shows 0 and the check stops the request. Sam can still do the whole job.

    >>> allowed("sam", "payroll:read")
    False           # not in {"orders:read"}
    
    stopped at      the access check
    Sam's job       still works: orders:read is enough
  6. Deny by default

    A request from the internet uses the name "guest", which is not in GRANTS. GRANTS.get(who, set()) then gives an empty set, so every answer is False. Deny by default: if nobody wrote a rule, the answer is no.

    >>> GRANTS.get("guest", set())
    set()           # the default: an empty set
    >>> allowed("guest", "orders:read")
    False
    
    deny by default   no rule → no access
                      (OWASP: "Except for public resources,
                       deny by default.")
  7. A stolen account stays small

    Suppose Sam's password is stolen. The attacker signs in as Sam, so who is "sam" and they get only what Sam has: reading orders. Their try at the settings is refused. Least privilege keeps the harm small, the 'blast radius'.

    stolen account    the attacker acts as "sam"
    >>> allowed("sam", "settings:edit")
    False
    
    blast radius      how much one stolen account
                      or one mistake can reach
      Sam as admin    orders, payroll, settings
      least privilege orders (read only)
  8. Programs get least privilege too

    The rule is for programs too. The report bot only reads orders. When a bug makes it try to delete orders, the check says no, and nothing is lost. NIST applies least privilege to processes, not just people.

    >>> allowed("bot", "orders:delete")
    False           # the bot can only read
    
    a bug or a takeover of the bot
    still cannot change or delete orders
    NIST SP 800-53 AC-6   users, and processes
                          acting for them
  9. Take access back

    Access piles up unless someone removes it; this is called privilege creep. When Sam moves to another team, take "orders:read" away and grant only what the new job needs. Review everyone's access on a schedule.

    privilege creep   old access kept after a job changes
    Sam moves team    remove "orders:read"
                      grant only what the new job needs
    review            check every account's access
                      regularly (NIST AC-6(7))
    temporary         extra access for one task,
                      then take it back
  10. Least privilege in four habits

    Four habits: start from the job, deny by default, check every request, and take access back when it is no longer needed. Mistakes and stolen accounts still happen; least privilege keeps the harm they can do small.

    1  start from the job       what does it really need?
    2  deny by default          no rule → no
    3  check every request      one allowed() for all
    4  take access back         review, remove, expire
    
    it does not stop mistakes; it limits the harm

Quiz Unlocks after the last step.