Security and fintech
API security checks who can see and do what: 401 without a valid token, 403 for a role without rights, no foreign data by id (IDOR). Fintech adds requirements that follow from the nature of money: exact amounts without floats, zero and negative values, ISO 4217 for currencies, and idempotency through Idempotency-Key with no double debit. A foreign resource is checked with a separate user.
API · Checklist
User A requests the account of user B and gets 200. What else was left unchecked?
In a financial API broken access costs more than a broken interface: a stranger's account can be peeked at, a transfer can be debited twice, and the cents can "float" in reports. A functional test for 201 does not see this, because each of these problems lives outside the happy path.
Below: what is checked in security and access, what fintech adds, and why these checks are not part of a typical set.
How it is built: two lists
Security and access answers the question "who can see and do what".
- no token, an expired token, a tampered token: 401;
- a role without rights: 403;
- someone else's id in the URL: no foreign data (IDOR, in OWASP terms "broken object level authorization");
- the response has no passwords, full card numbers, internal fields;
- SQL injection and special characters in string fields do not break the request;
- rate limiting: 429 after the limit, ideally with
Retry-After.
Fintech adds requirements that follow from the nature of money.
- amounts are exact:
0.1 + 0.2, rounding, cents; - a negative amount, zero, an amount above the balance or the limit;
- a repeat with the same
Idempotency-Key: no double debit; - currency: ISO 4217 codes, an unsupported currency;
- the balances of the sender and the receiver add up, and on an error nothing is partly debited;
- dates and time zones: the ISO 8601 format, operations near midnight.
Why money needs separate attention. Floating-point numbers cannot represent most decimal fractions exactly, because they are stored in binary. That is why ordinary arithmetic gives surprising results:
console.log(0.1 + 0.2); // 0.30000000000000004
console.log(Math.round(1.005 * 100)); // 100, not 101: 1.005 * 100 is 100.49999999999999
If a backend or a frontend counts money in a float, the cents sooner or later "float away". So amounts are stored in decimal types or as integers of the minimal unit (cents), and are sent in JSON as an integer number of units or as a string with a decimal point.
How to store an amount
A floating-point number
"amount": 0.1 in a float. The sum of a hundred such payments is not exactly ten, and rounding depends on who counts.
Integer minimal units or a decimal string
"amountMinor": 10 or "amount": "0.10". Addition is exact, rounding is set by a rule, not by chance.
Example: two tests that catch the most expensive bugs
Idempotency: a client repeats a request after a dropped connection, and the money must not be debited twice. The test sends the same request with the same key twice and looks at the balance.
test('the same Idempotency-Key does not charge twice', async ({ request }) => {
const before = await balance(request, 42);
const key = crypto.randomUUID();
const send = () =>
request.post('/v1/accounts/42/transfers', {
headers: { 'Idempotency-Key': key },
data: { toAccount: 'UA21...', amountMinor: 10000, currency: 'UAH' },
});
const first = await (await send()).json();
const second = await (await send()).json();
expect(second.id).toBe(first.id); // the replay returns the same transfer
expect(await balance(request, 42)).toBe(before - 10000); // debited once
});
The status of a repeat (the same 201 or 200) depends on the contract, so the test compares not the status but what matters: the same transfer id and a single debit. Per the IETF draft, 409 is returned while the first request with this key is still being processed. The balance function is a helper here: it reads the balance through GET.
Access to someone else's data: user A asks for a resource of user B.
test('user A cannot read the account of user B', async ({ request }) => {
const res = await request.get('/v1/accounts/' + accountOfB.id, { headers: authA });
expect([403, 404]).toContain(res.status()); // which one is decided by the contract
expect(await res.text()).not.toContain(accountOfB.owner);
});
Both 403 and 404 are allowed: for someone else's resource 404 is often returned so as not to reveal that such an id exists. The contract decides which. The main check is in the second line: the response has no foreign data, even if the status looks "nice".
Limits and alternatives
- This lesson is about what to check, not a full pentest. A deep look at access, JWT and injections is in the API security chapter; here is only the minimum every set must have.
- Idempotency through the
Idempotency-Keyheader is not finally standardised (there is an IETF draft), so details, including what to answer to a repeat with a different body, depend on the service and are described in its documentation. - Rate limiting and injections are risky to test on a shared environment: bulk requests can block others, so this is done on a separate environment.
- Fintech may have extra requirements (PCI DSS for cards, regulatory ones for payments): they are checked against the product requirements, not against this list.
Common mistakes
- Checking only a missing token. An expired, a tampered and another user's token behave differently, and the bugs live exactly there.
- Counting on the UI not letting it happen. The interface does not show someone else's id, but the API accepts it, so IDOR is checked at the request level.
- Comparing amounts with
toBeon fractional numbers. The test fails with an error that is a sign of a real storage problem, not a trifle. - Not checking a partial failure. If the second half of a transfer failed, the first must not stay done.
Check yourself
A request of user A to /v1/accounts/{someone else's id} returns 404. Is that right or not?
Answer
It is acceptable and even desirable if the contract says so: 404 does not reveal that such an id exists. Another acceptable option is 403. The bug starts when 200 or any foreign data is returned. The test checks that the status is from the allowed set and that the body contains no foreign data.
A client repeated a payment with the same Idempotency-Key, and the account has two debits. What exactly went wrong?
Answer
The server did not link the key to the result of the first request, so it processed the repeat as a new payment. The right behaviour: return the same stored response, with no new debit. The test should compare the number of debits and the transfer id, not only the status of the repeat.