In open banking the account system knows nothing about consent. A third-party provider asks for a balance, and something has to check the customer agreed. This post builds bank-openbanking, a PSD2-style account information API where the Nano Gateway is that something.

Read this first

I have not run this API on a Nano Gateway. I had no management login for the cluster, so I could not publish it. Most posts in this series were measured on a live gateway. This one was not.

What I checked offline:

  • apic lint passes.
  • The Lua consent check runs under Lua 5.5 and LuaJIT against a test harness.
  • Every JSONata expression passes a subset check for what the gateway supports, and the guard expressions have unit tests (42 assertions for this API).
  • tests/bank-openbanking.sh passes a bash syntax check and a stub-server smoke run.

Where I rely on gateway behaviour I have not seen, I say so, and the list of assumptions is at the end. If you can publish the project, the test script will tell you which ones were wrong.

The API

Operation Purpose
POST /consents A provider asks to read an account. Returns a consent ID.
GET /accounts/{accountId}/balances Reads balances. Needs a consent that covers balances.
GET /accounts/{accountId}/transactions Reads transactions between two dates. Needs a consent that covers transactions.

The provider sends the consent ID in X-Consent-Id and the scopes it was granted in X-Consent-Scope, for example accounts,balances. In a real deployment the gateway would look those up. Here they are headers, which is enough to show where the checks go. The limit of that shortcut is in the notes.

CORS: one origin, everything else denied

kind: CORS
spec:
  rules:
    - action: allow
      originList:
        - https://tpp.example.test
      allowMethods: [GET, POST, OPTIONS]
      allowHeaders: [Authorization, Content-Type, X-Request-Id, X-Consent-Id, X-Consent-Scope]
      exposeHeaders:
        predefined: true
        backend: true
      allowCredentials: false
    - action: deny
      originList:
        - '*'

CORS is a browser control. It does nothing to curl. It still matters here, because a provider’s web app is the likely client, and a wide-open origin lets any page in the customer’s browser call the API.

The test sends a preflight OPTIONS from the allowed origin and expects Access-Control-Allow-Origin to match it. It sends one from https://other.example.test and expects no Access-Control-Allow-Origin header. That second assertion depends on the catch-all deny rule behaving as I read the documentation. That is an assumption.

Block: scanners and debug flags

Block rejects a request on an exact match, and the answer is a bare 403, as the traffic post showed. Two policies:

kind: Block
spec:
  header:
    denyList:
      - name: User-Agent
        value: sqlmap/1.7.2
      - name: User-Agent
        value: Nikto/2.1.6
---
kind: Block
spec:
  queryParam:
    denyList:
      - name: debug
        value: 'true'
      - name: admin

The second has two entries of different kinds. debug with the value true is blocked. admin has no value, so it is a query flag. The product documentation says queryParam supports flags, that is keys without values. I read that as blocking the parameter whatever the value, and have not tested it.

