Find id-guessing in a log

An attacker exploiting broken access control often walks through record ids one by one. A request log shows it clearly, if you know what to look for.

What to build

From a table of who owns each record and a request log, report every user who fetched other people's records, and flag likely enumeration.

Requirements

  • Line 1: N, then N lines recordId owner. Line N + 2: M, then M log lines user recordId.
  • A request is foreign if the record exists and belongs to someone else. Requests for missing records are not foreign.
  • For each user with at least one foreign request, in order of first foreign request, print user: K foreign records (id1, id2, ...), listing the distinct foreign ids in the order first requested.
  • Add ENUMERATION at the end of the line if the user touched 3 or more distinct foreign ids, or 3 or more missing ids, or both together reach 4.
  • If no user made a foreign request, print no foreign access.

Input

As described.

Output

One line per flagged user, or one line.

Constraints

Up to 100 records and 200 log lines.

Example

Input:

3
101 rahim
102 anika
103 mina
4
rahim 101
rahim 102
rahim 103
anika 102

Output:

rahim: 2 foreign records (102, 103)

Why it works

Comparing each request with the record's owner finds every access the app should have refused; many different foreign or missing ids from one user is the pattern of someone stepping through ids rather than using the app normally.

Examples

Example 1 · Input
3
101 rahim
102 anika
103 mina
4
rahim 101
rahim 102
rahim 103
anika 102
Expected output
rahim: 2 foreign records (102, 103)
Example 2 · Input
2
1 a
2 b
2
a 1
b 2
Expected output
no foreign access

Hints

Hint 1

Track, per user, an ordered list of distinct foreign ids and a Set of distinct missing ids.

Hint 2

Users with only missing-id requests are not printed, but their missing ids still count if they also have a foreign request.

Solution

Show one correct solution (try it yourself first)
const n = Number(prompt());
const owner = new Map();
for (let k = 0; k < n; k++) {
  const [id, who] = prompt().trim().split(/\s+/);
  owner.set(id, who);
}
const m = Number(prompt());
const foreign = new Map();
const missing = new Map();
const order = [];
for (let k = 0; k < m; k++) {
  const [user, id] = prompt().trim().split(/\s+/);
  if (!owner.has(id)) {
    if (!missing.has(user)) missing.set(user, new Set());
    missing.get(user).add(id);
  } else if (owner.get(id) !== user) {
    if (!foreign.has(user)) {
      foreign.set(user, []);
      order.push(user);
    }
    if (!foreign.get(user).includes(id)) foreign.get(user).push(id);
  }
}
if (!order.length) console.log("no foreign access");
for (const user of order) {
  const ids = foreign.get(user);
  const miss = missing.get(user)?.size ?? 0;
  const flag = ids.length >= 3 || miss >= 3 || ids.length + miss >= 4;
  console.log(`${user}: ${ids.length} foreign record${ids.length === 1 ? "" : "s"} (${ids.join(", ")})${flag ? " ENUMERATION" : ""}`);
}

▶ Open this solution in the compiler, then press Visualize to watch it run step by step

Lesson: The OWASP Top 10 and broken access control →