learning-it by example

#98 AI security and governance 5 min

Audit Logs for AI Apps: Tamper-Evident Logging in Python

I edited one line of my own audit log. It noticed. Then I edited the last line.

1. In a hash-chained JSON-lines audit log for an AI assistant, how does each line vouch for the line before it?
2. After six requests from a pretend assistant the hash-chained log had 20 lines. What did the chain check report when a line in the middle (line 9) was edited by hand, and when the last line was edited?
3. Why does the hash chain not notice an edit to the very last line?
4. How does the code keep raw email addresses out of the log?
5. Where should the latest hash of the log be kept, so that edits to the end can be noticed?

Practice only: nothing is saved and no score counts towards anything. Each answer comes with the reason.

  1. Add a seventh request

    Copy the code folder first. In compare.py add a seventh request to REQUESTS: ('dan', 'refund order D-1001, mail me at dan@example.org'). Run compare.py. Predict the number of lines and the users who used a write tool. You should see 24 lines, 7 of 7 requests with a start and an end, the write users ana, ben and dan, and 0 raw email addresses. Remove the new request again afterwards.

    A hint

    A refund request writes a start, a tool call, a tool result and an end line.

  2. Edit a different middle line

    In compare.py the middle edit changes line 9. Change it so that it edits line 2 instead: use middle = lines[:1] + [lines[1].replace('read', 'write')] + lines[2:]. Predict where the chain breaks, then run it. You should see the chain break at line 2, and the edit to the last line still not detected. Put the original middle line back afterwards.

    A hint

    The edited line is the one reported, because the line after it no longer matches.

  3. Turn the masking off

    In app.py change prompt=mask(prompt) to prompt=prompt in the start event, then run compare.py. Make sure REQUESTS has only the original six requests. Predict how many raw email addresses the log now has. You should see 2, and the log still intact. Change it back afterwards.

    A hint

    Two of the six prompts contain an email address.

  4. Predict a deleted line

    Think before you run anything. Suppose someone deletes line 4 from the middle of the log file, or the very last line. Predict what broken_at in audit.py reports in each case, using what the video says about the last line. In a copy of the folder you can test this by writing the file back without that line.

    A hint

    Ask which line would normally vouch for the deleted line.

These tasks are for you to try on your own machine. Nothing is checked or saved.

What this does not show

A pretend assistant with no model, six requests, one log file on one disk. The chain does not catch an edit to the last line, lines deleted from the end, or a rewrite of the whole file, unless the latest hash is kept somewhere else. No timestamps, so that the output repeats; one email pattern is not full masking of personal data; hashes are cut to 12 characters.

Up next in AI security and governance

More in AI security and governance

All AI security and governance videos

Related videos