I wanted a regular expression for the user agents, so one rule would cover sqlmap/*. The version of apic lint I used rejects valueType: regex on Block, so these are exact matches. A scanner that changes its version string walks through. This control stops lazy tools, not a determined attacker. The test script checks that debug=false is not blocked, so the value match is exact.

After the Block policies comes an SQLInjectionFilter on the URI. As in the traffic post, that filter blocked with a 500 and missed %20-encoded payloads, so the test accepts 400, 403 or 500.

Three If and Throw pairs:

kind: If
spec:
  condition: $not($exists($header('request', 'X-Consent-Id')))
  # -> 403 "no consent supplied"
---
kind: If
spec:
  condition: $not($exists($header('request', 'X-Consent-Scope')))
  # -> 403 "consent scope is missing"
---
kind: If
spec:
  condition: $not($contains($header('request', 'X-Consent-Scope'), 'balances'))
  # -> 403 "consent does not cover balances"

Order matters. $contains on a missing header gives no result, and an If cannot evaluate that, so the presence check runs first. I saw that behaviour in the earlier JSONata work, not in this project. The transactions route has the same pair with transactions. The test calls balances with a scope of transactions and expects the 403 and the message.

The bearer token is checked for presence and forwarded to the account system with SetAuthorization, as in the payments post.

Transform: normalise the request ID

A Transform upper-cases X-Request-Id, so logs and the backend see one form:

kind: Transform
spec:
  messageHeader:
    messageName: request
    jsonataTransforms:
    - headerNames:
        - X-Request-Id
      transform: $uppercase($header('request', 'X-Request-Id'))

The response echoes the ID, and the test expects req-abc to come back as REQ-ABC.

A rate limit that only applies to some calls

PSD2 limits how often a provider may read an account without the customer present, to four times a day. A customer-present call carries the customer’s IP address in PSU-IP-Address. So the limit applies only when that header is missing:

kind: If
spec:
  condition: $not($exists($header('request', 'PSU-IP-Address')))
  execute:
    - $ref: bank-openbanking:bank-openbanking-unattended-limit:1.0.0
---
kind: RateLimitDef
spec:
  max: 4
  intervalLen: 1
  intervalUnit: day
  extensions:
    dp-nano-gateway:
      apiVersion: api.ibm.com/v1
      spec:
        dynamicValue: $pathParameter('accountId')

The counter is per account. The test makes four unattended calls expecting 200, then a fifth expecting 429, then a call with PSU-IP-Address expecting 200.

A caller can send any value in PSU-IP-Address, including a made-up one, and skip the limit. In production that header needs to be tied to something the gateway can trust. As written, this is a demonstration of a conditional limit, not a control.

Cache the balance for ten seconds

kind: Cache
spec:
  inputs:
    - custom:
        expression: $telemetry('client_id') & '|' & $pathParameter('accountId')
  outputs:
    - messageBody:
        name: response
  expire:
    static:
      seconds: 10
  execute:
    - $ref: bank-openbanking:bank-openbanking-forward-bearer:1.0.0
    - $ref: bank-openbanking:bank-openbanking-invoke-balances:1.0.0
    - $ref: bank-openbanking:bank-openbanking-parse-balances:1.0.0
    - $ref: bank-openbanking:bank-openbanking-balances-response:1.0.0

The key is the client and the account. The unattended limit sits outside the cache, so a cache hit still counts against the daily four. That is the right way round: the limit is about how often a provider asks, not how often the backend is called.

The cached body includes the first caller’s requestId. The test reads the account twice with different request IDs and expects the second answer to carry the first ID. It uses a different account and expects its own ID. That is a deliberate oddity to show what a cache hit means. The cache key is the client and account, not the consent. The guards run before the cache, so a call that fails a check never gets a cached answer. Two consents for the same client and account would share an entry, which is fine for a ten second window.

Transactions: validate the query, then clamp the page size

The OpenAPI document says fromDate is required and both dates match YYYY-MM-DD, and limit is an integer from 1 to 1000. ValidateAPISpecification checks the query string against that:

kind: ValidateAPISpecification
spec:
  input: request
  validate:
    request:
      query: true

Bad dates, limit=abc, limit=0 and limit=5000 all return 400. The Nano Gateway reference documents query, and apic lint accepts it. Whether it enforces pattern, minimum and maximum on parameters is an assumption.

Then a date order guard. JSONata compares strings, and YYYY-MM-DD sorts correctly:

condition: $exists($queryParameter('request', 'toDate')) and $queryParameter('request', 'toDate') < $queryParameter('request', 'fromDate')

The test sends a toDate 60 days before the fromDate and expects 400 with toDate must not be before fromDate.

Next a Switch sets the page size. The provider may ask for up to 1000. The account system only gets up to 100, and the default is 25:

kind: Switch
spec:
  cases:
    - condition: $exists($queryParameter('request', 'limit'))
      execute:
        - $ref: bank-openbanking:bank-openbanking-set-limit-clamped:1.0.0
  otherwise:
    - $ref: bank-openbanking:bank-openbanking-set-limit-default:1.0.0
---
kind: Set
spec:
  valueType: jsonata
  variable:
    name: limit
    value: $string($max([$min([$number($queryParameter('request', 'limit')), 100]), 1]))

The default policy sets the variable to '25'. The invoke builds its URL from the variable. The test checks 7 stays 7, 100 stays 100, 500 becomes 100, 1 stays 1, and no limit gives 25. This matches the clamp pattern from the JSONata post.

Consents: a Lua expiry check

PSD2 consents must be renewed at least every 90 days. The request carries an expirationDate. JSONata has no clock and no date parser, so a Lua script decides:

local y, m, d = tostring(body.expirationDate or ''):match('^(%d%d%d%d)-(%d%d)-(%d%d)$')
if y then
  y, m, d = tonumber(y), tonumber(m), tonumber(d)
  local t = at_noon(y, m, d)
  -- os.time normalises 2026-02-31 to 3 March; a round trip catches that.
  if os.date('%Y-%m-%d', t) == string.format('%04d-%02d-%02d', y, m, d) then
    ...
    if days < 0 then status = 'past'
    elseif days > MAX_DAYS then status = 'too-far'
    else status = 'ok' end
  end
end
req.headers:set('X-Consent-Expiry-Status', status)

Three guards read the status and return 422: invalid, in the past, more than 90 days away. The round trip catches an impossible date such as 31 February. It compares at noon so a daylight saving change cannot move the day.

The script uses os.time and os.date. I have not confirmed the Nano Gateway Lua sandbox allows them. That is the assumption I would check first for this API.

Running the tests

export NANOGW_CLIENT_ID=<client-id>
export NANOGW_CLIENT_SECRET=<client-secret>
export NANOGW_DOMAIN=<gateway-ingress-domain>
./tests/bank-openbanking.sh

76 assertions across access control, consent expiry, schema, balances, Block, SQL injection, the unattended limit, transactions, page size and CORS. Each run uses fresh account IDs, so it can be repeated. NANOGW_SLOW_TESTS=1 adds the 11-second wait that proves the cache expires.

Summary

  • Order the checks: stop known-bad callers first, then validate, then check consent, then spend allowances, then cache.
  • A Cache key should say who is asking and what for. Put guards before it, so the cache never hides a failed check.
  • Use ValidateAPISpecification on the query string to push parameter rules into the OpenAPI document.
  • Use Lua for dates. JSONata cannot read a clock.
  • Block is an exact match. Treat it as a filter for lazy tools.

Limitations and Notes

  • Not run on a gateway. The checks above are offline.
  • Assumptions to confirm on the first run:
    • ValidateAPISpecification with query: true enforces patterns and numeric ranges.
    • A catch-all deny CORS rule stops unlisted origins getting CORS headers.
    • The Lua sandbox allows os.time and os.date.
    • A header set by Lua is visible to a later JSONata If.
    • $telemetry('client_id') works in a Cache key and dynamicValue works as written in the rate limit definition.
  • The consent is a pair of headers sent by the caller. The gateway trusts them. A real design looks the consent up or validates a signed token.
  • The conditional rate limit can be skipped by sending any PSU-IP-Address.
  • Block matches exact values only, so the scanner list is easy to evade.
  • The balances are fixed values in a JSONata expression. The echo backend only proves the gateway sent the account ID and the bearer token.