#72 · Errors and security

Find the bug · #72 · Errors and security Hard Java

Anyone can open anyone’s invoice

Changing the number in an invoice link shows another customer’s invoice.

On this page

The report

Bug report

A customer noticed that changing the number in their invoice link, from /invoices/1001 to /invoices/1002, shows someone else’s invoice, with their name and address.

The code

Read it first: what should it do? Then click the line where you think the bug is.

JavaClick the line you think is wrong
class Invoices {
    private final Map<Integer, String[]> byId = new HashMap<>(); // id -> {owner, pdf}

    void add(int id, String owner, String pdf) { byId.put(id, new String[] { owner, pdf }); }

    // GET /invoices/{id}, for the signed-in user.
    String open(int id, String signedInUser) {
        String[] invoice = byId.get(id);
        if (invoice == null) throw new NoSuchElementException("no invoice " + id);
        return invoice[1];
    }
}

Read it, then pick a line.

What it prints

Running it on a small example prints the output on the left. It should print the output on the right.

The example
Invoices inv = new Invoices();
inv.add(1001, "asha", "asha-nov.pdf");
inv.add(1002, "ravi", "ravi-nov.pdf");
System.out.println(inv.open(1001, "asha"));
try {
    System.out.println(inv.open(1002, "asha"));
} catch (NoSuchElementException e) {
    System.out.println("refused: " + e.getMessage());
}

It prints

asha-nov.pdf
ravi-nov.pdf

It should print

asha-nov.pdf
refused: no invoice 1002
Show the answer: the bug, the fix, and how you’d find it

The bug (lines 9, 10)

The method checks that the invoice exists (line 9) and returns it (line 10), but never checks that it belongs to the signed-in user: signedInUser isn’t used at all. Anyone signed in can open any invoice just by changing the number. This is an IDOR, an “insecure direct object reference”. Check ownership, and answer “not found” rather than “forbidden”, so the response doesn’t reveal which invoice numbers exist.

The fix

7    String open(int id, String signedInUser) {8        String[] invoice = byId.get(id);9        if (invoice == null) throw new NoSuchElementException("no invoice " + id);10        if (!invoice[0].equals(signedInUser)) throw new NoSuchElementException("no invoice " + id);10        return invoice[1];11    }12}

How you’d find it

  1. Reproduce as the customer did: sign in as Asha and open Ravi’s invoice. It opens.
  2. Read the method for any check on who is asking: none. The signedInUser parameter is never read, and an unused parameter is a strong hint.
  3. Ask the access question for every endpoint that takes an id: “is this the caller’s?”

The test that catches it

It fails on the code above and passes once it’s fixed. Keep it, so the bug can’t come back.

Invoices inv = new Invoices();
inv.add(1, "a", "a.pdf");
check(inv.open(1, "a").equals("a.pdf"), "the owner can open it");
boolean refused = false;
try {
    inv.open(1, "b");
} catch (NoSuchElementException e) {
    refused = true;
}
check(refused, "someone else can't");

Where else this bites

  • Sequential ids in URLs make it easy to try others; random ids help, but they don’t replace the check.
  • APIs that check you’re signed in, but not what you’re allowed to see.
Your progress stays on this device.