Sub topic 12
Predictable IDs and what they reveal
This chapter assumes you know the ID schemes from earlier chapters: auto-increment counters, UUID v4, UUID v7, and Snowflake-style IDs. You should also be comfortable with bit shifting and basic web authorization.
An ID is more than a label. If clients can see it, the ID may leak information, and an attacker may be able to guess other valid IDs.
What each scheme leaks
Every scheme exposes some structure to anyone who can read an ID.
| Scheme | What an observer can learn | Can others guess valid IDs? |
|---|---|---|
| Auto-increment | Total record count, growth rate | Yes, by counting up or down |
| Block allocation | Roughly the same as auto-increment, plus which block a node holds | Yes, within a block |
| UUID v4 | Almost nothing | No, if the random source is secure |
| UUID v7 / ULID | Creation time to the millisecond | Partly, because the time prefix is guessable |
| Snowflake | Creation time, worker ID, and sequence number | Partly, because only the sequence is hard to guess |
Auto-increment IDs leak business data. If you sign up and receive user ID 48,213, then sign up a week later and receive 49,001, a competitor knows your weekly growth. This is the same reasoning used to estimate German tank production from serial numbers in the Second World War.
Time-ordered IDs leak when a record was created. That can matter. A UUID v7 on an account reveals the exact signup time, even if the API hides the created_at field.
Decoding a Snowflake ID
The fields in a Snowflake ID are plain bit ranges, so anyone who knows the layout can read them. The epoch is usually easy to find or guess.
from datetime import datetime, timezone
EPOCH_MS = 1_700_000_000_000
def decode_snowflake(snowflake_id):
sequence = snowflake_id & 0xFFF
worker_id = (snowflake_id >> 12) & 0x3FF
created_ms = (snowflake_id >> 22) + EPOCH_MS
return created_ms, worker_id, sequence
sample = ((60_000) << 22) | (7 << 12) | 3
created_ms, worker_id, sequence = decode_snowflake(sample)
created = datetime.fromtimestamp(created_ms / 1000, tz=timezone.utc)
print(created.isoformat(), worker_id, sequence)This prints the creation time (60 seconds after the epoch), worker 7, and sequence 3. The worker ID shows which machine handled the request. A UUID v7 is just as easy to read: uuid_value.int >> 80 gives its Unix timestamp in milliseconds.
Guessable IDs and broken access control
The more serious risk is guessing. Suppose an endpoint is GET /invoices/1042 and the server returns any invoice that exists. An attacker who is logged in as a different user tries 1041, 1043, and so on. This flaw is called an insecure direct object reference (IDOR).
The fix is the ownership check in the diagram, not a harder-to-guess ID. Random IDs only make enumeration slower.
Making IDs hard to predict
Use a cryptographically secure random source for any ID that acts as a secret, such as a password-reset token, an invite link, or a session ID. The uuid.uuid4() function in Python reads from the operating system's random source, so it is acceptable for many cases. Do not use random.random() or a seeded generator. Their output can be reconstructed from observed values.
import secrets
reset_token = secrets.token_urlsafe(32) # 256 bits of randomness
print(reset_token)A common compromise uses two IDs per record:
- An internal ID, such as a Snowflake or auto-increment value, is the primary key. It is compact, sorts well, and keeps B-tree inserts local.
- An external ID, such as a random UUID or a token, appears in URLs and APIs.
The cost is an extra unique index and a lookup from external to internal ID. You may also choose to mask internal IDs without storing a second column, for example by encrypting them with a keyed block cipher before exposing them. That adds key management you must maintain.
Decision notes
- Public-facing, security-sensitive identifiers should be random and long enough that guessing is infeasible.
- If creation time or worker identity is sensitive, avoid exposing v7 and Snowflake IDs directly.
- If neither concern applies, such as an internal event log, the leak is harmless and sortable IDs are the better choice.
Exercise
Your public API exposes order IDs as Snowflake values. Name two things a customer can learn from an ID, and give the one change that actually stops customers reading other customers' orders.
Show answer
A customer can learn the order's creation time and the worker ID of the machine that created it. The sequence number also hints at the load on that machine in that millisecond. Replacing the ID with a random one does not stop other customers reading orders. The real fix is a server-side check that the caller owns the order. A random external ID can be added as defense in depth.