Skip to main content

Masking tokens and card numbers in request records and logs

Mask headers, query arguments and body fields before a request record is stored, mask what your loggers write with data-mask, and audit every body view.

By Efsun Balcı Updated Security

What you will have at the end

Two kinds of masking, measured on one Route. First, the masks of Traffic Explorer: a card number in a request body that was stored in the clear is stored as asterisks once its JSON path is on the mask list, and the viewing of a body is itself an audit record. Second, data-mask on the Route: a rule per field that changes what your own logger Plugins write, while the backend and the client still see the real values. The article also says plainly what each mechanism does not cover.

Prerequisites

  • A Route that receives JSON. Here it is catalog-api from Caching responses with proxy-cache, which forwards /catalog/* to an echo service, so the article can show what the backend received.
  • Traffic logging turned on under Traffic Explorer › Settings, with Request body capture on. The screenshots below were taken as nora.becker, whose role may view request bodies and manage Routes.

Part one: what the gateway stores

Open Observe › Traffic Explorer › Settings. Under Capture, two switches decide whether bodies are stored at all: Request body, on here, stores a JSON body up to the limit beside it; Response body is off, so records carry response headers and sizes only. The note under them is the point of this article: bodies can carry card numbers and passwords, viewing a body needs the permission for request bodies, and every view is written to the audit log.

Under Masking is the list of values the gateway replaces before a record is stored. On this Environment the request headers are the defaults, authorization, proxy-authorization, cookie, apikey and x-api-key, plus the two that Keycloak and G8 added, x-access-token and x-userinfo; the query arguments token, access_token and apikey; and one body field from an earlier article. Names are case-sensitive, and a body path starts at the root of the JSON, $.card.number or $..password.

The Masking card of Traffic Explorer settings before the change: Request headers authorization, proxy-authorization, cookie, apikey, x-api-key, x-access-token and x-userinfo; Query arguments token, access_token and apikey; one Body field, and the heading Stored as it arrives under them The list the gateway applies to every record of the Environment, and under it what is stored as it arrives.

A record before the mask

A client sends an order with a card:

curl -X POST -H 'content-type: application/json' http://localhost:9281/catalog/orders \
  -d '{"customer":"ACME GmbH","card":{"number":"4111111111111111","holder":"J. Miller","expiry":"12/28"},"amount":1250}'

Open the record in Traffic Explorer, switch to Request, and under Body select Show body. The body is there as it arrived, card number included, because no path named it.

The Request tab of the record: eight headers, no query string, and the stored body of 113 bytes with the card number 4111111111111111, the holder and the expiry in the clear Stored as it arrived. Whoever may view bodies sees the number, here the test number every payment sandbox accepts; the view is in the audit log.

Add the path, send again

Back in Settings › Masking, add $.card.number under Body fields and select Save settings. The note says when it applies: a new mask applies to the records written from now on, and the record above keeps its number. Saving the list is itself a change, written to the audit log as an update of the global rule g8-request-records, which is how the console gives the gateway its masks.

The Masking card after the change: Body fields now list the earlier path and $.card.number, with the help line "JSON bodies only, in requests and in responses" One path added. It applies to request bodies and, when response capture is on, to response bodies.

The same request a few seconds later, and its record:

The Request tab of the new record: the same headers and the same 113-byte body, with the card number stored as eight asterisks and the holder and expiry unchanged The gateway replaced the value before the record left it. The log store never held this number.

The backend received the full number both times: Traffic Explorer masks decide what is stored about a request, not what is sent on.

The view is a record too

Each time a body was shown above, the audit log received an entry, traffic.body_viewed, with the actor, the time, the request record it belongs to and, under Details, the part of the record and the size of the body shown; Traffic Explorer: connect your log store has a screenshot of one. The permission for request bodies is separate from the permission to read records, so a role can read every record of an Environment and still not open a body.

The Event and Target cards of the audit record "Body of a request record viewed": action traffic.body_viewed, result Success, actor nora.becker, and the target request record with a link to open it Who looked, when, at which record. An auditor reads the list without opening a single body.

Part two: what your own loggers write

The masks above belong to G8’s own records. A logger Plugin you add to a Route, http-logger, elasticsearch-logger, syslog or the others from Access logs to the systems you run, writes its own record, and the measurement of this article shows where the line runs: the masked headers of the list, apikey among them, arrive masked in those records too, but a JSON body a logger is told to include arrives as it was sent. For that there is data-mask.

Open the Route, Plugins, Add plugin, data-mask. The form is one table, Request, with a rule per row:

  • Type: body, header or query. Action: replace with a Value, remove, or regex with a Regex and a Value. Name: the header, the query argument, or the JSON path of the body field; a body rule also needs its Body format, json or urlencoded.
  • The article adds two rules: body · replace · $.card.holder · **** · json, and header · replace · user-agent · masked.

The note under the table is the sentence to remember: masking runs after the answer, the upstream and the client saw the value, and only the lines the loggers write carry the mask. Body size limit and Form fields read under More say how large a body is still masked and how many form fields are read; a body over the limit is logged unmasked, and only the gateway’s error log says so.

The data-mask form on catalog-api with two rules in the Request table: body, replace, $.card.holder, ****, json; and header, replace, user-agent, masked; under it the note that masking runs after the answer and the Body size limit and Form fields read at their defaults A rule per field. The note says who still sees the real value: the backend and the client.

Select Add plugin. For the measurement, an http-logger with Include request body on was added to the same Route, pointing at a small endpoint that prints what it receives. The same order again, and what the backend echoed back:

backend saw json: {'amount': 1250, 'card': {'expiry': '12/28', 'holder': 'J. Miller', 'number': '4111111111111111'}, 'customer': 'ACME GmbH'}
backend saw user-agent: ['curl/8.7.1']

What the logger delivered for the same request:

request.body: {"customer":"ACME GmbH","amount":1250,"card":{"holder":"****","expiry":"12/28","number":"4111111111111111"}}
user-agent: masked

The holder and the user agent carry the rules; the card number does not, because no data-mask rule names it, and the Traffic Explorer path $.card.number applies to G8’s record and not to a logger’s copy of the body. A field that must never reach any log therefore needs both: the path under Traffic Explorer › Settings for G8’s records, and a data-mask rule for every logger that includes the body. The G8 record of this request, opened the same way as above, shows the holder and the user agent as sent: data-mask does not change it.

What the audit log keeps

Three kinds of entries came out of this article: the global rule update when the mask list was saved, with the old and the new rule side by side; the Route update that added data-mask, with the two rules as the difference; and one traffic.body_viewed entry per body shown, naming the actor and the record. None of them holds the card number.

What to do next

Written by
Efsun Balcı
Engineering intern, G8
Was this helpful?
Next step

See G8 in a demo

We’ll show you the console with APIs and AI traffic like yours, and answer the questions these articles leave open.

  1. 01Demo
  2. 02Proof of conceptOn Docker for a quick start, or on the infrastructure you plan to run G8 on.
  3. 03LicenseWe prepare a quote for your edition and nodes, and you receive a signed license file.