Security logging and monitoring: record the right events, protect the record, and prove each alert fires
Security logging and monitoring is how an application notices that someone is guessing passwords, changing permissions or asking for records that are not theirs.
This guide covers which security events to record, what each record needs to be useful a month later, how to keep secrets and forged lines out of the log, how to protect the log from the person it records, and how to turn a record into an alert that someone acts on. It comes with an event schema you can copy, three detection rules with tests, redaction fixtures, a review checklist and a lab you can run on your own computer.
In this guide
Security event schema, detection tests, redaction fixtures and review checklist
Part 1 is a security event schema you can copy: eleven fields with the source behind each, and the event names for sign-in, privilege change and object denial. Part 2 is three written detection rules, each with a test that fires it and a near miss that has to stay quiet.
How to use itHide the instructions
Part 3 is the redaction fixtures. Part 4 is a review checklist for your own application, covering timestamps, correlation identifiers, event coverage, content, retention, access, integrity, alerting and testing. Part 5 is an illustration, not a monitoring product: an invented member portal on 127.0.0.1 in a careless and a careful version, a detector, a redaction scan and a hash-chain check, in eight files that import only node: modules and install no packages. Every output was copied from one recorded run on Node.js 26.0.0.
security-logging-and-monitoring.txt
Introduction11 lines
Part 1: The security event schema, version 198 lines
Part 2: The three detection rules37 lines
Part 3: The redaction fixtures35 lines
Part 4: The review checklist for your own application134 lines
Part 5: The lab (an illustration)1,907 lines
Whole template2,252 lines
SECURITY EVENT SCHEMA, DETECTION TESTS, REDACTION FIXTURES AND REVIEW CHECKLIST
Use it on an application you own, or one you are authorised in writing to test, with test accounts and test records.
Part 1 is the security event schema. It works without the lab.
Part 2 is the three detection rules, each with a test that fires it and a near miss that has to stay quiet.
Part 3 is the redaction fixtures: what to send, what to search the log for, and what a pass does and does not prove.
Part 4 is the review checklist for your own application.
Part 5 is the lab: an illustration with an invented application that runs all of the above on your own computer.
Wherever a line rests on a source, the source is named with its requirement number and level. Wherever it rests on judgement, it says "this page's advice".
================================================================
PART 1: THE SECURITY EVENT SCHEMA, VERSION 1
================================================================
One JSON object per line. Eleven fields, written in this order. Copy the field list, then change the event names and the detail keys to suit your application.
THE ELEVEN FIELDS
1. schema_version
Holds: the version of this field list. The lab writes "1".
Basis: this page's advice. The OWASP Logging Cheat Sheet, Note B, says each organisation should ensure it has a consistent, and documented, approach to classification of events (type, confidence, severity), the syntax of descriptions, and field lengths and data types including the format used for dates/times. A version on every line says which edition of that documented approach the line was written under.
2. time
Holds: when the event happened, as the server's clock had it, in UTC: a date, the letter T, a time with three fraction digits, and Z. From the lab: 2026-03-07T23:07:00.000Z
Basis: ASVS 5.0 requirement 16.2.2, Level 2: time sources for all logging components are synchronised, and timestamps in security event metadata use UTC or include an explicit time zone offset. It recommends UTC to ensure consistency across distributed systems and to prevent confusion during daylight saving time transitions.
RFC 3339 section 5.6 defines the Internet Date/Time Format, a profile of ISO 8601, and says it SHOULD be used in new protocols on the Internet. In its grammar a date-time is a full-date, the letter T and a full-time, and the offset is either Z or a numeric offset. Section 2 defines Z as a suffix which denotes a UTC offset of 00:00. Its first example in section 5.8 is 1985-04-12T23:20:50.52Z.
RFC 9557, published in April 2024, updates RFC 3339 in the interpretation of Z: section 2.2 aligns it with the practice of reading Z to mean the same as -00:00, "the time in UTC is known, but the offset to local time is unknown". The format in section 5.6 of RFC 3339 is what this field uses.
The OWASP Application Logging Vocabulary Cheat Sheet notes that all dates should be logged in ISO 8601 format with UTC offset to ensure maximum portability.
3. app
Holds: which application wrote the event. The lab writes garden-portal.
Basis: OWASP Logging Cheat Sheet, Event attributes, under Where: an application identifier, with name and version as its examples. The lab writes a name and no version.
4. route
Holds: the entry point, as a method and a route pattern. From the lab: GET /api/invoices/:id
Basis: OWASP Logging Cheat Sheet, under Where: window, form or page, with the entry point URL and HTTP method for a web application as an example. Writing the pattern in place of the address that was sent is this page's advice: it keeps query strings and odd path text out of this field.
5. event
Holds: what happened, as one name from a fixed list. The list for three families of event is below.
Basis: OWASP Logging Cheat Sheet, under What: type of event. The names come from the vocabulary proposed in the OWASP Application Logging Vocabulary Cheat Sheet. Keeping the name as a fixed string, with identifiers in fields of their own, is this page's advice; the vocabulary itself writes the user id into the event string after a colon.
6. outcome
Holds: success, failure or denied.
Basis: OWASP Logging Cheat Sheet, under "Additionally consider recording": result status, whether the action aimed at the object was successful, with Success, Fail and Defer as its examples. The three words used here are this page's choice. For an event the application raises about its own action, such as a lock, success means the action was applied.
7. actor
Holds: who acted, as an object with two keys. id is the account identifier, never a name or an email address, and is null when nobody is signed in. roles is the list of roles the account held when the request arrived.
Basis: ASVS 5.0 requirement 16.2.1, Level 2: each log entry includes necessary metadata (such as when, where, who, what) that would allow for a detailed investigation of the timeline when an event happens.
OWASP Logging Cheat Sheet, under Who: user identity, if authenticated or otherwise known, with a user database table primary key value, a username and a licence number as its examples.
Choosing the identifier over a name is this page's advice, guided by the cheat sheet's second list under Data to exclude, where non sensitive personal data, with personal names, telephone numbers and email addresses as its examples, is data that may need to be treated in some special manner before the event is recorded.
8. object
Holds: what the action was aimed at, as an object with two keys: type (account or invoice in the lab) and id. For a sign-in, the object is the account the attempt was aimed at.
Basis: OWASP Logging Cheat Sheet, under "Additionally consider recording": object, with the affected component or other object (user account, data resource, file) as its examples.
9. source
Holds: where the request came from, as an object with one key, ip: the address the server saw the request arrive from. In the lab every request arrives from 127.0.0.1.
Basis: OWASP Logging Cheat Sheet, under Who: source address, with the user's device or machine identifier and the user's IP address among its examples.
The OWASP Application Logging Vocabulary Cheat Sheet's note on data privacy says logging a user IP address may be useful for detection and response, but may be considered personally identifiable information when combined with other data and subject to regulation or deletion requests.
Behind a proxy or a content delivery network, deciding which address is the client's is a question of its own. The rate limiting guide in this library covers it.
10. correlation_id
Holds: a random identifier created once for each request and written on every event that request produces. The lab also returns it to the caller in a response header it names x-correlation-id, which is the lab's own name for it.
Basis: OWASP Logging Cheat Sheet, under When: interaction identifier. Its Note A calls it a method of linking all (relevant) events for a single user interaction, with a web page request among its examples.
If your services already propagate W3C Trace Context, the trace-id field of the traceparent header is an identifier of this kind. Section 6 of that Recommendation says tracing vendors MUST NOT use the traceparent and tracestate fields for any personally identifiable or otherwise sensitive information. Using it as the correlation identifier is this page's advice.
11. detail
Holds: the few extra facts an event needs, as an object whose allowed keys are fixed for each event name. A reason is one word from a fixed list.
Basis: OWASP Logging Cheat Sheet, under "Additionally consider recording": reason, why the status occurred. Fixing the keys for each event name is this page's advice.
THREE MORE FIELDS, FOR THE INTEGRITY CHECK
The lab's careful logger adds these to the end of every line. They are not part of the event.
seq the position of the entry in the log, counting from 1
prev_hash the entry_hash of the entry before it, or 64 zeros for the first entry
entry_hash the SHA-256 hash of the entry's JSON text without this field
Basis: this page's advice. The OWASP Logging Cheat Sheet, under Protection, says to build in tamper detection so you know if a record has been modified or deleted. The OWASP Top 10:2025 page for A09 has a How to prevent list, which opens by saying developers should implement some or all of its controls, depending on the risk of the application. One of them is to ensure all transactions have an audit trail with integrity controls to prevent tampering or deletion, such as append-only database tables or similar.
THE EVENT NAMES, IN THREE FAMILIES
Sign-in
authn_login_success
outcome: success. actor: the account. object: the same account.
detail: failures_before, the number of failed attempts in a row before this one.
authn_login_fail
outcome: failure. actor: id is null, because nobody has been authenticated. object: the account the attempt was aimed at, with a null id when the username matches no account.
detail: reason, one of wrong_password, unknown_account or account_locked. consecutive_failures, the count so far.
authn_login_lock
outcome: success, meaning the lock was applied. actor: id is null. object: the account that was locked.
detail: reason, max_failures. consecutive_failures, the count that caused the lock.
Basis: ASVS 5.0 requirement 16.3.1, Level 2: all authentication operations are logged, including successful and unsuccessful attempts. Additional metadata, such as the type of authentication or factors used, should also be collected. The lab has one kind of sign-in and records no such metadata.
The vocabulary describes authn_login_success as "All login events should be recorded including success", authn_login_fail as "All login events should be recorded including failure", and authn_login_lock as: when the feature exists to lock an account after x retries or other condition, the lock should be logged with relevant data. The reasons the vocabulary lists for a lock are maxretries, suspicious, customer and other. The reason words in this schema are the lab's own.
The lab keeps nothing of a typed username that matches no account. That is this page's advice.
Privilege change
authz_change
outcome: success. actor: the account that made the change, with the roles it held when the request arrived. object: the account that was changed.
detail: change, grant or remove. role, the role granted or removed.
Basis: the vocabulary describes authz_change as: the user or entity entitlements was changed. The OWASP Logging Cheat Sheet's list of what to always log, where possible, has use of higher-risk functionality, including user administration actions such as addition or deletion of users and changes to privileges.
Object denial
authz_fail
outcome: denied. actor: the account that asked, or a null id when there was no session. object: the type and id of what was asked for.
detail: reason, one of no_session, not_owner, not_found or role_required.
Basis: ASVS 5.0 requirement 16.3.2, Level 2: failed authorisation attempts are logged. For Level 3, this must include logging all authorisation decisions, including logging when sensitive data is accessed (without logging the sensitive data itself).
The vocabulary describes authz_fail as: an attempt was made to access a resource which was unauthorised.
The lab writes a request for a record id that does not exist under this name, with the reason not_found, so that an account working through guessed ids shows up in one place. It writes a refused role change under this name too, with the reason role_required and the account as the object. Both are this page's choices, not the vocabulary's.
WHAT THE SCHEMA LEAVES OUT ON PURPOSE
No username, email address or name. No request body. No session cookie value, and no hash of one. No free-text message.
WHAT THE LAB DOES NOT LOG, AND YOUR APPLICATION MAY NEED TO
An allowed read or a completed payment writes no event. A request with an unknown role or change word is answered 400 and writes no event. Part 4 lists what the cited standards name beyond these three families.
================================================================
PART 2: THE THREE DETECTION RULES
================================================================
Each rule is written in words first, then in code in detector.mjs. Every threshold, window and priority is the lab's own choice, not a recommendation. Choose yours from your own normal traffic.
RULE 1: failed_signin_burst
Reads: authn_login_fail events whose object has an account id.
Fires when: 5 of them for one account fall inside 600 seconds.
Priority: high.
Alert fields: rule, priority, account, count, window_seconds, first_time, last_time, correlation_ids, first_step.
First step: Check whether a sign-in for this account succeeded after the failures.
Test, fires: five wrong passwords for one account, one minute apart. One alert, account acct_m2, count 5.
Test, quiet: four wrong passwords, then the right one.
Does not see: failures for usernames that match no account, and attempts spread thinly across a list of accounts. The credential stuffing guide in this library covers measures for those.
RULE 2: privilege_change
Reads: authz_change events.
Fires when: any one is written.
Priority: high, or urgent when the actor's account id is the object's account id.
Alert fields: rule, priority, actor, account, change, role, self_change, last_time, correlation_ids, first_step.
First step: Ask the person named as actor, through another channel, whether they made this change.
Test, fires: the admin grants a role to another account, removes a role from a third, and grants a role to their own. Three alerts: high, high, urgent.
Test, quiet: a member asks to grant themselves the admin role and is refused with a 403. The log holds an authz_fail, and this rule raises nothing.
Does not see: a change made directly in the database, which writes no event.
RULE 3: repeated_object_denials
Reads: authz_fail events that have an actor account id and the reason not_owner or not_found.
Fires when: 4 of them by one account fall inside 300 seconds.
Priority: high.
Alert fields: rule, priority, actor, count, objects, window_seconds, first_time, last_time, correlation_ids, first_step.
First step: List the records this account asked for and who owns them.
Test, fires: four requests for other members' invoices, half a minute apart. One alert, actor acct_m1, count 4, four invoice ids.
Test, quiet: three requests a minute apart and a fourth 390 seconds after the first.
Does not see: requests with no session, which have no account to count against, and requests that were allowed.
HOW A BURST IS COUNTED
Rules 1 and 3 report a burst when a run of consecutive events for one account, as long as the threshold, falls inside the window. After a burst is reported, the same account is not reported again until a full window has passed since the event that completed it.
WHAT EVERY QUIET TEST ALSO CHECKS
That the detector skipped no line and read the expected number of events. A detector that reads nothing raises nothing.
================================================================
PART 3: THE REDACTION FIXTURES
================================================================
A redaction fixture is a marked value sent in a request so that a test can search the log for it afterwards. The lab sends five kinds.
1. A correct password and a wrong password
Sent as: the password field of two sign-in requests. The values are LAB-PASSWORD-ROWAN and LAB-WRONG-PASSWORD-0001.
A pass: neither value is in any line of the log.
Why: ASVS 5.0 requirement 16.2.5, Level 2, gives credentials as an example of data it may not be allowed to log. The OWASP Logging Cheat Sheet's first list under Data to exclude has authentication passwords. MITRE's CWE-778, in its second example, says passwords should never be part of the log message. Searching for the wrong password as well as the right one is this page's advice.
2. A session cookie value
Sent as: the garden_session cookie on each request the member makes after signing in. The value is random and comes from the sign-in response.
A pass: the value is in no line of the log.
Why: requirement 16.2.5 says data such as session tokens may only be logged by being hashed or masked, either in full or partially. The cheat sheet's first list has session identification values, with the suggestion to consider replacing them with a hashed value if needed to track session specific events.
3. A card number
Sent as: the card_number field of a request to pay an invoice that belongs to someone else. The value is 0000 0000 0000 0001, invented for the lab: zeros with a counter.
A pass: the value is in no line of the log.
Why: requirement 16.2.5 gives payment details as an example of data it may not be allowed to log. The cheat sheet's first list has bank account or payment card holder data.
4. A record id with line breaks
Sent as: the id in the address of a request to read an invoice: an id that does not exist, a line break, a line shaped like the careless log's sign-in line, a line break, and a line shaped like a JSON event. Both forged lines carry the marker FORGED-BY-FIXTURE.
A pass: the log has as many lines as entries written, no line begins with forged text, and the log appears to record no sign-in that did not happen.
Why: ASVS 5.0 requirement 16.4.1, Level 2: all logging components appropriately encode data to prevent log injection. MITRE's CWE-117 describes a product that constructs a log message from external input without neutralising special elements. Among its defences, the cheat sheet says not to use unstructured text log formats and to use structured log formats, such as JSON.
5. An over-long record id
Sent as: an id of 500 characters in the address of a request to read an invoice.
A pass: no line holds all 500 characters, and one line holds the first 200 followed by the marker (cut).
Why: the cheat sheet's defences against log injection include limiting the size of the user input value used to create the log message. The limit of 200 is the lab's own choice.
HOW THE SCAN WORKS
It searches every line of the log for each exact value. It uses exact values and not patterns, so it has no false matches and no misses on the values it was given.
WHAT A PASS DOES NOT PROVE
The scan covers the values it was given, on the routes the fixtures used. A secret in a field the fixtures never fill, or on a route they never call, is outside it. Add a fixture whenever a route starts accepting a new kind of secret.
THE CASE THE FIXTURES CANNOT COVER FOR YOU
A value that is allowed in a field is written as given. In the lab, the record id in a denied request is whatever the caller put in the address, encoded and cut to 200 characters. If a caller pastes a card number into an address, it will be in the id field. Decide whether a field that takes text from the request needs a rule of its own. That is this page's advice.
================================================================
PART 4: THE REVIEW CHECKLIST FOR YOUR OWN APPLICATION
================================================================
Work through it with the person who owns the application and the person who would be called when an alert fires. Each item names its source and that source's conditions, or says it is this page's advice. Where an item cites A09's How to prevent list, read it with the line that opens that list: developers should implement some or all the following controls, depending on the risk of the application.
TIMESTAMPS
1. Source of time. Does every component that writes events take its time from a synchronised source?
ASVS 5.0 requirement 16.2.2, Level 2: time sources for all logging components are synchronised. OWASP Logging Cheat Sheet, Event collection: synchronise time across all servers and devices. Its Note C says this is not always possible where the application is running on a device under some other party's control, and in those cases to attempt to measure the time offset, or record a confidence level in the event timestamp.
2. Format. Is every event's time written in one format?
RFC 3339 section 5.6 defines the Internet Date/Time Format and says it SHOULD be used in new protocols on the Internet. ASVS 5.0 requirement 16.2.4, Level 2: logs can be read and correlated by the log processor that is in use, preferably by using a common logging format.
3. Time zone. Is every time in UTC, or written with an explicit offset?
Requirement 16.2.2: timestamps in security event metadata use UTC or include an explicit time zone offset, and UTC is recommended.
4. Event time and log time. Where an event can be written later than it happened, are both times kept?
The cheat sheet lists both a log date and time and an event date and time, and says the event timestamp may be different to the time of logging, with a client application on a remote device that is only periodically or intermittently online as its example.
5. A test. Does a test fail when an event's time is not in the agreed format?
This page's advice. The lab's tests assert it on every entry.
CORRELATION IDENTIFIERS
1. Where it is created. Is the identifier created on the server, at the first component you control?
This page's advice.
2. Which services receive it. Is there a list of the services it is passed to, and does each write the same value?
This page's advice. W3C Trace Context is a Recommendation that defines standard HTTP headers and a value format to propagate context information that enables distributed tracing scenarios.
3. On every event. Does every security event of one request carry the same identifier?
OWASP Logging Cheat Sheet, Note A: the application knows all these events relate to the same interaction, and this should be recorded instead of losing the information and forcing subsequent correlation techniques to re-construct the separate events. The lab's tests assert it.
4. Back to the caller. Is the identifier returned, so that a support request can quote it?
This page's advice.
5. About nothing. Is it random, with no account id, address or time folded into it?
W3C Trace Context, section 6, says tracing vendors MUST NOT use the traceparent and tracestate fields for any personally identifiable or otherwise sensitive information. Applying the same rule to an identifier of your own is this page's advice.
EVENT COVERAGE
1. Sign-in: success, failure and lock.
ASVS 5.0 requirement 16.3.1, Level 2: all authentication operations are logged, including successful and unsuccessful attempts. Additional metadata, such as the type of authentication or factors used, should also be collected.
2. Privilege change: who gave or removed what, for whom.
OWASP Logging Cheat Sheet, Which events to log: use of higher-risk functionality, including user administration actions such as addition or deletion of users, changes to privileges, assigning users to tokens, adding or deleting tokens.
3. Object denial: who asked for which record and why it was refused.
ASVS 5.0 requirement 16.3.2, Level 2: failed authorisation attempts are logged. For Level 3, this must include logging all authorisation decisions, including logging when sensitive data is accessed (without logging the sensitive data itself).
4. Attempts to get around a control.
ASVS 5.0 requirement 16.3.3, Level 2: the application logs the security events that are defined in the documentation and also logs attempts to bypass the security controls, such as input validation, business logic, and anti-automation.
5. Errors and failed controls.
ASVS 5.0 requirement 16.3.4, Level 2: the application logs unexpected errors and security control failures such as backend TLS failures.
6. The rest of the cheat sheet's list. Under "where possible, always log" it has: input validation failures; output validation failures; authentication successes and failures; authorisation (access control) failures; session management failures; application errors and system events; application and related systems start-ups and shut-downs, and logging initialisation; use of higher-risk functionality; legal and other opt-ins; and suspicious business logic activities.
Under "optionally consider" it has: sequencing failure; excessive use; data changes; fraud and other criminal activities; suspicious, unacceptable or unexpected behaviour; modifications to configuration; and application code file and/or memory changes.
7. Both outcomes of every control.
OWASP Top 10:2025, A09, one of the controls under How to prevent: ensure that every part of your app that contains a security control is logged, whether it succeeds or fails. Its first sign is auditable events, such as logins, failed logins, and high-value transactions, not being logged or being logged inconsistently.
8. Nobody left out.
OWASP Logging Cheat Sheet, Data to exclude: never exclude any events from "known" users such as other internal systems, "trusted" third parties, search engine robots, uptime/process and other remote monitoring systems, pen testers, auditors. It adds that you may want to include a classification flag for each of these in the recorded data.
9. Written down.
ASVS 5.0 requirement 16.1.1, Level 2: an inventory exists documenting the logging performed at each layer of the application's technology stack, what events are being logged, log formats, where that logging is stored, how it is used, how access to it is controlled, and for how long logs are kept.
CONTENT
1. The four answers. Does each entry say when, where, who and what?
ASVS 5.0 requirement 16.2.1, Level 2: each log entry includes necessary metadata (such as when, where, who, what) that would allow for a detailed investigation of the timeline when an event happens.
2. Credentials, payment details and session tokens.
ASVS 5.0 requirement 16.2.5, Level 2: when logging sensitive data, the application enforces logging based on the data's protection level. For example, it may not be allowed to log certain data, such as credentials or payment details. Other data, such as session tokens, may only be logged by being hashed or masked, either in full or partially.
3. The cheat sheet's first list. It says the following should usually not be recorded directly in the logs, but instead should be removed, masked, sanitised, hashed, or encrypted: application source code; session identification values (consider replacing with a hashed value if needed to track session specific events); access tokens; sensitive personal data and some forms of personally identifiable information, with health, government identifiers and vulnerable people as its examples; authentication passwords; database connection strings; encryption keys and other primary secrets; bank account or payment card holder data; data of a higher security classification than the logging system is allowed to store; commercially-sensitive information; information it is illegal to collect in the relevant jurisdictions; and information a user has opted out of collection, or not consented to.
4. The cheat sheet's second list. It says the following can sometimes also exist, and while useful for subsequent investigation, may also need to be treated in some special manner before the event is recorded: file paths; database connection strings; internal network names and addresses; and non sensitive personal data, with personal names, telephone numbers and email addresses as its examples.
5. The opening rule of the same section: never log data unless it is legally sanctioned. Its examples are that intercepting some communications, monitoring employees, and collecting some data without consent may all be illegal. What applies to your logs is a question for your own adviser, not for this page.
6. Personal data in general.
ASVS 5.0, V16 control objective: logs should not include sensitive personal data unless legally required, and any logged data must be protected as a high-value asset. ASVS 5.0 requirement 14.1.2, Level 2, asks that every protection level for sensitive data has a documented set of protection requirements, and its list of what those must include has how the data is to be logged and access controls around sensitive data in logs. The sensitive data exposure guide in this library works through a record field by field.
7. Encoded and bounded.
ASVS 5.0 requirement 16.4.1, Level 2: all logging components appropriately encode data to prevent log injection. OWASP Logging Cheat Sheet: limit the size of the user input value used to create the log message.
8. A test. Does a test search the log for marked secrets after sending them?
This page's advice. Part 3 is the lab's version.
RETENTION
1. Decided. Is there a retention period for each kind of log?
This page gives no period. ASVS 5.0 requirement 16.1.1, Level 2, asks that the inventory documents for how long logs are kept.
2. Its basis written down. Which obligation or need sets each period?
OWASP Logging Cheat Sheet, Disposal of logs: log data, temporary debug logs, and backups/copies/extractions, must not be destroyed before the duration of the required data retention period, and must not be kept beyond this time. It says legal, regulatory and contractual obligations may impact on these periods. Which obligations apply to you is a question for your own adviser.
3. Long enough to investigate.
OWASP Top 10:2025, A09, one of the controls under How to prevent: ensure all login, access control, and server-side input validation failures can be logged with sufficient user context to identify suspicious or malicious accounts and held for enough time to allow delayed forensic analysis.
4. The copies too. Do backups, exports and debug logs follow the same period?
The cheat sheet's sentence above names backups, copies and extractions.
5. Checked. Is the oldest record you can still read as old as the period says?
This page's advice.
ACCESS
1. Who can read the logs? Is the list short, named and reviewed?
ASVS 5.0 requirement 16.4.2, Level 2: logs are protected from unauthorised access and cannot be modified. OWASP Logging Cheat Sheet, Protection: the privileges to read log data should be restricted and reviewed periodically.
2. Who can delete or change them? Does that list include the application's own account, or the administrators the log records?
Requirement 16.4.2. The cheat sheet says that when using a database, it is preferable to use a separate database account that is only used for writing log data and which has very restrictive database, table, function and command permissions. Asking the question of the administrators the log records is this page's advice.
3. Is reading the logs itself recorded?
OWASP Logging Cheat Sheet, Protection: all access to the logs must be recorded and monitored (and may need prior approval).
4. Can the logs be reached from the web?
OWASP Logging Cheat Sheet: in web applications, the logs should not be exposed in web-accessible locations, and if done so, should have restricted access and be configured with a plain text MIME type (not HTML).
5. Where else do they go?
ASVS 5.0 requirement 16.2.3, Level 2: the application only stores or broadcasts logs to the files and services that are documented in the log inventory. OWASP Logging Cheat Sheet: perform due diligence checks (regulatory and security) before sending event data to third parties.
INTEGRITY
1. How would a change be noticed?
OWASP Logging Cheat Sheet, Protection: build in tamper detection so you know if a record has been modified or deleted. OWASP Top 10:2025, A09, lists among its signs that the integrity of logs is not properly protected from tampering.
2. Does a copy leave the application as it is written?
ASVS 5.0 requirement 16.4.3, Level 2: logs are securely transmitted to a logically separate system for analysis, detection, alerting, and escalation. The aim is to ensure that if the application is breached, the logs are not compromised. The cheat sheet says to store or copy log data to read-only media as soon as possible. A09 lists among its signs that logs are only stored locally, and not properly backed up.
3. Is the journey protected?
OWASP Logging Cheat Sheet, Protection, in transit: if log data is sent over untrusted networks, use a secure transmission protocol.
4. Would you notice logging stopping?
OWASP Logging Cheat Sheet, Operation: enable processes to detect whether logging has stopped, and to identify tampering or unauthorised access and deletion.
5. If you use a hash chain, where is the head kept?
This page's advice, from the lab: a chain alone did not notice the last entry being deleted, or an entry being changed and the later hashes worked out again. A count and a last hash kept where the log's own writers cannot change them noticed both.
6. What it will not tell you.
A hash chain shows that a record changed or is missing, not who changed it. The cheat sheet says non-repudiation is hard to achieve for logs.
ALERTING
1. Which events alert? Is there a written list?
OWASP Top 10:2025, A09, one of the controls under How to prevent: DevSecOps and security teams should establish effective monitoring and alerting use cases including playbooks such that suspicious activities are detected and responded to quickly by the Security Operations Center (SOC) team.
2. Who receives each alert, and how soon is it read?
A09 lists among its signs that alerts are not received or reviewed within a reasonable amount of time. Naming a person or a roster for each alert is this page's advice.
3. What do they do first?
A09 lists among its signs that detected alerts cannot be processed correctly because the playbook for the use case is incomplete, out of date, or missing. Carrying the first step inside the alert, as the lab does, is this page's advice.
4. Why is each threshold what it is?
A09 lists among its signs that appropriate alerting thresholds and response escalation processes are not in place or effective. Recording the reason for each number is this page's advice.
5. How many alerts were false last month?
A09 lists among its signs that too many false positive alerts make it impossible to distinguish important alerts from unimportant ones. The OWASP Logging Cheat Sheet warns that a blind checklist approach can lead to unnecessary "alarm fog" that means real problems go undetected.
6. Do the serious ones arrive at once?
OWASP Logging Cheat Sheet, Monitoring of events: enable alerting and signal the responsible teams about more serious events immediately.
7. Repeated refusals.
OWASP Top 10:2025, A01, recommends logging access control failures and alerting admins when appropriate, such as after repeated failures.
8. What happens after the alert?
OWASP Logging Cheat Sheet, Release: ensure the outputs of the monitoring are integrated with incident response processes. One of the controls under A09's How to prevent is to establish or adopt an incident response and recovery plan. Clever Ops does not provide incident response or monitoring.
TESTING
1. Does each alert have a test that fires it?
This page's advice. A09 lists among its signs that penetration testing and scans by dynamic application security testing (DAST) tools do not trigger alerts.
2. Does each alert have a near miss that has to stay quiet?
This page's advice.
3. Does each quiet test check that the detector read the log?
This page's advice, from the lab: the careless log raised no alerts because none of its lines could be read.
4. Is logging switched on while the application is being tested?
OWASP Logging Cheat Sheet, Verification: ensure logging is implemented and enabled during application security, fuzz, penetration, and performance testing.
5. Has the logging itself been attacked?
OWASP Logging Cheat Sheet, Verification: test the mechanisms are not susceptible to injection attacks, and ensure logging cannot be used to deplete system resources, for example by filling up disk space or exceeding database transaction log space, leading to denial of service.
6. What happens when logging fails?
OWASP Logging Cheat Sheet, Verification: test the effect on the application of logging failures such as simulated database connectivity loss, lack of file system space, missing write permissions to the file system, and runtime errors in the logging module itself.
7. Does a real alert reach a real person?
This page's advice: send a marked test event through the running pipeline on a schedule, and confirm that the named receiver got it.
8. Do the tests run where a failure stops a release?
This page's advice. The Node.js test runner documentation says that if any tests fail, the process exit code is set to 1.
================================================================
PART 5: THE LAB (AN ILLUSTRATION)
================================================================
WHAT IT IS
An invented member portal in two versions, careless and careful, a detector that reads a log and raises alerts from the three rules in Part 2, a script that plays one story against both versions and prints what each log holds, and a set of tests. It is an illustration with an invented application, an in-memory log and a detector written for this page. It is not a monitoring product, and its thresholds are not recommendations. Nothing in it is a client's code or a client's result.
THE GARDEN AND ITS DETAILS
The portal belongs to an invented community garden. It has four accounts: two members (acct_m1 and acct_m2), a coordinator (acct_c1) and an admin (acct_a1), with the usernames rowan, sage, quinn and kit at garden.example. It has five plot-fee invoices. Usernames end in .example, a top-level domain that RFC 2606 reserves and recommends for use in documentation or as examples. Passwords are labels such as LAB-PASSWORD-ROWAN. The card number is 0000 0000 0000 0001, zeros with a counter. Nothing is charged: paying an invoice marks it paid.
WHAT IT NEEDS
Node.js. The eight files import only node:assert/strict, node:crypto, node:http and node:test, and call the global fetch() function. The lab installs no packages. It was written and run on Node.js 26.0.0 on macOS, the only version and the only system it was run on. The Node.js documentation lists the node:test module as stable since v20.0.0 and fetch() as no longer experimental from v21.0.0.
WHAT IT DOES NOT DO
The portal listens on 127.0.0.1, on a port the system picks. Each log is a string in memory, and the lab writes nothing to disk. There is no database, no log collector and no paging system in it: an alert is an object the detector returns. The kept head of the hash chain is returned to the caller, which stands in for storing it on a separate system. Both versions apply the same sign-in, lockout, role and ownership checks, so nothing in the lab is an access control flaw.
THE FOUR ROUTES, THE SAME IN BOTH VERSIONS
POST /api/sign-in anyone username and password, answered with a session cookie
GET /api/invoices/:id signed in the invoice, for its owner or an admin
POST /api/invoices/:id/pay signed in marks the invoice paid, for its owner
POST /api/accounts/:id/roles an admin grants or removes a role
WHAT THE CARELESS LOGGER WRITES, AND WHAT THE CAREFUL LOGGER WRITES INSTEAD
Format Free-text lines built by joining strings. One JSON object per line, in the schema from Part 1.
Time The computer's local time, with no zone. UTC.
Who The username as typed, or the session cookie value. The account id.
Body The whole request body, when there is one. Never read.
Request Nothing ties two lines to one request. A correlation identifier on every event.
Integrity Nothing. Three hash-chain fields on every line.
SETUP
Create an empty folder, save the eight files below into it under the names shown, and open a terminal in that folder.
================================================================
FILE 1 OF 8: garden-data.mjs
================================================================
// garden-data.mjs: the invented accounts, invoices and fixture values the lab uses.
// Illustration only. The community garden, its four accounts and its five
// invoices are invented. Usernames end in .example, passwords are labels and
// the card number is zeros with a counter.
export const APP_NAME = 'garden-portal'
export const ROLES = ['member', 'coordinator', 'admin']
export const ACCOUNTS = [
{ id: 'acct_m1', username: 'rowan@garden.example', password: 'LAB-PASSWORD-ROWAN', roles: ['member'] },
{ id: 'acct_m2', username: 'sage@garden.example', password: 'LAB-PASSWORD-SAGE', roles: ['member'] },
{ id: 'acct_c1', username: 'quinn@garden.example', password: 'LAB-PASSWORD-QUINN', roles: ['member', 'coordinator'] },
{ id: 'acct_a1', username: 'kit@garden.example', password: 'LAB-PASSWORD-KIT', roles: ['member', 'admin'] },
]
// One plot-fee invoice per line. The owner may read and pay it. An admin may
// read it. Nobody else may reach it.
export const INVOICES = [
{ id: 'inv_1001', ownerId: 'acct_m1', status: 'due' },
{ id: 'inv_2001', ownerId: 'acct_m2', status: 'due' },
{ id: 'inv_2002', ownerId: 'acct_m2', status: 'due' },
{ id: 'inv_3001', ownerId: 'acct_c1', status: 'due' },
{ id: 'inv_3002', ownerId: 'acct_c1', status: 'paid' },
]
const NEWLINE = String.fromCharCode(10)
// Two lines of forged text. The first is shaped like the careless log's
// sign-in line and the second like a JSON event.
const FORGED_TEXTS = [
'08/03/2026 09:15:00 LOGIN OK for kit@garden.example session=FORGED-BY-FIXTURE',
'{"schema_version":"1","event":"authn_login_success","actor":{"id":"acct_a1"},"note":"FORGED-BY-FIXTURE"}',
]
// The values the lab sends as fixtures and then searches each log for.
export const FIXTURES = {
wrongPassword: 'LAB-WRONG-PASSWORD-0001',
cardNumber: '0000 0000 0000 0001',
forgedTexts: FORGED_TEXTS,
// A record id with two line breaks in it: an id that does not exist, a
// line break, the first forged text, a line break, the second forged text.
forgedLineId: ['inv_9999'].concat(FORGED_TEXTS).join(NEWLINE),
// A record id of 500 characters.
overLongId: 'L'.repeat(500),
}
================================================================
FILE 2 OF 8: event-schema.mjs
================================================================
// event-schema.mjs: the security event schema, version 1.
// The careful logger writes the keys on the lists in this file, adds the three
// chain fields (seq, prev_hash, entry_hash) and, when it drops keys, a count of
// them in detail. The detector reads a log line only if checkEvent finds no
// problem with it.
export const SCHEMA_VERSION = '1'
// The fixed list of event names. The names follow the vocabulary proposed in
// the OWASP Application Logging Vocabulary Cheat Sheet.
export const EVENT_NAMES = ['authn_login_success', 'authn_login_fail', 'authn_login_lock', 'authz_change', 'authz_fail']
export const OUTCOMES = ['success', 'failure', 'denied']
// The top-level fields of an event, in the order they are written.
export const EVENT_FIELDS = [
'schema_version',
'time',
'app',
'route',
'event',
'outcome',
'actor',
'object',
'source',
'correlation_id',
'detail',
]
// The only keys allowed inside each nested part.
export const PART_FIELDS = {
actor: ['id', 'roles'],
object: ['type', 'id'],
source: ['ip'],
}
// The only keys allowed inside detail, for each event name.
export const DETAIL_FIELDS = {
authn_login_success: ['failures_before'],
authn_login_fail: ['reason', 'consecutive_failures'],
authn_login_lock: ['reason', 'consecutive_failures'],
authz_change: ['change', 'role'],
authz_fail: ['reason'],
}
// The fixed lists of reasons and changes a detail may carry.
export const REASONS = {
authn_login_fail: ['wrong_password', 'unknown_account', 'account_locked'],
authn_login_lock: ['max_failures'],
authz_fail: ['no_session', 'not_owner', 'not_found', 'role_required'],
}
export const CHANGES = ['grant', 'remove']
// A string value longer than this is cut before it is written.
export const MAX_TEXT_LENGTH = 200
export const CUT_MARKER = '(cut)'
// The key the logger adds to detail when it has dropped fields from an event.
export const DROPPED_FIELDS_KEY = 'dropped_fields'
// A UTC time such as 2026-03-07T22:58:00.000Z: date, T, time, three
// fraction digits, Z.
const UTC_TIME_PATTERN = /^[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}[.][0-9]{3}Z$/
export function isUtcTime(value) {
return typeof value === 'string' && UTC_TIME_PATTERN.test(value) && !Number.isNaN(Date.parse(value))
}
function isPlainObject(value) {
return typeof value === 'object' && value !== null && !Array.isArray(value)
}
function listUnknownKeys(value, allowedKeys) {
return Object.keys(value).filter((key) => !allowedKeys.includes(key))
}
function checkParts(event, problems) {
for (const partName of Object.keys(PART_FIELDS)) {
const part = event[partName]
if (!isPlainObject(part)) {
problems.push(partName + ' is not an object')
continue
}
const unknownKeys = listUnknownKeys(part, PART_FIELDS[partName])
if (unknownKeys.length > 0) problems.push(partName + ' has unknown keys: ' + unknownKeys.join(', '))
}
}
function checkDetail(event, problems) {
if (!isPlainObject(event.detail)) {
problems.push('detail is not an object')
return
}
const allowedKeys = (DETAIL_FIELDS[event.event] || []).concat([DROPPED_FIELDS_KEY])
const unknownKeys = listUnknownKeys(event.detail, allowedKeys)
if (unknownKeys.length > 0) problems.push('detail has unknown keys: ' + unknownKeys.join(', '))
const reasons = REASONS[event.event]
if (reasons && !reasons.includes(event.detail.reason)) problems.push('detail.reason is not on the list for ' + event.event)
if (event.event === 'authz_change' && !CHANGES.includes(event.detail.change)) problems.push('detail.change is not grant or remove')
}
// Returns the list of problems with one parsed log entry. An empty list means
// the entry is a version 1 event. The three hash-chain fields the careful
// logger adds (seq, prev_hash, entry_hash) are allowed and not checked here.
export function checkEvent(event) {
if (!isPlainObject(event)) return ['the line is not a JSON object']
const problems = []
const allowedKeys = EVENT_FIELDS.concat(['seq', 'prev_hash', 'entry_hash'])
const unknownKeys = listUnknownKeys(event, allowedKeys)
if (unknownKeys.length > 0) problems.push('unknown top-level keys: ' + unknownKeys.join(', '))
if (event.schema_version !== SCHEMA_VERSION) problems.push('schema_version is not ' + SCHEMA_VERSION)
if (!isUtcTime(event.time)) problems.push('time is not a UTC timestamp')
if (!EVENT_NAMES.includes(event.event)) problems.push('event is not on the list of event names')
if (!OUTCOMES.includes(event.outcome)) problems.push('outcome is not on the list of outcomes')
if (typeof event.correlation_id !== 'string' || event.correlation_id.length === 0) problems.push('correlation_id is missing')
checkParts(event, problems)
checkDetail(event, problems)
return problems
}
================================================================
FILE 3 OF 8: security-log.mjs
================================================================
// security-log.mjs: the two loggers and the hash chain check.
// Each logger holds its log in memory as one string, the way a log file holds
// it. The portal makes the same five calls to both, with the same
// information. They differ in what they write, and in two other ways: only
// the careful logger makes a correlation id and keeps a chain head.
import { createHash, randomUUID } from 'node:crypto'
import { APP_NAME } from './garden-data.mjs'
import {
CUT_MARKER,
DETAIL_FIELDS,
DROPPED_FIELDS_KEY,
EVENT_FIELDS,
MAX_TEXT_LENGTH,
PART_FIELDS,
SCHEMA_VERSION,
} from './event-schema.mjs'
const NEWLINE = String.fromCharCode(10)
// The prev_hash of the first entry: 64 zeros.
export const GENESIS_HASH = '0'.repeat(64)
function hashText(text) {
return createHash('sha256').update(text).digest('hex')
}
// Splits a log's text into lines the way a reader of a log file would.
export function splitLines(text) {
return text.split(NEWLINE).filter((line) => line.length > 0)
}
function padTwoDigits(number) {
return String(number).padStart(2, '0')
}
// CARELESS: the computer's local time as day/month/year, with no time zone.
function formatLocalTime(date) {
const day = padTwoDigits(date.getDate()) + '/' + padTwoDigits(date.getMonth() + 1) + '/' + date.getFullYear()
const time = padTwoDigits(date.getHours()) + ':' + padTwoDigits(date.getMinutes()) + ':' + padTwoDigits(date.getSeconds())
return day + ' ' + time
}
// CARELESS: free-text messages built by joining strings. A message names
// who acted by the username as typed or by the session cookie value, takes a
// record or account id from the address once it is decoded, and ends with the
// whole request body when there is one.
export function createCarelessLogger(clock) {
const state = { text: '', writes: 0 }
function write(message, requestInfo) {
let line = formatLocalTime(clock.now()) + ' ' + message
if (requestInfo.bodyText) line = line + ' body=' + requestInfo.bodyText
state.text = state.text + line + NEWLINE
state.writes = state.writes + 1
}
return {
versionName: 'careless',
newCorrelationId: () => '',
signInSucceeded: (requestInfo, account, failuresBefore, newToken) => {
write('LOGIN OK for ' + requestInfo.body.username + ' session=' + newToken, requestInfo)
},
signInFailed: (requestInfo, account, reason) => {
write('LOGIN FAILED for ' + requestInfo.body.username + ' (' + reason + ')', requestInfo)
},
accountLocked: (requestInfo, account) => {
write('ACCOUNT LOCKED ' + account.username, requestInfo)
},
roleChanged: (requestInfo, actor, target, change, role) => {
write('ROLE CHANGE by session=' + requestInfo.sessionToken + ' ' + change + ' ' + role + ' on ' + target.username, requestInfo)
},
requestDenied: (requestInfo, actor, objectType, objectId, reason) => {
write('DENIED session=' + requestInfo.sessionToken + ' wanted ' + objectType + ' ' + objectId + ' (' + reason + ')', requestInfo)
},
text: () => state.text,
writes: () => state.writes,
head: () => null,
}
}
function cutLongText(value) {
if (typeof value !== 'string' || value.length <= MAX_TEXT_LENGTH) return value
return value.slice(0, MAX_TEXT_LENGTH) + CUT_MARKER
}
// Copies the allowed keys of one part of an event, cuts long strings, and
// counts the keys it left behind.
function pickAllowed(part, allowedKeys, counter) {
const picked = {}
for (const key of Object.keys(part || {})) {
if (!allowedKeys.includes(key)) {
counter.dropped = counter.dropped + 1
continue
}
picked[key] = cutLongText(part[key])
}
return picked
}
// CAREFUL: the last step before writing. It rebuilds the event from the
// schema's field lists, so a key that is not on a list is not written,
// whatever the caller handed over, and it cuts any string value longer than
// MAX_TEXT_LENGTH (strings inside a list, such as role names, are not cut).
// When it drops keys it records how many, not their names
// or values.
export function enforceFieldLists(event) {
const counter = { dropped: 0 }
const checked = pickAllowed(event, EVENT_FIELDS, counter)
for (const partName of Object.keys(PART_FIELDS)) {
checked[partName] = pickAllowed(event[partName], PART_FIELDS[partName], counter)
}
checked.detail = pickAllowed(event.detail, DETAIL_FIELDS[event.event] || [], counter)
if (counter.dropped > 0) checked.detail[DROPPED_FIELDS_KEY] = counter.dropped
return checked
}
// CAREFUL: each entry carries its position, the hash of the entry before it,
// and its own hash, which is the SHA-256 of the entry's JSON text without the
// entry_hash field.
function appendEntry(state, event) {
const entry = Object.assign({}, event, { seq: state.writes + 1, prev_hash: state.lastHash })
const entryHash = hashText(JSON.stringify(entry))
entry.entry_hash = entryHash
state.text = state.text + JSON.stringify(entry) + NEWLINE
state.writes = state.writes + 1
state.lastHash = entryHash
}
// CAREFUL: one JSON object per line. It never reads the request body text,
// the parsed body or the session cookie from the request information. The
// only text it takes from the request is the record or account id in the
// address. It writes a UTC time and the request's correlation id on every
// event. Extra detail a handler passes is merged into detail and then goes
// through enforceFieldLists like everything else.
export function createCarefulLogger(clock) {
const state = { text: '', writes: 0, lastHash: GENESIS_HASH }
function record(requestInfo, parts) {
const event = {
schema_version: SCHEMA_VERSION,
time: clock.now().toISOString(),
app: APP_NAME,
route: requestInfo.route,
event: parts.event,
outcome: parts.outcome,
actor: parts.actor,
object: parts.object,
source: { ip: requestInfo.sourceIp },
correlation_id: requestInfo.correlationId,
detail: parts.detail,
}
appendEntry(state, enforceFieldLists(event))
}
function describeActor(account) {
if (!account) return { id: null, roles: [] }
return { id: account.id, roles: account.roles.slice() }
}
function describeAccount(account) {
return { type: 'account', id: account ? account.id : null }
}
return {
versionName: 'careful',
newCorrelationId: () => randomUUID(),
signInSucceeded: (requestInfo, account, failuresBefore) => {
const detail = { failures_before: failuresBefore }
record(requestInfo, { event: 'authn_login_success', outcome: 'success', actor: describeActor(account), object: describeAccount(account), detail: detail })
},
signInFailed: (requestInfo, account, reason, consecutiveFailures) => {
const detail = { reason: reason, consecutive_failures: consecutiveFailures }
record(requestInfo, { event: 'authn_login_fail', outcome: 'failure', actor: describeActor(null), object: describeAccount(account), detail: detail })
},
accountLocked: (requestInfo, account, consecutiveFailures) => {
const detail = { reason: 'max_failures', consecutive_failures: consecutiveFailures }
record(requestInfo, { event: 'authn_login_lock', outcome: 'success', actor: describeActor(null), object: describeAccount(account), detail: detail })
},
roleChanged: (requestInfo, actor, target, change, role) => {
const detail = { change: change, role: role }
record(requestInfo, { event: 'authz_change', outcome: 'success', actor: describeActor(actor), object: describeAccount(target), detail: detail })
},
requestDenied: (requestInfo, actor, objectType, objectId, reason, extraDetail) => {
const detail = Object.assign({ reason: reason }, extraDetail || {})
record(requestInfo, { event: 'authz_fail', outcome: 'denied', actor: describeActor(actor), object: { type: objectType, id: objectId }, detail: detail })
},
text: () => state.text,
writes: () => state.writes,
// The number of entries written and the hash of the last one. The lab
// returns it to the caller, which stands in for keeping it somewhere the
// log's own writers cannot change.
head: () => ({ count: state.writes, hash: state.lastHash }),
}
}
function parseLine(line) {
try {
return JSON.parse(line)
} catch (error) {
return null
}
}
function checkAgainstKeptHead(lines, lastHash, keptHead, problems) {
if (lines.length !== keptHead.count) {
problems.push('the log holds ' + lines.length + ' line(s) but the kept head says ' + keptHead.count)
return
}
if (lastHash !== keptHead.hash) problems.push('the last entry hash does not match the kept head')
}
// Walks the log from the first line and returns a list of problems. An empty
// list means every line matches its own hash and links to the line before it
// and, when a kept head is given, that the log still ends where the head says.
export function verifyChain(lines, keptHead) {
const problems = []
let previousHash = GENESIS_HASH
let position = 0
for (const line of lines) {
position = position + 1
const entry = parseLine(line)
if (!entry || typeof entry.entry_hash !== 'string') {
problems.push('line ' + position + ': no entry hash, so nothing links it to the line before')
continue
}
const storedHash = entry.entry_hash
delete entry.entry_hash
if (hashText(JSON.stringify(entry)) !== storedHash) problems.push('line ' + position + ': the content does not match its own hash')
if (entry.prev_hash !== previousHash) problems.push('line ' + position + ': prev_hash does not match the hash of the line before')
previousHash = storedHash
}
if (keptHead) checkAgainstKeptHead(lines, previousHash, keptHead, problems)
return problems
}
// Rewrites the hashes of every line from a given position onwards, the way
// someone who can edit the log and run the same hashing would. The lab uses
// it only to show what the chain alone cannot notice.
export function recomputeChainFrom(lines, firstPosition) {
const rewritten = lines.slice(0, firstPosition - 1)
let previousHash = firstPosition > 1 ? JSON.parse(lines[firstPosition - 2]).entry_hash : GENESIS_HASH
for (const line of lines.slice(firstPosition - 1)) {
const entry = JSON.parse(line)
delete entry.entry_hash
entry.prev_hash = previousHash
const entryHash = hashText(JSON.stringify(entry))
entry.entry_hash = entryHash
rewritten.push(JSON.stringify(entry))
previousHash = entryHash
}
return rewritten
}
================================================================
FILE 4 OF 8: portal.mjs
================================================================
// portal.mjs: the invented community garden's member portal.
// One application in two versions. Both versions run the same four routes,
// the same sign-in, lockout, role and ownership checks, and call their logger
// at the same points. Only the logger differs.
// Illustration only: it shows the comparison, not how any framework behaves.
import { createServer } from 'node:http'
import { randomBytes, scryptSync, timingSafeEqual } from 'node:crypto'
import { ACCOUNTS, INVOICES, ROLES } from './garden-data.mjs'
import { createCarefulLogger, createCarelessLogger } from './security-log.mjs'
// The lab's own choices, not recommendations.
export const LOCK_AFTER_FAILURES = 5
export const LOCK_SECONDS = 900
const SESSION_COOKIE = 'garden_session'
const CORRELATION_HEADER = 'x-correlation-id'
const NOT_FOUND_BODY = { error: 'Not found' }
const SIGN_IN_REFUSED_BODY = { error: 'Sign-in failed' }
function hashPassword(password, salt) {
return scryptSync(password, salt, 32)
}
// Each password label is stored as a salted scrypt hash, worked out once
// when this file loads.
const STORED_PASSWORDS = new Map()
for (const account of ACCOUNTS) {
const salt = randomBytes(16)
STORED_PASSWORDS.set(account.id, { salt: salt, hash: hashPassword(account.password, salt) })
}
function isPasswordCorrect(account, typedPassword) {
const stored = STORED_PASSWORDS.get(account.id)
return timingSafeEqual(hashPassword(String(typedPassword), stored.salt), stored.hash)
}
function copyRecords(records) {
return records.map((record) => Object.assign({}, record, record.roles ? { roles: record.roles.slice() } : {}))
}
function readCookie(request, name) {
const header = request.headers.cookie || ''
for (const part of header.split(';')) {
const pair = part.trim().split('=')
if (pair[0] === name) return pair[1]
}
return ''
}
function readBodyText(request) {
return new Promise((resolve) => {
let text = ''
request.on('data', (chunk) => {
text = text + chunk
})
request.on('end', () => resolve(text))
})
}
function parseBody(bodyText) {
try {
const parsed = JSON.parse(bodyText || '{}')
return typeof parsed === 'object' && parsed !== null ? parsed : {}
} catch (error) {
return {}
}
}
// Splits the address into its parts and decodes each one, so an encoded line
// break in a record id arrives as a real line break.
function readPathParts(request) {
const path = request.url.split('?')[0]
try {
return path.split('/').filter((part) => part.length > 0).map((part) => decodeURIComponent(part))
} catch (error) {
return []
}
}
// Matches the request to one of the four routes. The route is recorded as a
// pattern, such as GET /api/invoices/:id, never as the address that was sent.
function matchRoute(method, parts) {
if (parts[0] !== 'api') return null
if (method === 'POST' && parts.length === 2 && parts[1] === 'sign-in') return { handler: 'signIn', route: 'POST /api/sign-in' }
if (method === 'GET' && parts.length === 3 && parts[1] === 'invoices') return { handler: 'readInvoice', route: 'GET /api/invoices/:id', id: parts[2] }
if (method === 'POST' && parts.length === 4 && parts[1] === 'invoices' && parts[3] === 'pay') return { handler: 'payInvoice', route: 'POST /api/invoices/:id/pay', id: parts[2] }
if (method === 'POST' && parts.length === 4 && parts[1] === 'accounts' && parts[3] === 'roles') return { handler: 'changeRole', route: 'POST /api/accounts/:id/roles', id: parts[2] }
return null
}
function reply(status, body, headers) {
return { status: status, body: body, headers: headers || {} }
}
function readFailureState(portal, account) {
if (!portal.failures.has(account.id)) portal.failures.set(account.id, { count: 0, lockedUntil: 0 })
const state = portal.failures.get(account.id)
const isLockOver = state.lockedUntil > 0 && state.lockedUntil <= portal.clock.now().getTime()
if (isLockOver) {
state.count = 0
state.lockedUntil = 0
}
return state
}
function refuseSignIn(portal, requestInfo, account, state) {
state.count = state.count + 1
portal.logger.signInFailed(requestInfo, account, 'wrong_password', state.count)
if (state.count >= LOCK_AFTER_FAILURES) {
state.lockedUntil = portal.clock.now().getTime() + LOCK_SECONDS * 1000
portal.logger.accountLocked(requestInfo, account, state.count)
}
return reply(401, SIGN_IN_REFUSED_BODY)
}
// Every refusal gets the same reply, whatever the reason. The reason goes to
// the log.
function signIn(portal, requestInfo) {
const account = portal.accounts.find((candidate) => candidate.username === requestInfo.body.username)
if (!account) {
portal.logger.signInFailed(requestInfo, null, 'unknown_account', 0)
return reply(401, SIGN_IN_REFUSED_BODY)
}
const state = readFailureState(portal, account)
if (state.lockedUntil > 0) {
portal.logger.signInFailed(requestInfo, account, 'account_locked', state.count)
return reply(401, SIGN_IN_REFUSED_BODY)
}
if (!isPasswordCorrect(account, requestInfo.body.password)) return refuseSignIn(portal, requestInfo, account, state)
const failuresBefore = state.count
state.count = 0
const newToken = randomBytes(24).toString('hex')
portal.sessions.set(newToken, account.id)
portal.logger.signInSucceeded(requestInfo, account, failuresBefore, newToken)
const cookie = SESSION_COOKIE + '=' + newToken + '; HttpOnly; SameSite=Strict; Path=/'
return reply(200, { signedIn: true }, { 'set-cookie': cookie })
}
function findSessionAccount(portal, requestInfo) {
const accountId = portal.sessions.get(requestInfo.sessionToken)
return portal.accounts.find((candidate) => candidate.id === accountId) || null
}
// The checks both invoice routes share. A record the caller may not reach
// gets the same 404 as a record that does not exist, and the log says which
// it was.
function findInvoiceFor(portal, requestInfo, invoiceId, isAdminAllowed, extraDetail) {
const caller = findSessionAccount(portal, requestInfo)
if (!caller) {
portal.logger.requestDenied(requestInfo, null, 'invoice', invoiceId, 'no_session', extraDetail)
return { refusal: reply(401, { error: 'Sign in first' }) }
}
const invoice = portal.invoices.find((candidate) => candidate.id === invoiceId)
if (!invoice) {
portal.logger.requestDenied(requestInfo, caller, 'invoice', invoiceId, 'not_found', extraDetail)
return { refusal: reply(404, NOT_FOUND_BODY) }
}
const isAllowed = invoice.ownerId === caller.id || (isAdminAllowed && caller.roles.includes('admin'))
if (!isAllowed) {
portal.logger.requestDenied(requestInfo, caller, 'invoice', invoiceId, 'not_owner', extraDetail)
return { refusal: reply(404, NOT_FOUND_BODY) }
}
return { invoice: invoice }
}
function readInvoice(portal, requestInfo, invoiceId) {
const found = findInvoiceFor(portal, requestInfo, invoiceId, true)
if (found.refusal) return found.refusal
return reply(200, { id: found.invoice.id, status: found.invoice.status })
}
// Stands in for a card payment. Nothing is charged: the invoice is marked
// paid. This handler hands the logger the parsed request body as extra
// detail, which a handler should not do. It is here on purpose, so the lab
// can show what each logger does with it.
function payInvoice(portal, requestInfo, invoiceId) {
const found = findInvoiceFor(portal, requestInfo, invoiceId, false, { submitted: requestInfo.body })
if (found.refusal) return found.refusal
found.invoice.status = 'paid'
return reply(200, { id: found.invoice.id, status: found.invoice.status })
}
function applyRoleChange(target, change, role) {
const hasRole = target.roles.includes(role)
if (change === 'grant' && !hasRole) {
target.roles.push(role)
return true
}
if (change === 'remove' && hasRole) {
target.roles = target.roles.filter((existing) => existing !== role)
return true
}
return false
}
// Only an admin may change roles. A change is logged when it alters the
// account's roles, and a refused attempt is logged as a denial. The actor is
// recorded with the roles held when the request arrived.
function changeRole(portal, requestInfo, accountId) {
const caller = findSessionAccount(portal, requestInfo)
if (!caller) {
portal.logger.requestDenied(requestInfo, null, 'account', accountId, 'no_session')
return reply(401, { error: 'Sign in first' })
}
if (!caller.roles.includes('admin')) {
portal.logger.requestDenied(requestInfo, caller, 'account', accountId, 'role_required')
return reply(403, { error: 'Forbidden' })
}
const target = portal.accounts.find((candidate) => candidate.id === accountId)
if (!target) {
portal.logger.requestDenied(requestInfo, caller, 'account', accountId, 'not_found')
return reply(404, NOT_FOUND_BODY)
}
const change = requestInfo.body.change
const role = requestInfo.body.role
if (!['grant', 'remove'].includes(change) || !ROLES.includes(role)) return reply(400, { error: 'Unknown change or role' })
const callerOnArrival = Object.assign({}, caller, { roles: caller.roles.slice() })
if (applyRoleChange(target, change, role)) portal.logger.roleChanged(requestInfo, callerOnArrival, target, change, role)
return reply(200, { id: target.id, roles: target.roles })
}
const HANDLERS = { signIn: signIn, readInvoice: readInvoice, payInvoice: payInvoice, changeRole: changeRole }
async function answer(portal, request, response) {
const matched = matchRoute(request.method, readPathParts(request))
if (!matched) {
response.writeHead(404, { 'content-type': 'application/json' })
response.end(JSON.stringify(NOT_FOUND_BODY))
return
}
const bodyText = await readBodyText(request)
const requestInfo = {
route: matched.route,
sourceIp: request.socket.remoteAddress,
bodyText: bodyText,
body: parseBody(bodyText),
sessionToken: readCookie(request, SESSION_COOKIE),
correlationId: portal.logger.newCorrelationId(),
}
const result = HANDLERS[matched.handler](portal, requestInfo, matched.id)
const headers = Object.assign({ 'content-type': 'application/json' }, result.headers)
if (requestInfo.correlationId) headers[CORRELATION_HEADER] = requestInfo.correlationId
response.writeHead(result.status, headers)
response.end(JSON.stringify(result.body))
}
// Starts one version on 127.0.0.1, on a port the system picks, with its own
// copy of the accounts and invoices and its own in-memory log.
export function startPortal(versionName, clock) {
const logger = versionName === 'careless' ? createCarelessLogger(clock) : createCarefulLogger(clock)
const portal = {
clock: clock,
logger: logger,
accounts: copyRecords(ACCOUNTS),
invoices: copyRecords(INVOICES),
sessions: new Map(),
failures: new Map(),
}
const server = createServer((request, response) => answer(portal, request, response))
return new Promise((resolve) => {
server.listen(0, '127.0.0.1', () => {
resolve({ server: server, logger: logger, url: 'http://127.0.0.1:' + server.address().port })
})
})
}
export function stopPortal(started) {
return new Promise((resolve) => started.server.close(resolve))
}
================================================================
FILE 5 OF 8: detector.mjs
================================================================
// detector.mjs: reads log lines and raises alerts from three written rules.
// It reads only lines that are version 1 events. Any other line is counted
// as skipped. Illustration only: the thresholds and windows are this lab's
// own choices, not recommendations, and this is not a monitoring product.
import { checkEvent } from './event-schema.mjs'
export const RULES = {
failedSignInBurst: {
name: 'failed_signin_burst',
threshold: 5,
windowSeconds: 600,
priority: 'high',
firstStep: 'Check whether a sign-in for this account succeeded after the failures.',
},
privilegeChange: {
name: 'privilege_change',
priority: 'high',
selfChangePriority: 'urgent',
firstStep: 'Ask the person named as actor, through another channel, whether they made this change.',
},
repeatedObjectDenials: {
name: 'repeated_object_denials',
threshold: 4,
windowSeconds: 300,
priority: 'high',
firstStep: 'List the records this account asked for and who owns them.',
},
}
// The denial reasons rule 3 counts: a request for a record, refused.
const RECORD_DENIAL_REASONS = ['not_owner', 'not_found']
function parseLine(line) {
try {
return JSON.parse(line)
} catch (error) {
return null
}
}
// Keeps the lines that are version 1 events, oldest first, and counts the rest.
export function readEvents(lines) {
const events = []
let linesSkipped = 0
for (const line of lines) {
const entry = parseLine(line)
if (entry === null || checkEvent(entry).length > 0) {
linesSkipped = linesSkipped + 1
continue
}
events.push(entry)
}
events.sort((first, second) => Date.parse(first.time) - Date.parse(second.time))
return { events: events, linesSkipped: linesSkipped }
}
function groupByKey(events, readKey) {
const groups = new Map()
for (const event of events) {
const key = readKey(event)
if (!groups.has(key)) groups.set(key, [])
groups.get(key).push(event)
}
return groups
}
// Reports a burst when a run of consecutive events for one key, as long as
// the threshold, falls inside the window. After a burst is reported, the same
// key is not reported again until a full window has passed since the event
// that completed it.
function findBursts(events, readKey, threshold, windowSeconds) {
const bursts = []
const windowMilliseconds = windowSeconds * 1000
for (const [key, group] of groupByKey(events, readKey)) {
let quietUntil = 0
for (let index = threshold - 1; index < group.length; index = index + 1) {
const run = group.slice(index - threshold + 1, index + 1)
const firstTime = Date.parse(run[0].time)
const lastTime = Date.parse(run[run.length - 1].time)
if (lastTime - firstTime > windowMilliseconds || lastTime < quietUntil) continue
bursts.push({ key: key, events: run })
quietUntil = lastTime + windowMilliseconds
}
}
return bursts
}
function listCorrelationIds(events) {
return events.map((event) => event.correlation_id)
}
// Rule 1: failed sign-ins for one account inside a window.
function findFailedSignInBursts(events) {
const rule = RULES.failedSignInBurst
const failures = events.filter((event) => event.event === 'authn_login_fail' && event.object.id !== null)
const bursts = findBursts(failures, (event) => event.object.id, rule.threshold, rule.windowSeconds)
return bursts.map((burst) => ({
rule: rule.name,
priority: rule.priority,
account: burst.key,
count: burst.events.length,
window_seconds: rule.windowSeconds,
first_time: burst.events[0].time,
last_time: burst.events[burst.events.length - 1].time,
correlation_ids: listCorrelationIds(burst.events),
first_step: rule.firstStep,
}))
}
// Rule 2: any privilege change, with a higher priority when the account
// that made the change is the account that was changed.
function findPrivilegeChanges(events) {
const rule = RULES.privilegeChange
const changes = events.filter((event) => event.event === 'authz_change')
return changes.map((event) => {
const isSelfChange = event.actor.id === event.object.id
return {
rule: rule.name,
priority: isSelfChange ? rule.selfChangePriority : rule.priority,
actor: event.actor.id,
account: event.object.id,
change: event.detail.change,
role: event.detail.role,
self_change: isSelfChange,
last_time: event.time,
correlation_ids: [event.correlation_id],
first_step: rule.firstStep,
}
})
}
// Rule 3: repeated requests by one account for records it may not reach or
// that do not exist (reasons not_owner and not_found), inside a window.
function findRepeatedObjectDenials(events) {
const rule = RULES.repeatedObjectDenials
const denials = events.filter((event) => {
return event.event === 'authz_fail' && event.actor.id !== null && RECORD_DENIAL_REASONS.includes(event.detail.reason)
})
const bursts = findBursts(denials, (event) => event.actor.id, rule.threshold, rule.windowSeconds)
return bursts.map((burst) => ({
rule: rule.name,
priority: rule.priority,
actor: burst.key,
count: burst.events.length,
objects: Array.from(new Set(burst.events.map((event) => event.object.id))),
window_seconds: rule.windowSeconds,
first_time: burst.events[0].time,
last_time: burst.events[burst.events.length - 1].time,
correlation_ids: listCorrelationIds(burst.events),
first_step: rule.firstStep,
}))
}
// Reads the log and returns the alerts, oldest first, with the counts of
// events read and lines skipped.
export function detect(lines) {
const read = readEvents(lines)
const alerts = [
...findFailedSignInBursts(read.events),
...findPrivilegeChanges(read.events),
...findRepeatedObjectDenials(read.events),
]
alerts.sort((first, second) => Date.parse(first.last_time) - Date.parse(second.last_time))
return { alerts: alerts, eventsRead: read.events.length, linesSkipped: read.linesSkipped }
}
================================================================
FILE 6 OF 8: scenarios.mjs
================================================================
// scenarios.mjs: the clock, the client and the request sequences that
// run-lab.mjs prints and detection.test.mjs asserts on.
import { FIXTURES } from './garden-data.mjs'
import { startPortal, stopPortal } from './portal.mjs'
import { recomputeChainFrom, splitLines } from './security-log.mjs'
// An invented start time. The lab's clock moves only when a scenario moves it.
export const START_TIME = '2026-03-07T22:58:00.000Z'
export function createStepClock(startText) {
let milliseconds = Date.parse(startText)
return {
now: () => new Date(milliseconds),
advance: (seconds) => {
milliseconds = milliseconds + seconds * 1000
},
}
}
async function send(lab, method, path, cookie, body) {
const headers = {}
if (cookie) headers.cookie = cookie
if (body) headers['content-type'] = 'application/json'
const response = await fetch(lab.portal.url + path, { method: method, headers: headers, body: body ? JSON.stringify(body) : undefined })
await response.text()
const setCookie = response.headers.get('set-cookie') || ''
return {
status: response.status,
cookie: setCookie.split(';')[0],
correlationId: response.headers.get('x-correlation-id') || '',
}
}
// Starts one version with a fresh clock and gives back the calls a scenario
// makes. Each call sends one request and returns its status, the session
// cookie if one was set, and the correlation id if one was returned.
export async function startLab(versionName) {
const clock = createStepClock(START_TIME)
const lab = { versionName: versionName, clock: clock, portal: await startPortal(versionName, clock) }
lab.signIn = (username, password) => send(lab, 'POST', '/api/sign-in', '', { username: username, password: password })
lab.readInvoice = (cookie, invoiceId) => send(lab, 'GET', '/api/invoices/' + encodeURIComponent(invoiceId), cookie)
lab.payInvoice = (cookie, invoiceId, cardNumber) => {
return send(lab, 'POST', '/api/invoices/' + encodeURIComponent(invoiceId) + '/pay', cookie, { card_number: cardNumber })
}
lab.changeRole = (cookie, accountId, change, role) => {
return send(lab, 'POST', '/api/accounts/' + accountId + '/roles', cookie, { change: change, role: role })
}
lab.lines = () => splitLines(lab.portal.logger.text())
lab.stop = () => stopPortal(lab.portal)
return lab
}
// Sends a number of wrong passwords for one username, a set number of
// seconds apart.
export async function sendWrongPasswords(lab, username, count, secondsApart) {
const replies = []
for (let attempt = 1; attempt <= count; attempt = attempt + 1) {
replies.push(await lab.signIn(username, FIXTURES.wrongPassword))
lab.clock.advance(secondsApart)
}
return replies
}
// Asks for each invoice id in turn from one session, a set number of
// seconds apart.
export async function requestInvoices(lab, cookie, invoiceIds, secondsApart) {
const replies = []
for (const invoiceId of invoiceIds) {
replies.push(await lab.readInvoice(cookie, invoiceId))
lab.clock.advance(secondsApart)
}
return replies
}
// The admin signs in and makes three role changes, a minute apart: a grant
// to another account, a removal from another account, and a grant to the
// admin's own account.
export async function makeRoleChanges(lab) {
const kit = await lab.signIn('kit@garden.example', 'LAB-PASSWORD-KIT')
lab.clock.advance(60)
const replies = []
replies.push(await lab.changeRole(kit.cookie, 'acct_m2', 'grant', 'coordinator'))
lab.clock.advance(60)
replies.push(await lab.changeRole(kit.cookie, 'acct_c1', 'remove', 'coordinator'))
lab.clock.advance(60)
replies.push(await lab.changeRole(kit.cookie, 'acct_a1', 'grant', 'coordinator'))
lab.clock.advance(60)
return replies
}
// The redaction fixtures, sent as five requests half a minute apart: a
// sign-in with a correct password, a sign-in with a wrong password, and then
// three requests that carry the first sign-in's session cookie. Those three
// are a payment with a card number, a read of the record id with line breaks
// and a read of the over-long record id. Returns the values the scan then
// searches the log for.
export async function sendRedactionFixtures(lab) {
const rowan = await lab.signIn('rowan@garden.example', 'LAB-PASSWORD-ROWAN')
lab.clock.advance(30)
await lab.signIn('sage@garden.example', FIXTURES.wrongPassword)
lab.clock.advance(30)
await lab.payInvoice(rowan.cookie, 'inv_2001', FIXTURES.cardNumber)
lab.clock.advance(30)
await lab.readInvoice(rowan.cookie, FIXTURES.forgedLineId)
lab.clock.advance(30)
await lab.readInvoice(rowan.cookie, FIXTURES.overLongId)
lab.clock.advance(30)
return {
correctPassword: 'LAB-PASSWORD-ROWAN',
wrongPassword: FIXTURES.wrongPassword,
sessionToken: rowan.cookie.split('=')[1],
cardNumber: FIXTURES.cardNumber,
overLongId: FIXTURES.overLongId,
}
}
// The whole story run-lab.mjs prints: a sign-in and an allowed read, five
// failed sign-ins, the admin's sign-in and three role changes, a refused
// role change, four refused invoice requests, the two odd record ids and one
// request with no session.
export async function playStory(lab) {
const rowan = await lab.signIn('rowan@garden.example', 'LAB-PASSWORD-ROWAN')
lab.clock.advance(30)
await lab.readInvoice(rowan.cookie, 'inv_1001')
lab.clock.advance(30)
await sendWrongPasswords(lab, 'sage@garden.example', 5, 60)
await makeRoleChanges(lab)
await lab.changeRole(rowan.cookie, 'acct_m1', 'grant', 'admin')
lab.clock.advance(30)
await requestInvoices(lab, rowan.cookie, ['inv_2001', 'inv_2002', 'inv_3001'], 30)
await lab.payInvoice(rowan.cookie, 'inv_2001', FIXTURES.cardNumber)
lab.clock.advance(30)
await lab.readInvoice(rowan.cookie, FIXTURES.forgedLineId)
lab.clock.advance(30)
await lab.readInvoice(rowan.cookie, FIXTURES.overLongId)
lab.clock.advance(30)
await lab.readInvoice('', 'inv_1001')
return {
correctPassword: 'LAB-PASSWORD-ROWAN',
wrongPassword: FIXTURES.wrongPassword,
sessionToken: rowan.cookie.split('=')[1],
cardNumber: FIXTURES.cardNumber,
overLongId: FIXTURES.overLongId,
}
}
function countLinesHolding(lines, value) {
return lines.filter((line) => line.includes(value)).length
}
function isForgedLine(line) {
return FIXTURES.forgedTexts.some((forgedText) => line.startsWith(forgedText))
}
// Searches the log for each fixture value, compares the number of lines with
// the number of entries the logger wrote, lists the lines that begin with
// one of the forged texts, and counts the lines that hold the forged sign-in
// text anywhere.
export function scanLog(lab, sent) {
const lines = lab.lines()
const writes = lab.portal.logger.writes()
return {
writes: writes,
lineCount: lines.length,
forgedLines: lines.filter(isForgedLine),
linesHoldingForgedSignIn: countLinesHolding(lines, FIXTURES.forgedTexts[0]),
found: {
correctPassword: countLinesHolding(lines, sent.correctPassword),
wrongPassword: countLinesHolding(lines, sent.wrongPassword),
sessionToken: countLinesHolding(lines, sent.sessionToken),
cardNumber: countLinesHolding(lines, sent.cardNumber),
overLongId: countLinesHolding(lines, sent.overLongId),
},
}
}
// The position, counting from 1, of the line that records the admin granting
// a role to the admin's own account, in either log's format. 0 if none.
export function findSelfGrantPosition(lines) {
const index = lines.findIndex((line) => {
const isCarelessLine = line.includes('ROLE CHANGE') && line.includes('on kit@garden.example')
const isCarefulLine = line.includes('"event":"authz_change"') && line.includes('"object":{"type":"account","id":"acct_a1"}')
return isCarelessLine || isCarefulLine
})
return index + 1
}
function parseJsonObject(line) {
try {
const parsed = JSON.parse(line)
return typeof parsed === 'object' && parsed !== null ? parsed : null
} catch (error) {
return null
}
}
// How many sign-ins by the admin a reader of the log would count. A line
// that is a JSON object is read by its fields. Any other line is searched
// for the careless sign-in text.
export function countApparentAdminSignIns(lines) {
let count = 0
for (const line of lines) {
const entry = parseJsonObject(line)
const isSignInEvent = entry !== null && entry.event === 'authn_login_success' && entry.actor.id === 'acct_a1'
const isSignInText = entry === null && line.includes('LOGIN OK for kit@garden.example')
if (isSignInEvent || isSignInText) count = count + 1
}
return count
}
function replaceText(line, from, to) {
return line.split(from).join(to)
}
// Four ways of interfering with a log, each applied to a copy of its lines.
// The change rewrites the line at the given position so that it names sage
// in place of kit, by account id and by username.
export function listTamperings(lines, position) {
const original = lines[position - 1]
const changed = lines.slice()
changed[position - 1] = replaceText(replaceText(original, 'acct_a1', 'acct_m2'), 'kit@garden.example', 'sage@garden.example')
const isChainedLog = original.includes('"entry_hash"')
return [
{ name: 'line ' + position + ' changed to name sage in place of kit', lines: changed },
{ name: 'line ' + position + ' deleted', lines: lines.filter((line, index) => index !== position - 1) },
{ name: 'the last line, line ' + lines.length + ', deleted', lines: lines.slice(0, lines.length - 1) },
{
name: 'line ' + position + ' changed, then every hash from that line onwards worked out again',
lines: isChainedLog ? recomputeChainFrom(changed, position) : changed,
},
]
}
================================================================
FILE 7 OF 8: run-lab.mjs
================================================================
// run-lab.mjs: plays the same story against both versions and prints each
// log, what the detector made of it, the redaction scan and the integrity
// check.
// Usage: node run-lab.mjs
import { RULES, detect } from './detector.mjs'
import { LOCK_AFTER_FAILURES, LOCK_SECONDS } from './portal.mjs'
import { verifyChain } from './security-log.mjs'
import {
START_TIME,
countApparentAdminSignIns,
findSelfGrantPosition,
listTamperings,
playStory,
scanLog,
startLab,
} from './scenarios.mjs'
function printHeading(text) {
console.log('')
console.log(text)
}
function printLog(lab) {
const lines = lab.lines()
printHeading('THE LOG: ' + lab.portal.logger.writes() + ' entries written, ' + lines.length + ' lines')
console.log('--- log starts ---')
for (const line of lines) console.log(line)
console.log('--- log ends ---')
}
function printOneEntryLaidOut(lab) {
const position = findSelfGrantPosition(lab.lines())
printHeading('ONE ENTRY LAID OUT: line ' + position + ', the admin granting a role to their own account')
console.log(JSON.stringify(JSON.parse(lab.lines()[position - 1]), null, 2))
}
function printDetector(lab) {
const result = detect(lab.lines())
printHeading('DETECTOR')
console.log(' lines read as version 1 events: ' + result.eventsRead)
console.log(' lines skipped: ' + result.linesSkipped)
console.log(' alerts raised: ' + result.alerts.length)
for (const alert of result.alerts) console.log(JSON.stringify(alert, null, 2))
}
function printScan(lab, sent) {
const scan = scanLog(lab, sent)
printHeading('REDACTION SCAN: the fixture values, searched for in the log')
console.log(' the correct password ................ in ' + scan.found.correctPassword + ' line(s)')
console.log(' the wrong password .................. in ' + scan.found.wrongPassword + ' line(s)')
console.log(' the session cookie value ............ in ' + scan.found.sessionToken + ' line(s)')
console.log(' the card number ..................... in ' + scan.found.cardNumber + ' line(s)')
console.log(' the 500-character record id, whole .. in ' + scan.found.overLongId + ' line(s)')
console.log(' entries written: ' + scan.writes + ', lines in the log: ' + scan.lineCount)
console.log(' lines that begin with forged text: ' + scan.forgedLines.length)
for (const line of scan.forgedLines) console.log(' ' + line)
console.log(' lines that hold the forged sign-in text anywhere: ' + scan.linesHoldingForgedSignIn)
console.log(' sign-ins by the admin in the story: 1, sign-ins by the admin the log appears to record: ' + countApparentAdminSignIns(lab.lines()))
}
function describeProblems(problems) {
if (problems.length === 0) return 'no problems'
const more = problems.length > 1 ? ' (and ' + (problems.length - 1) + ' more)' : ''
return problems[0] + more
}
function printIntegrity(lab) {
const lines = lab.lines()
const keptHead = lab.portal.logger.head()
const position = findSelfGrantPosition(lines)
printHeading('INTEGRITY CHECK')
if (keptHead) console.log(' kept head: ' + keptHead.count + ' entries, last entry hash begins ' + keptHead.hash.slice(0, 12))
else console.log(' kept head: none, because this logger keeps no hashes')
console.log(' the log as written: ' + describeProblems(verifyChain(lines, keptHead)))
for (const tampering of listTamperings(lines, position)) {
console.log(' ' + tampering.name)
console.log(' the chain alone: ' + describeProblems(verifyChain(tampering.lines)))
if (keptHead) console.log(' with the kept head: ' + describeProblems(verifyChain(tampering.lines, keptHead)))
}
}
async function runVersion(versionName) {
const lab = await startLab(versionName)
const sent = await playStory(lab)
await lab.stop()
console.log('')
console.log('================================================================')
console.log('VERSION: ' + versionName)
console.log('================================================================')
printLog(lab)
if (versionName === 'careful') printOneEntryLaidOut(lab)
printDetector(lab)
printScan(lab, sent)
printIntegrity(lab)
}
function printLabChoices() {
const burst = RULES.failedSignInBurst
const denials = RULES.repeatedObjectDenials
console.log('Choices this lab made, not recommendations:')
console.log(' the portal locks an account after ' + LOCK_AFTER_FAILURES + ' failed sign-ins in a row, for ' + LOCK_SECONDS + ' seconds')
console.log(' rule 1, ' + burst.name + ': ' + burst.threshold + ' failed sign-ins for one account inside ' + burst.windowSeconds + ' seconds')
console.log(' rule 2, ' + RULES.privilegeChange.name + ': every role change, urgent when an account changes its own')
console.log(' rule 3, ' + denials.name + ': ' + denials.threshold + ' refused record requests by one account inside ' + denials.windowSeconds + ' seconds')
}
console.log('SECURITY LOGGING LAB: one story, sent to both versions')
console.log('The story clock starts at ' + START_TIME + ' and moves only when the story moves it.')
printLabChoices()
await runVersion('careless')
await runVersion('careful')
================================================================
FILE 8 OF 8: detection.test.mjs
================================================================
// detection.test.mjs: the detection tests, the redaction fixtures and the
// integrity check, as tests. They fail on the careless version and pass on
// the careful one.
// Usage: node detection.test.mjs careless, or node detection.test.mjs careful
// With no argument it tests the careful version, which is what
// node --test runs when it finds this file by its .test.mjs name.
import { test } from 'node:test'
import assert from 'node:assert/strict'
import { detect, RULES } from './detector.mjs'
import { CUT_MARKER, MAX_TEXT_LENGTH, isUtcTime } from './event-schema.mjs'
import { verifyChain } from './security-log.mjs'
import {
countApparentAdminSignIns,
findSelfGrantPosition,
listTamperings,
makeRoleChanges,
requestInvoices,
scanLog,
sendRedactionFixtures,
sendWrongPasswords,
startLab,
} from './scenarios.mjs'
const versionName = process.argv[2] === 'careless' ? 'careless' : 'careful'
// Starts a fresh portal, plays one scenario, stops the portal and returns
// what the scenario returned with the log it left behind.
async function playOnFreshLab(scenario) {
const lab = await startLab(versionName)
try {
const outcome = await scenario(lab)
return { lab: lab, outcome: outcome, lines: lab.lines(), detection: detect(lab.lines()) }
} finally {
await lab.stop()
}
}
function listAlerts(detection, ruleName) {
return detection.alerts.filter((alert) => alert.rule === ruleName)
}
// A quiet result means something only if the detector could read the log.
function assertLogWasRead(played, expectedEvents) {
assert.equal(played.detection.linesSkipped, 0, 'lines the detector could not read as events: ' + played.detection.linesSkipped)
assert.equal(played.detection.eventsRead, expectedEvents, 'events read')
}
test(versionName + ': rule 1 fires on five failed sign-ins for one account inside ten minutes', async () => {
const played = await playOnFreshLab((lab) => sendWrongPasswords(lab, 'sage@garden.example', 5, 60))
const alerts = listAlerts(played.detection, RULES.failedSignInBurst.name)
assert.equal(alerts.length, 1, 'failed_signin_burst alerts raised')
assert.equal(alerts[0].account, 'acct_m2')
assert.equal(alerts[0].count, 5)
assert.equal(alerts[0].priority, 'high')
assert.equal(alerts[0].first_time, '2026-03-07T22:58:00.000Z')
assert.equal(alerts[0].last_time, '2026-03-07T23:02:00.000Z')
assert.equal(new Set(alerts[0].correlation_ids).size, 5, 'one correlation id per failed request')
})
test(versionName + ': rule 1 stays quiet on four failures followed by the right password', async () => {
const played = await playOnFreshLab(async (lab) => {
await sendWrongPasswords(lab, 'sage@garden.example', 4, 60)
return lab.signIn('sage@garden.example', 'LAB-PASSWORD-SAGE')
})
assert.equal(played.outcome.status, 200, 'the sign-in with the right password')
assertLogWasRead(played, 5)
assert.equal(played.detection.alerts.length, 0, 'alerts raised')
})
test(versionName + ': rule 2 fires on every role change, and is urgent when an account changes its own', async () => {
const played = await playOnFreshLab(makeRoleChanges)
const alerts = listAlerts(played.detection, RULES.privilegeChange.name)
assert.equal(alerts.length, 3, 'privilege_change alerts raised')
const summaries = alerts.map((alert) => [alert.priority, alert.actor, alert.change, alert.role, alert.account].join(' '))
assert.deepEqual(summaries, [
'high acct_a1 grant coordinator acct_m2',
'high acct_a1 remove coordinator acct_c1',
'urgent acct_a1 grant coordinator acct_a1',
])
assert.equal(alerts[2].self_change, true)
})
test(versionName + ': rule 2 stays quiet when a member is refused a role change', async () => {
const played = await playOnFreshLab(async (lab) => {
const rowan = await lab.signIn('rowan@garden.example', 'LAB-PASSWORD-ROWAN')
lab.clock.advance(30)
return lab.changeRole(rowan.cookie, 'acct_m1', 'grant', 'admin')
})
assert.equal(played.outcome.status, 403, 'the refused role change')
assertLogWasRead(played, 2)
assert.equal(played.detection.alerts.length, 0, 'alerts raised')
})
test(versionName + ': rule 3 fires on four refused invoice requests by one account inside five minutes', async () => {
const played = await playOnFreshLab(async (lab) => {
const rowan = await lab.signIn('rowan@garden.example', 'LAB-PASSWORD-ROWAN')
lab.clock.advance(30)
return requestInvoices(lab, rowan.cookie, ['inv_2001', 'inv_2002', 'inv_3001', 'inv_3002'], 30)
})
assert.deepEqual(played.outcome.map((reply) => reply.status), [404, 404, 404, 404], 'what the caller saw')
const alerts = listAlerts(played.detection, RULES.repeatedObjectDenials.name)
assert.equal(alerts.length, 1, 'repeated_object_denials alerts raised')
assert.equal(alerts[0].actor, 'acct_m1')
assert.equal(alerts[0].count, 4)
assert.deepEqual(alerts[0].objects, ['inv_2001', 'inv_2002', 'inv_3001', 'inv_3002'])
assert.equal(alerts[0].priority, 'high')
})
test(versionName + ': rule 3 stays quiet on three refusals inside the window and a fourth after it', async () => {
const played = await playOnFreshLab(async (lab) => {
const rowan = await lab.signIn('rowan@garden.example', 'LAB-PASSWORD-ROWAN')
lab.clock.advance(30)
await requestInvoices(lab, rowan.cookie, ['inv_2001', 'inv_2002', 'inv_3001'], 60)
lab.clock.advance(210)
return lab.readInvoice(rowan.cookie, 'inv_3002')
})
assert.equal(played.outcome.status, 404, 'the fourth request')
assertLogWasRead(played, 5)
assert.equal(played.detection.alerts.length, 0, 'alerts raised')
})
// The fixtures are sent once, and each of the five tests below looks at one
// kind of value.
const fixtures = await playOnFreshLab(sendRedactionFixtures)
const scan = scanLog(fixtures.lab, fixtures.outcome)
test(versionName + ': no submitted password reaches the log', () => {
assert.equal(scan.found.correctPassword, 0, 'lines holding the correct password')
assert.equal(scan.found.wrongPassword, 0, 'lines holding the wrong password')
})
test(versionName + ': the session cookie value does not reach the log', () => {
assert.equal(scan.found.sessionToken, 0, 'lines holding the session cookie value')
})
test(versionName + ': the card number does not reach the log', () => {
assert.equal(scan.found.cardNumber, 0, 'lines holding the card number')
})
test(versionName + ': a record id with line breaks does not forge a log line', () => {
assert.equal(scan.lineCount, scan.writes, 'lines in the log, against entries written')
assert.equal(scan.forgedLines.length, 0, 'lines that begin with forged text')
assert.equal(countApparentAdminSignIns(fixtures.lines), 0, 'sign-ins by the admin the log appears to record')
})
test(versionName + ': an over-long record id is cut before it is written', () => {
assert.equal(scan.found.overLongId, 0, 'lines holding all 500 characters')
const cutValue = 'L'.repeat(MAX_TEXT_LENGTH) + CUT_MARKER + '"'
const linesHoldingCutValue = fixtures.lines.filter((line) => line.includes(cutValue))
assert.equal(linesHoldingCutValue.length, 1, 'lines holding the first ' + MAX_TEXT_LENGTH + ' characters and the cut marker')
})
test(versionName + ': every entry carries a UTC time and the correlation id its response returned', async () => {
const played = await playOnFreshLab((lab) => sendWrongPasswords(lab, 'sage@garden.example', 5, 60))
const returnedIds = played.outcome.map((reply) => reply.correlationId)
assert.equal(returnedIds.filter((id) => id.length > 0).length, 5, 'responses that returned a correlation id')
assertLogWasRead(played, 6)
const entries = played.lines.map((line) => JSON.parse(line))
assert.ok(entries.every((entry) => isUtcTime(entry.time)), 'every time is a UTC timestamp')
assert.deepEqual(entries.map((entry) => entry.correlation_id), returnedIds.concat([returnedIds[4]]), 'the lock shares the id of the fifth failure')
})
test(versionName + ': a changed, deleted or rewritten entry is noticed', async () => {
const played = await playOnFreshLab(async (lab) => {
await makeRoleChanges(lab)
return lab.signIn('rowan@garden.example', 'LAB-PASSWORD-ROWAN')
})
const keptHead = played.lab.portal.logger.head()
assert.ok(keptHead, 'the logger keeps a head: a count and a last hash')
assert.deepEqual(verifyChain(played.lines, keptHead), [], 'the log as written')
const position = findSelfGrantPosition(played.lines)
const tamperings = listTamperings(played.lines, position)
const noticedByChain = tamperings.map((tampering) => verifyChain(tampering.lines).length > 0)
const noticedWithHead = tamperings.map((tampering) => verifyChain(tampering.lines, keptHead).length > 0)
assert.deepEqual(noticedByChain, [true, true, false, false], 'noticed by the chain alone')
assert.deepEqual(noticedWithHead, [true, true, true, true], 'noticed with the kept head')
})
================================================================
STEP 1: play the story against both versions
================================================================
Command: TZ=Australia/Melbourne node run-lab.mjs
SECURITY LOGGING LAB: one story, sent to both versions
The story clock starts at 2026-03-07T22:58:00.000Z and moves only when the story moves it.
Choices this lab made, not recommendations:
the portal locks an account after 5 failed sign-ins in a row, for 900 seconds
rule 1, failed_signin_burst: 5 failed sign-ins for one account inside 600 seconds
rule 2, privilege_change: every role change, urgent when an account changes its own
rule 3, repeated_object_denials: 4 refused record requests by one account inside 300 seconds
================================================================
VERSION: careless
================================================================
THE LOG: 19 entries written, 21 lines
--- log starts ---
08/03/2026 09:58:00 LOGIN OK for rowan@garden.example session=4e573afca2250712503f7b64360965e12154577c9194dbaf body={"username":"rowan@garden.example","password":"LAB-PASSWORD-ROWAN"}
08/03/2026 09:59:00 LOGIN FAILED for sage@garden.example (wrong_password) body={"username":"sage@garden.example","password":"LAB-WRONG-PASSWORD-0001"}
08/03/2026 10:00:00 LOGIN FAILED for sage@garden.example (wrong_password) body={"username":"sage@garden.example","password":"LAB-WRONG-PASSWORD-0001"}
08/03/2026 10:01:00 LOGIN FAILED for sage@garden.example (wrong_password) body={"username":"sage@garden.example","password":"LAB-WRONG-PASSWORD-0001"}
08/03/2026 10:02:00 LOGIN FAILED for sage@garden.example (wrong_password) body={"username":"sage@garden.example","password":"LAB-WRONG-PASSWORD-0001"}
08/03/2026 10:03:00 LOGIN FAILED for sage@garden.example (wrong_password) body={"username":"sage@garden.example","password":"LAB-WRONG-PASSWORD-0001"}
08/03/2026 10:03:00 ACCOUNT LOCKED sage@garden.example body={"username":"sage@garden.example","password":"LAB-WRONG-PASSWORD-0001"}
08/03/2026 10:04:00 LOGIN OK for kit@garden.example session=ff1d98c4db0981cea3c2e777b9d9a2d6e067559443d1cf2f body={"username":"kit@garden.example","password":"LAB-PASSWORD-KIT"}
08/03/2026 10:05:00 ROLE CHANGE by session=ff1d98c4db0981cea3c2e777b9d9a2d6e067559443d1cf2f grant coordinator on sage@garden.example body={"change":"grant","role":"coordinator"}
08/03/2026 10:06:00 ROLE CHANGE by session=ff1d98c4db0981cea3c2e777b9d9a2d6e067559443d1cf2f remove coordinator on quinn@garden.example body={"change":"remove","role":"coordinator"}
08/03/2026 10:07:00 ROLE CHANGE by session=ff1d98c4db0981cea3c2e777b9d9a2d6e067559443d1cf2f grant coordinator on kit@garden.example body={"change":"grant","role":"coordinator"}
08/03/2026 10:08:00 DENIED session=4e573afca2250712503f7b64360965e12154577c9194dbaf wanted account acct_m1 (role_required) body={"change":"grant","role":"admin"}
08/03/2026 10:08:30 DENIED session=4e573afca2250712503f7b64360965e12154577c9194dbaf wanted invoice inv_2001 (not_owner)
08/03/2026 10:09:00 DENIED session=4e573afca2250712503f7b64360965e12154577c9194dbaf wanted invoice inv_2002 (not_owner)
08/03/2026 10:09:30 DENIED session=4e573afca2250712503f7b64360965e12154577c9194dbaf wanted invoice inv_3001 (not_owner)
08/03/2026 10:10:00 DENIED session=4e573afca2250712503f7b64360965e12154577c9194dbaf wanted invoice inv_2001 (not_owner) body={"card_number":"0000 0000 0000 0001"}
08/03/2026 10:10:30 DENIED session=4e573afca2250712503f7b64360965e12154577c9194dbaf wanted invoice inv_9999
08/03/2026 09:15:00 LOGIN OK for kit@garden.example session=FORGED-BY-FIXTURE
{"schema_version":"1","event":"authn_login_success","actor":{"id":"acct_a1"},"note":"FORGED-BY-FIXTURE"} (not_found)
08/03/2026 10:11:00 DENIED session=4e573afca2250712503f7b64360965e12154577c9194dbaf wanted invoice LLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLL (not_found)
08/03/2026 10:11:30 DENIED session= wanted invoice inv_1001 (no_session)
--- log ends ---
DETECTOR
lines read as version 1 events: 0
lines skipped: 21
alerts raised: 0
REDACTION SCAN: the fixture values, searched for in the log
the correct password ................ in 1 line(s)
the wrong password .................. in 6 line(s)
the session cookie value ............ in 8 line(s)
the card number ..................... in 1 line(s)
the 500-character record id, whole .. in 1 line(s)
entries written: 19, lines in the log: 21
lines that begin with forged text: 2
08/03/2026 09:15:00 LOGIN OK for kit@garden.example session=FORGED-BY-FIXTURE
{"schema_version":"1","event":"authn_login_success","actor":{"id":"acct_a1"},"note":"FORGED-BY-FIXTURE"} (not_found)
lines that hold the forged sign-in text anywhere: 1
sign-ins by the admin in the story: 1, sign-ins by the admin the log appears to record: 2
INTEGRITY CHECK
kept head: none, because this logger keeps no hashes
the log as written: line 1: no entry hash, so nothing links it to the line before (and 20 more)
line 11 changed to name sage in place of kit
the chain alone: line 1: no entry hash, so nothing links it to the line before (and 20 more)
line 11 deleted
the chain alone: line 1: no entry hash, so nothing links it to the line before (and 19 more)
the last line, line 21, deleted
the chain alone: line 1: no entry hash, so nothing links it to the line before (and 19 more)
line 11 changed, then every hash from that line onwards worked out again
the chain alone: line 1: no entry hash, so nothing links it to the line before (and 20 more)
================================================================
VERSION: careful
================================================================
THE LOG: 19 entries written, 19 lines
--- log starts ---
{"schema_version":"1","time":"2026-03-07T22:58:00.000Z","app":"garden-portal","route":"POST /api/sign-in","event":"authn_login_success","outcome":"success","actor":{"id":"acct_m1","roles":["member"]},"object":{"type":"account","id":"acct_m1"},"source":{"ip":"127.0.0.1"},"correlation_id":"01adf402-084e-496f-8e6c-f7bcd8d16785","detail":{"failures_before":0},"seq":1,"prev_hash":"0000000000000000000000000000000000000000000000000000000000000000","entry_hash":"387c835c2af0cc9c87057438a5e7cf7089cba597dd9b684b36f8df780be03ec0"}
{"schema_version":"1","time":"2026-03-07T22:59:00.000Z","app":"garden-portal","route":"POST /api/sign-in","event":"authn_login_fail","outcome":"failure","actor":{"id":null,"roles":[]},"object":{"type":"account","id":"acct_m2"},"source":{"ip":"127.0.0.1"},"correlation_id":"e77652f5-7f28-4104-8a34-0f6c4919f9bf","detail":{"reason":"wrong_password","consecutive_failures":1},"seq":2,"prev_hash":"387c835c2af0cc9c87057438a5e7cf7089cba597dd9b684b36f8df780be03ec0","entry_hash":"e81e68f00618eb28d0bdb6be9b6109a8a7a211730958372f53ef7e02db2c70b8"}
{"schema_version":"1","time":"2026-03-07T23:00:00.000Z","app":"garden-portal","route":"POST /api/sign-in","event":"authn_login_fail","outcome":"failure","actor":{"id":null,"roles":[]},"object":{"type":"account","id":"acct_m2"},"source":{"ip":"127.0.0.1"},"correlation_id":"605dc4ae-0694-41f4-9909-d2f37a792e6d","detail":{"reason":"wrong_password","consecutive_failures":2},"seq":3,"prev_hash":"e81e68f00618eb28d0bdb6be9b6109a8a7a211730958372f53ef7e02db2c70b8","entry_hash":"d04cd94e9540729ffcefa9b3d00c5e00191a217cf8f8191edebdf39afd7044aa"}
{"schema_version":"1","time":"2026-03-07T23:01:00.000Z","app":"garden-portal","route":"POST /api/sign-in","event":"authn_login_fail","outcome":"failure","actor":{"id":null,"roles":[]},"object":{"type":"account","id":"acct_m2"},"source":{"ip":"127.0.0.1"},"correlation_id":"38657e34-a37b-4c2b-a9cb-3edb4b72375f","detail":{"reason":"wrong_password","consecutive_failures":3},"seq":4,"prev_hash":"d04cd94e9540729ffcefa9b3d00c5e00191a217cf8f8191edebdf39afd7044aa","entry_hash":"27976e0ab7fb54302c87041785527039ce1b1f452d1008d31f02fbf5f8fbddbd"}
{"schema_version":"1","time":"2026-03-07T23:02:00.000Z","app":"garden-portal","route":"POST /api/sign-in","event":"authn_login_fail","outcome":"failure","actor":{"id":null,"roles":[]},"object":{"type":"account","id":"acct_m2"},"source":{"ip":"127.0.0.1"},"correlation_id":"7a922969-813c-4494-a068-6ce3d7a3fec5","detail":{"reason":"wrong_password","consecutive_failures":4},"seq":5,"prev_hash":"27976e0ab7fb54302c87041785527039ce1b1f452d1008d31f02fbf5f8fbddbd","entry_hash":"1a599b618f74e99fcc0e800b563772a4004d5440e1d9158acf241e0d38d8ed68"}
{"schema_version":"1","time":"2026-03-07T23:03:00.000Z","app":"garden-portal","route":"POST /api/sign-in","event":"authn_login_fail","outcome":"failure","actor":{"id":null,"roles":[]},"object":{"type":"account","id":"acct_m2"},"source":{"ip":"127.0.0.1"},"correlation_id":"e5f40ba0-5dca-41a9-a7d0-e60720e5cd22","detail":{"reason":"wrong_password","consecutive_failures":5},"seq":6,"prev_hash":"1a599b618f74e99fcc0e800b563772a4004d5440e1d9158acf241e0d38d8ed68","entry_hash":"de95f0a7773fc7850e366048bda038b40160658ff5c892ab63eccb66b909f1e9"}
{"schema_version":"1","time":"2026-03-07T23:03:00.000Z","app":"garden-portal","route":"POST /api/sign-in","event":"authn_login_lock","outcome":"success","actor":{"id":null,"roles":[]},"object":{"type":"account","id":"acct_m2"},"source":{"ip":"127.0.0.1"},"correlation_id":"e5f40ba0-5dca-41a9-a7d0-e60720e5cd22","detail":{"reason":"max_failures","consecutive_failures":5},"seq":7,"prev_hash":"de95f0a7773fc7850e366048bda038b40160658ff5c892ab63eccb66b909f1e9","entry_hash":"cb078f4f42241b721a5649e73240cd0558d89c7ae26b391409744680edd9265b"}
{"schema_version":"1","time":"2026-03-07T23:04:00.000Z","app":"garden-portal","route":"POST /api/sign-in","event":"authn_login_success","outcome":"success","actor":{"id":"acct_a1","roles":["member","admin"]},"object":{"type":"account","id":"acct_a1"},"source":{"ip":"127.0.0.1"},"correlation_id":"767efe98-2195-4a06-b5ba-cee31d86b1fe","detail":{"failures_before":0},"seq":8,"prev_hash":"cb078f4f42241b721a5649e73240cd0558d89c7ae26b391409744680edd9265b","entry_hash":"0ae72fb195e4044bdd277231b6526d96c69cd935daf08fb47252ef1bf9d03aba"}
{"schema_version":"1","time":"2026-03-07T23:05:00.000Z","app":"garden-portal","route":"POST /api/accounts/:id/roles","event":"authz_change","outcome":"success","actor":{"id":"acct_a1","roles":["member","admin"]},"object":{"type":"account","id":"acct_m2"},"source":{"ip":"127.0.0.1"},"correlation_id":"007e81be-1115-4acd-8218-1b6e5c2cb28b","detail":{"change":"grant","role":"coordinator"},"seq":9,"prev_hash":"0ae72fb195e4044bdd277231b6526d96c69cd935daf08fb47252ef1bf9d03aba","entry_hash":"e203bd37fd9a72d7ac8f58949c7a4e038317686bec9bd47e7ade46105be6fd02"}
{"schema_version":"1","time":"2026-03-07T23:06:00.000Z","app":"garden-portal","route":"POST /api/accounts/:id/roles","event":"authz_change","outcome":"success","actor":{"id":"acct_a1","roles":["member","admin"]},"object":{"type":"account","id":"acct_c1"},"source":{"ip":"127.0.0.1"},"correlation_id":"b7a80083-3543-4832-999e-5d8844a9b272","detail":{"change":"remove","role":"coordinator"},"seq":10,"prev_hash":"e203bd37fd9a72d7ac8f58949c7a4e038317686bec9bd47e7ade46105be6fd02","entry_hash":"07effdbe9024453a602465be2e5fe71dfd327171fb9ec80d10fef06640f160b3"}
{"schema_version":"1","time":"2026-03-07T23:07:00.000Z","app":"garden-portal","route":"POST /api/accounts/:id/roles","event":"authz_change","outcome":"success","actor":{"id":"acct_a1","roles":["member","admin"]},"object":{"type":"account","id":"acct_a1"},"source":{"ip":"127.0.0.1"},"correlation_id":"61c0c03c-2162-41bb-b275-b70068f01d25","detail":{"change":"grant","role":"coordinator"},"seq":11,"prev_hash":"07effdbe9024453a602465be2e5fe71dfd327171fb9ec80d10fef06640f160b3","entry_hash":"1fbf2ee31fb4c079c3953017f91f3c11c400ea9f9279c88b1e1214e1172c845f"}
{"schema_version":"1","time":"2026-03-07T23:08:00.000Z","app":"garden-portal","route":"POST /api/accounts/:id/roles","event":"authz_fail","outcome":"denied","actor":{"id":"acct_m1","roles":["member"]},"object":{"type":"account","id":"acct_m1"},"source":{"ip":"127.0.0.1"},"correlation_id":"ec23b611-7b21-4d37-b3bf-2b72f717ed1d","detail":{"reason":"role_required"},"seq":12,"prev_hash":"1fbf2ee31fb4c079c3953017f91f3c11c400ea9f9279c88b1e1214e1172c845f","entry_hash":"04f2e0fbf9052f84d6dbbe485eb98e29b722372065e98830441655eb0ef73de9"}
{"schema_version":"1","time":"2026-03-07T23:08:30.000Z","app":"garden-portal","route":"GET /api/invoices/:id","event":"authz_fail","outcome":"denied","actor":{"id":"acct_m1","roles":["member"]},"object":{"type":"invoice","id":"inv_2001"},"source":{"ip":"127.0.0.1"},"correlation_id":"966a7b95-1db5-4ebd-8c35-0a629b95850b","detail":{"reason":"not_owner"},"seq":13,"prev_hash":"04f2e0fbf9052f84d6dbbe485eb98e29b722372065e98830441655eb0ef73de9","entry_hash":"477f81ea0c18b8febfe53363b69a68006fe0f53ffadc09cbe61f2e51cca228c0"}
{"schema_version":"1","time":"2026-03-07T23:09:00.000Z","app":"garden-portal","route":"GET /api/invoices/:id","event":"authz_fail","outcome":"denied","actor":{"id":"acct_m1","roles":["member"]},"object":{"type":"invoice","id":"inv_2002"},"source":{"ip":"127.0.0.1"},"correlation_id":"6293f653-6452-425e-bc64-2f8ed5c85e9d","detail":{"reason":"not_owner"},"seq":14,"prev_hash":"477f81ea0c18b8febfe53363b69a68006fe0f53ffadc09cbe61f2e51cca228c0","entry_hash":"63f4d26eaa797d5d95863c7a17cfe451ffd8bdc911278922416d6285a36ab7ae"}
{"schema_version":"1","time":"2026-03-07T23:09:30.000Z","app":"garden-portal","route":"GET /api/invoices/:id","event":"authz_fail","outcome":"denied","actor":{"id":"acct_m1","roles":["member"]},"object":{"type":"invoice","id":"inv_3001"},"source":{"ip":"127.0.0.1"},"correlation_id":"57e3eadd-4bd4-4ea5-9c88-5525c2f92b13","detail":{"reason":"not_owner"},"seq":15,"prev_hash":"63f4d26eaa797d5d95863c7a17cfe451ffd8bdc911278922416d6285a36ab7ae","entry_hash":"4aa79547f85211e504b7ea325705649c03207d51a641b7ca6a7c7102fd23f762"}
{"schema_version":"1","time":"2026-03-07T23:10:00.000Z","app":"garden-portal","route":"POST /api/invoices/:id/pay","event":"authz_fail","outcome":"denied","actor":{"id":"acct_m1","roles":["member"]},"object":{"type":"invoice","id":"inv_2001"},"source":{"ip":"127.0.0.1"},"correlation_id":"8d290e2e-cad0-4df2-83bf-3bd87e6b8aa8","detail":{"reason":"not_owner","dropped_fields":1},"seq":16,"prev_hash":"4aa79547f85211e504b7ea325705649c03207d51a641b7ca6a7c7102fd23f762","entry_hash":"213e69a1c38855aa4abb4c58cffa75c49859f10e6b5f47cc89dc5715bb8460fa"}
{"schema_version":"1","time":"2026-03-07T23:10:30.000Z","app":"garden-portal","route":"GET /api/invoices/:id","event":"authz_fail","outcome":"denied","actor":{"id":"acct_m1","roles":["member"]},"object":{"type":"invoice","id":"inv_9999\n08/03/2026 09:15:00 LOGIN OK for kit@garden.example session=FORGED-BY-FIXTURE\n{\"schema_version\":\"1\",\"event\":\"authn_login_success\",\"actor\":{\"id\":\"acct_a1\"},\"note\":\"FORGED-BY-FIXTURE\"}"},"source":{"ip":"127.0.0.1"},"correlation_id":"75f2c5e6-936f-4d86-b9f5-29bfa37682ac","detail":{"reason":"not_found"},"seq":17,"prev_hash":"213e69a1c38855aa4abb4c58cffa75c49859f10e6b5f47cc89dc5715bb8460fa","entry_hash":"9bba5212a73015b7f44ff03fafc4e5da1442f45d7f5dfb4fc407dbe97d0df556"}
{"schema_version":"1","time":"2026-03-07T23:11:00.000Z","app":"garden-portal","route":"GET /api/invoices/:id","event":"authz_fail","outcome":"denied","actor":{"id":"acct_m1","roles":["member"]},"object":{"type":"invoice","id":"LLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLL(cut)"},"source":{"ip":"127.0.0.1"},"correlation_id":"70f91840-93f5-4e2b-847d-07e3db4855fa","detail":{"reason":"not_found"},"seq":18,"prev_hash":"9bba5212a73015b7f44ff03fafc4e5da1442f45d7f5dfb4fc407dbe97d0df556","entry_hash":"f5855119d77f3b2176bd9228c8d0d89a535570fd151852b7e4fab7bf890ba448"}
{"schema_version":"1","time":"2026-03-07T23:11:30.000Z","app":"garden-portal","route":"GET /api/invoices/:id","event":"authz_fail","outcome":"denied","actor":{"id":null,"roles":[]},"object":{"type":"invoice","id":"inv_1001"},"source":{"ip":"127.0.0.1"},"correlation_id":"91a85f25-7ccf-49d9-a90b-0fed8c7509dc","detail":{"reason":"no_session"},"seq":19,"prev_hash":"f5855119d77f3b2176bd9228c8d0d89a535570fd151852b7e4fab7bf890ba448","entry_hash":"f2650514548a0409afaa86bb2af654059e4d3a3873d684f763d9db3e56d6727d"}
--- log ends ---
ONE ENTRY LAID OUT: line 11, the admin granting a role to their own account
{
"schema_version": "1",
"time": "2026-03-07T23:07:00.000Z",
"app": "garden-portal",
"route": "POST /api/accounts/:id/roles",
"event": "authz_change",
"outcome": "success",
"actor": {
"id": "acct_a1",
"roles": [
"member",
"admin"
]
},
"object": {
"type": "account",
"id": "acct_a1"
},
"source": {
"ip": "127.0.0.1"
},
"correlation_id": "61c0c03c-2162-41bb-b275-b70068f01d25",
"detail": {
"change": "grant",
"role": "coordinator"
},
"seq": 11,
"prev_hash": "07effdbe9024453a602465be2e5fe71dfd327171fb9ec80d10fef06640f160b3",
"entry_hash": "1fbf2ee31fb4c079c3953017f91f3c11c400ea9f9279c88b1e1214e1172c845f"
}
DETECTOR
lines read as version 1 events: 19
lines skipped: 0
alerts raised: 5
{
"rule": "failed_signin_burst",
"priority": "high",
"account": "acct_m2",
"count": 5,
"window_seconds": 600,
"first_time": "2026-03-07T22:59:00.000Z",
"last_time": "2026-03-07T23:03:00.000Z",
"correlation_ids": [
"e77652f5-7f28-4104-8a34-0f6c4919f9bf",
"605dc4ae-0694-41f4-9909-d2f37a792e6d",
"38657e34-a37b-4c2b-a9cb-3edb4b72375f",
"7a922969-813c-4494-a068-6ce3d7a3fec5",
"e5f40ba0-5dca-41a9-a7d0-e60720e5cd22"
],
"first_step": "Check whether a sign-in for this account succeeded after the failures."
}
{
"rule": "privilege_change",
"priority": "high",
"actor": "acct_a1",
"account": "acct_m2",
"change": "grant",
"role": "coordinator",
"self_change": false,
"last_time": "2026-03-07T23:05:00.000Z",
"correlation_ids": [
"007e81be-1115-4acd-8218-1b6e5c2cb28b"
],
"first_step": "Ask the person named as actor, through another channel, whether they made this change."
}
{
"rule": "privilege_change",
"priority": "high",
"actor": "acct_a1",
"account": "acct_c1",
"change": "remove",
"role": "coordinator",
"self_change": false,
"last_time": "2026-03-07T23:06:00.000Z",
"correlation_ids": [
"b7a80083-3543-4832-999e-5d8844a9b272"
],
"first_step": "Ask the person named as actor, through another channel, whether they made this change."
}
{
"rule": "privilege_change",
"priority": "urgent",
"actor": "acct_a1",
"account": "acct_a1",
"change": "grant",
"role": "coordinator",
"self_change": true,
"last_time": "2026-03-07T23:07:00.000Z",
"correlation_ids": [
"61c0c03c-2162-41bb-b275-b70068f01d25"
],
"first_step": "Ask the person named as actor, through another channel, whether they made this change."
}
{
"rule": "repeated_object_denials",
"priority": "high",
"actor": "acct_m1",
"count": 4,
"objects": [
"inv_2001",
"inv_2002",
"inv_3001"
],
"window_seconds": 300,
"first_time": "2026-03-07T23:08:30.000Z",
"last_time": "2026-03-07T23:10:00.000Z",
"correlation_ids": [
"966a7b95-1db5-4ebd-8c35-0a629b95850b",
"6293f653-6452-425e-bc64-2f8ed5c85e9d",
"57e3eadd-4bd4-4ea5-9c88-5525c2f92b13",
"8d290e2e-cad0-4df2-83bf-3bd87e6b8aa8"
],
"first_step": "List the records this account asked for and who owns them."
}
REDACTION SCAN: the fixture values, searched for in the log
the correct password ................ in 0 line(s)
the wrong password .................. in 0 line(s)
the session cookie value ............ in 0 line(s)
the card number ..................... in 0 line(s)
the 500-character record id, whole .. in 0 line(s)
entries written: 19, lines in the log: 19
lines that begin with forged text: 0
lines that hold the forged sign-in text anywhere: 1
sign-ins by the admin in the story: 1, sign-ins by the admin the log appears to record: 1
INTEGRITY CHECK
kept head: 19 entries, last entry hash begins f2650514548a
the log as written: no problems
line 11 changed to name sage in place of kit
the chain alone: line 11: the content does not match its own hash
with the kept head: line 11: the content does not match its own hash
line 11 deleted
the chain alone: line 11: prev_hash does not match the hash of the line before
with the kept head: line 11: prev_hash does not match the hash of the line before (and 1 more)
the last line, line 19, deleted
the chain alone: no problems
with the kept head: the log holds 18 line(s) but the kept head says 19
line 11 changed, then every hash from that line onwards worked out again
the chain alone: no problems
with the kept head: the last entry hash does not match the kept head
Reading it:
The times The careless log's first line says 08/03/2026 09:58:00. The careful log's first line says 2026-03-07T22:58:00.000Z. They are the same instant. The recorded run was made with the TZ environment variable set to Australia/Melbourne, as the TZ= at the front of the recorded command shows, and nothing in the careless line says so. The careless times in this output are from that one run.
The lines Both versions wrote 19 entries. The careless log has 21 lines, because the record id with two line breaks became three lines. The careful log has 19.
The detector It read none of the careless log's 21 lines and raised nothing. It read all 19 careful entries and raised five alerts: one failed_signin_burst for acct_m2, three privilege_change alerts, of which the admin's grant to their own account is urgent, and one repeated_object_denials for acct_m1. The fifth failed sign-in and the lock share one correlation identifier, the last in the first alert's list.
The quiet The two odd record ids were the fifth and sixth refused record requests by acct_m1 inside the window, and rule 3 did not fire a second time. The refused role change and the request with no session are not counted by rule 3.
The scan In the careless log the correct password is in 1 line, the wrong password in 6, the member's session cookie value in 8, the card number in 1 and the 500-character id in 1. In the careful log each is in none. The careful entry for the refused payment says dropped_fields 1: the handler passed the request body, and the logger's last step left it out.
The forgery Two lines of the careless log begin with forged text, and a reader counting the admin's sign-ins finds 2 where the story held 1. In the careful log the forged text is inside the id of one authz_fail entry, with each line break written as a reverse solidus and the letter n. A plain search for the forged sign-in words still finds that one line, which is why the count is made by field.
The chain The careless log has no hashes, so the check reports every line as unlinked, before and after each change. In the careful log the chain alone notices the changed entry and the deleted entry. It does not notice the last entry being deleted, or an entry changed with the later hashes worked out again. Checked against the kept head, all four are noticed. A hash chain shows that a record changed, not who changed it.
================================================================
STEP 2: run the tests against the careless version
================================================================
Command: node detection.test.mjs careless
✖ careless: rule 1 fires on five failed sign-ins for one account inside ten minutes (223.532ms)
✖ careless: rule 1 stays quiet on four failures followed by the right password (143.074125ms)
✖ careless: rule 2 fires on every role change, and is urgent when an account changes its own (32.162084ms)
✖ careless: rule 2 stays quiet when a member is refused a role change (28.458167ms)
✖ careless: rule 3 fires on four refused invoice requests by one account inside five minutes (31.725875ms)
✖ careless: rule 3 stays quiet on three refusals inside the window and a fourth after it (28.878542ms)
✖ careless: no submitted password reaches the log (0.142416ms)
✖ careless: the session cookie value does not reach the log (0.063584ms)
✖ careless: the card number does not reach the log (0.077875ms)
✖ careless: a record id with line breaks does not forge a log line (0.157584ms)
✖ careless: an over-long record id is cut before it is written (0.096708ms)
✖ careless: every entry carries a UTC time and the correlation id its response returned (238.466792ms)
✖ careless: a changed, deleted or rewritten entry is noticed (57.94125ms)
ℹ tests 13
ℹ suites 0
ℹ pass 0
ℹ fail 13
ℹ cancelled 0
ℹ skipped 0
ℹ todo 0
ℹ duration_ms 790.983625
✖ failing tests:
test at detection.test.mjs:48:1
✖ careless: rule 1 fires on five failed sign-ins for one account inside ten minutes (223.532ms)
AssertionError [ERR_ASSERTION]: failed_signin_burst alerts raised
0 !== 1
(stack trace and error details cut here, 11 lines: they hold local file paths)
test at detection.test.mjs:60:1
✖ careless: rule 1 stays quiet on four failures followed by the right password (143.074125ms)
AssertionError [ERR_ASSERTION]: lines the detector could not read as events: 5
5 !== 0
(stack trace and error details cut here, 12 lines: they hold local file paths)
test at detection.test.mjs:70:1
✖ careless: rule 2 fires on every role change, and is urgent when an account changes its own (32.162084ms)
AssertionError [ERR_ASSERTION]: privilege_change alerts raised
0 !== 3
(stack trace and error details cut here, 11 lines: they hold local file paths)
test at detection.test.mjs:83:1
✖ careless: rule 2 stays quiet when a member is refused a role change (28.458167ms)
AssertionError [ERR_ASSERTION]: lines the detector could not read as events: 2
2 !== 0
(stack trace and error details cut here, 12 lines: they hold local file paths)
test at detection.test.mjs:94:1
✖ careless: rule 3 fires on four refused invoice requests by one account inside five minutes (31.725875ms)
AssertionError [ERR_ASSERTION]: repeated_object_denials alerts raised
0 !== 1
(stack trace and error details cut here, 11 lines: they hold local file paths)
test at detection.test.mjs:109:1
✖ careless: rule 3 stays quiet on three refusals inside the window and a fourth after it (28.878542ms)
AssertionError [ERR_ASSERTION]: lines the detector could not read as events: 5
5 !== 0
(stack trace and error details cut here, 12 lines: they hold local file paths)
test at detection.test.mjs:127:1
✖ careless: no submitted password reaches the log (0.142416ms)
AssertionError [ERR_ASSERTION]: lines holding the correct password
1 !== 0
(stack trace and error details cut here, 15 lines: they hold local file paths)
test at detection.test.mjs:132:1
✖ careless: the session cookie value does not reach the log (0.063584ms)
AssertionError [ERR_ASSERTION]: lines holding the session cookie value
4 !== 0
(stack trace and error details cut here, 15 lines: they hold local file paths)
test at detection.test.mjs:136:1
✖ careless: the card number does not reach the log (0.077875ms)
AssertionError [ERR_ASSERTION]: lines holding the card number
1 !== 0
(stack trace and error details cut here, 15 lines: they hold local file paths)
test at detection.test.mjs:140:1
✖ careless: a record id with line breaks does not forge a log line (0.157584ms)
AssertionError [ERR_ASSERTION]: lines in the log, against entries written
7 !== 5
(stack trace and error details cut here, 15 lines: they hold local file paths)
test at detection.test.mjs:146:1
✖ careless: an over-long record id is cut before it is written (0.096708ms)
AssertionError [ERR_ASSERTION]: lines holding all 500 characters
1 !== 0
(stack trace and error details cut here, 15 lines: they hold local file paths)
test at detection.test.mjs:153:1
✖ careless: every entry carries a UTC time and the correlation id its response returned (238.466792ms)
AssertionError [ERR_ASSERTION]: responses that returned a correlation id
0 !== 5
(stack trace and error details cut here, 11 lines: they hold local file paths)
test at detection.test.mjs:163:1
✖ careless: a changed, deleted or rewritten entry is noticed (57.94125ms)
AssertionError [ERR_ASSERTION]: the logger keeps a head: a count and a last hash
(stack trace and error details cut here, 11 lines: they hold local file paths)
The process exits with code 1. All 13 tests fail, and each failure names what it found. The three tests that fire a rule fail because no alert was raised. The three quiet tests fail on the check that the detector could read the log, and not on the absence of alerts. Each test stops at its first failed check. Thirteen stack traces were cut from this output, with the error details after each, because they hold file paths from the computer that ran the lab.
================================================================
STEP 3: run them against the careful version
================================================================
Command: node detection.test.mjs careful
✔ careful: rule 1 fires on five failed sign-ins for one account inside ten minutes (220.574375ms)
✔ careful: rule 1 stays quiet on four failures followed by the right password (142.545667ms)
✔ careful: rule 2 fires on every role change, and is urgent when an account changes its own (30.733834ms)
✔ careful: rule 2 stays quiet when a member is refused a role change (28.947666ms)
✔ careful: rule 3 fires on four refused invoice requests by one account inside five minutes (30.971375ms)
✔ careful: rule 3 stays quiet on three refusals inside the window and a fourth after it (29.453ms)
✔ careful: no submitted password reaches the log (0.088333ms)
✔ careful: the session cookie value does not reach the log (0.036292ms)
✔ careful: the card number does not reach the log (0.048334ms)
✔ careful: a record id with line breaks does not forge a log line (0.146417ms)
✔ careful: an over-long record id is cut before it is written (0.065334ms)
✔ careful: every entry carries a UTC time and the correlation id its response returned (138.336083ms)
✔ careful: a changed, deleted or rewritten entry is noticed (57.164208ms)
ℹ tests 13
ℹ suites 0
ℹ pass 13
ℹ fail 0
ℹ cancelled 0
ℹ skipped 0
ℹ todo 0
ℹ duration_ms 684.170084
All 13 tests pass. The exit code recorded for this run, which the output above does not print, was 0. With no argument the file tests the careful version, which is what node --test runs when it finds the file by its .test.mjs name:
Command: node --test
✔ careful: rule 1 fires on five failed sign-ins for one account inside ten minutes (218.03675ms)
✔ careful: rule 1 stays quiet on four failures followed by the right password (142.108417ms)
✔ careful: rule 2 fires on every role change, and is urgent when an account changes its own (30.520834ms)
✔ careful: rule 2 stays quiet when a member is refused a role change (29.673167ms)
✔ careful: rule 3 fires on four refused invoice requests by one account inside five minutes (30.3615ms)
✔ careful: rule 3 stays quiet on three refusals inside the window and a fourth after it (31.377458ms)
✔ careful: no submitted password reaches the log (0.106625ms)
✔ careful: the session cookie value does not reach the log (0.037875ms)
✔ careful: the card number does not reach the log (0.041583ms)
✔ careful: a record id with line breaks does not forge a log line (0.134042ms)
✔ careful: an over-long record id is cut before it is written (0.058708ms)
✔ careful: every entry carries a UTC time and the correlation id its response returned (133.856667ms)
✔ careful: a changed, deleted or rewritten entry is noticed (58.853083ms)
ℹ tests 13
ℹ suites 0
ℹ pass 13
ℹ fail 0
ℹ cancelled 0
ℹ skipped 0
ℹ todo 0
ℹ duration_ms 899.166584
All 13 tests pass in this run as well, and the exit code recorded for it was 0.
================================================================
STEP 4: point the lab at your own application
================================================================
1. Copy event-schema.mjs and change the event names, the detail keys and the reason lists to your own. Keep the lists in one file.
2. Write your logger so that it builds each event from named fields and passes it through one last step that enforces the lists, as enforceFieldLists does. Do not hand that step a way to be skipped.
3. Make the clock something a test can set. The lab passes a clock into the portal and the loggers.
4. Rewrite the rules in detector.mjs for your events, with thresholds and windows taken from your own normal traffic, and write down why each number is what it is.
5. For each rule, copy the pair of tests in detection.test.mjs: one sequence that fires it, one near miss, and the check that the detector read every line.
6. Replace the fixture values in garden-data.mjs with marked values for the secrets your own routes accept, and send each through the route that accepts it.
7. Run the tests in the pipeline that deploys your application, as a check that has to pass before a change merges.
8. Then test the part the lab does not have: send a marked event through your real log pipeline and confirm that the named person received the alert.What security logging and monitoring is, and how it fails
Security logging is an application writing down the events that matter for security: who signed in, who was refused, who changed what another account may do.
Monitoring is something reading those records while they are fresh, and alerting is that reader telling a person. Each depends on the one before it. An alert cannot fire on an event nobody recorded, and a record nobody reads is only useful after the damage, if it is still there.
The OWASP Top 10:2025 gives the subject its ninth category, A09, which its page titles Security Logging & Alerting Failures. The page says the category keeps its position at number nine with a slight name change, to emphasise the alerting function needed to induce action on relevant logging events.
Its description begins: without logging and monitoring, attacks and breaches cannot be detected, and without alerting it is very difficult to respond quickly and effectively during a security incident.
The page also calls the category incredibly difficult to test for, says it has minimal representation in the CVE and CVSS data, and says it was voted into the list by community survey participants for the third time.
MITRE's catalogue of software weaknesses names the base case CWE-778, Insufficient Logging: when a security-critical event occurs, the product either does not record the event or omits important details about the event when logging it. Both halves count. A failed sign-in that leaves no line is one failure, and a line that says a sign-in failed without saying for which account is the other.
ASVS 5.0 treats the subject in chapter V16, Security Logging and Error Handling. Its control objective says security logs are distinct from error or performance logs and are used to record security-relevant events such as authentication decisions, access control decisions, and attempts to bypass security controls, such as input validation or business logic validation.
One limit of ASVS is worth knowing before you use the chapter as a checklist. Section V16.3 notes that while ASVS includes logging of security events in scope, alerting and correlation are considered out of scope and are handled by operational and monitoring systems.
Its examples of what is out of scope are rules in a security information and event management (SIEM) system and monitoring infrastructure. So ASVS says what to record and how to protect it, and A09 is where to look for whether anyone is told.
The signs OWASP lists
A09 lists the conditions under which logging, monitoring, detection and alerting are insufficient. Closely paraphrased, they are:
- Auditable events, such as logins, failed logins and high-value transactions, are not logged or are logged inconsistently, for instance only logging successful logins but not failed attempts.
- Warnings and errors generate no, inadequate or unclear log messages.
- The integrity of logs is not properly protected from tampering.
- Logs of applications and APIs are not monitored for suspicious activity.
- Logs are only stored locally, and not properly backed up.
- Appropriate alerting thresholds and response escalation processes are not in place or effective, and alerts are not received or reviewed within a reasonable amount of time.
- Penetration testing and scans by dynamic application security testing (DAST) tools, of which the page names two, do not trigger alerts.
- The application cannot detect, escalate or alert for active attacks in real time or near real time.
- The application is vulnerable to sensitive information leakage, by making logging and alerting events visible to a user or an attacker, or by logging sensitive information that should not be logged, such as PII or PHI.
- Log data is not correctly encoded, which leaves the logging or monitoring systems vulnerable to injections or attacks.
- The application is missing or mishandling errors and other exceptional conditions, such that the system is unaware there was an error and is therefore unable to log that there was a problem.
- Adequate use cases for issuing alerts are missing or outdated to recognise a special situation.
- Too many false positive alerts make it impossible to distinguish important alerts from unimportant ones, so that they are recognised too late or not at all.
- Detected alerts cannot be processed correctly because the playbook for the use case is incomplete, out of date or missing.
Why your application's own events matter when the server already has access logs
If a web server or hosting platform in front of your application already keeps a log of requests, it is fair to ask why the application should keep another.
OWASP's Logging Cheat Sheet answers directly. It says many systems enable network device, operating system, web server, mail server and database server logging, but often custom application event logging is missing, disabled or poorly configured, and that application logging provides much greater insight than infrastructure logging alone. Web application logging, it says, is much more than having web server logs enabled.
The reason is where the knowledge sits. The cheat sheet calls the application code itself the primary event data source. The application, it says, has the most information about the user, with identity, roles and permissions as its examples, and about the context of the event, which it gives as target, action and outcomes, and this data is often not available to either infrastructure devices or even closely related applications.
The lab on this page shows the gap in one request. A signed-in member asks for an invoice that belongs to another member. The portal answers 404, the same reply it gives for an invoice that does not exist, so the caller learns nothing. A record of the request alone would hold a method, an address and a 404.
The careful version's own log holds an event named authz_fail with the outcome denied, the account that asked, the invoice it asked for and the reason not_owner. Four of those inside five minutes is a pattern worth a person's attention, and it can only be counted by something that can tell not_owner from a mistyped link.
Why the reply is a 404 and not a 403 is covered in the broken access control and IDOR guide.
Keep the other logs. The cheat sheet lists further sources of information about application usage that could also be considered, among them network firewalls, network and host intrusion detection systems, application firewalls, and database applications with their automatic audit trails. It adds that data from systems in a different trust zone may be missing, modified, forged or replayed and could be malicious, and must always be treated as untrusted data. The application's security events sit beside those sources and do not replace them.
Security event logging: which events to record
Start from the decisions your application makes about people: who they are, and what they may do.
ASVS 5.0 section V16.3 sets out four requirements, all at Level 2.
- 16.3.1:all authentication operations are logged, including successful and unsuccessful attempts. Additional metadata, such as the type of authentication or factors used, should also be collected.
- 16.3.2:failed authorisation attempts are logged. For Level 3, this must include logging all authorisation decisions, including logging when sensitive data is accessed, without logging the sensitive data itself.
- 16.3.3:the application logs the security events that are defined in the documentation and also logs attempts to bypass the security controls, such as input validation, business logic and anti-automation.
- 16.3.4:the application logs unexpected errors and security control failures such as backend TLS failures.
The section says of itself that it outlines the types of events that should be logged but does not attempt to provide exhaustive detail, and that each application has unique risk factors and operational context. A09's prevention list opens by saying developers should implement some or all of the controls on it, depending on the risk of the application.
Two of its eleven controls add to what V16.3 names: ensure all login, access control and server-side input validation failures can be logged with sufficient user context to identify suspicious or malicious accounts, and held for enough time to allow delayed forensic analysis; and ensure that every part of your app that contains a security control is logged, whether it succeeds or fails.
OWASP's Logging Cheat Sheet gives the longest list. It says the level and content of security monitoring, alerting and reporting needs to be set during the requirements and design stage of projects, and should be proportionate to the information security risks.
Under the words "where possible, always log", its entries include input validation failures, output validation failures, authentication successes and failures, authorisation failures, session management failures, application errors and system events, application start-ups and shut-downs, use of higher-risk functionality, legal and other opt-ins, and suspicious business logic activities.
Under higher-risk functionality it names, among others, user administration actions such as addition or deletion of users and changes to privileges, access to sensitive data such as payment cardholder data, and data import and export.
The same cheat sheet warns against treating any list as the answer: there is no one size fits all solution, and a blind checklist approach can lead to unnecessary "alarm fog" that means real problems go undetected. MITRE's CWE-778 gives the cost side.
Its mitigations say sufficient data should be logged to enable system administrators to detect attacks, diagnose errors and recover from attacks, and that logging too much data can cause the same problems, including unexpected costs when using a cloud environment.
This guide works through three families of event that an application with accounts and records has, and builds its schema, rules and tests around them. Treat them as the floor. The review checklist in the asset lists the rest of what the standards name.
Record every sign-in attempt with its outcome, and record the lock when repeated failures cause one. CWE-778's first example gives a reason to log both outcomes: logging failed authentication attempts can warn administrators of potential brute force attacks, and logging successful authentication events can provide a useful audit trail when a legitimate account is compromised.
In the lab's schema, a failed sign-in records the account that was aimed at as the object and leaves the actor empty, because nobody has been authenticated yet. It records which of three reasons applied: a wrong password, an unknown account or a locked account.
The caller sees the same reply for all three. What to keep about a username that matches no account is a decision to make on purpose, because the typed text can hold a mistyped password, and the lab keeps none of it.
Telling guesses at one account apart from stolen pairs tried across a list of accounts takes more than a per-account count, and the credential stuffing guide covers those measures.
Record every change to what an account may do: the account that made the change, the account that was changed, and what was granted or removed. The cheat sheet's higher-risk list has user administration actions, changes to privileges among them, and OWASP's Application Logging Vocabulary Cheat Sheet proposes an event for it, authz_change, described as the user or entity entitlements being changed.
Name both people by account, and build the record from the actor's roles as they stood when the request arrived. An event assembled after the change has been applied can show an admin who granted themselves a role as already holding it. A refused attempt is a different event.
In the lab, a member who asks to grant themselves the admin role gets a 403 and an authz_fail record with the reason role_required. Nothing changed, so no privilege change is recorded, and the attempt is still on the record.
Record each refusal with the account, the type and id of the record asked for, and the reason. ASVS 16.3.2 is the requirement, and A01 in the OWASP Top 10:2025 recommends logging access control failures and alerting admins when appropriate, such as after repeated failures.
The lab records three reasons for a refused record request: no session, a record that belongs to someone else, and a record id that does not exist. It files the third under the same event name on purpose, so that an account working through guessed ids shows up in one place. That is this page's choice, and your application may prefer a separate event for it.
What each record needs to be useful a month later
Write each event for a reader who was not there: someone working through an incident weeks afterwards, with the log and nothing else.
ASVS 5.0 requirement 16.2.1, at Level 2, asks that each log entry includes necessary metadata, such as when, where, who and what, that would allow for a detailed investigation of the timeline when an event happens. The Logging Cheat Sheet uses the same four words and says the application logs must record them for each event.
A line such as "login failed for sage" answers one of the four and part of another. The schema in the asset gives each answer its own field, so that the log can be questioned field by field and nobody has to read sentences.
- When. One instant, written the same way on every line. ASVS 5.0 requirement 16.2.2, at Level 2, asks that time sources for all logging components are synchronised, and that timestamps in security event metadata use UTC or include an explicit time zone offset. It recommends UTC, to ensure consistency across distributed systems and to prevent confusion during daylight saving time transitions.
- Where. Which application wrote the event and which entry point was called. The cheat sheet's examples include an application identifier and, for a web application, the entry point URL and HTTP method. The lab records the route as a pattern, such as GET /api/invoices/:id, and not the address that was sent.
- Who. An account identifier, and the address the request came from. The cheat sheet's examples of a user identity include a user database table primary key value. An id keeps a person's name and email address out of every line and still lets an investigator look the person up.
- What. The name of the event, from a fixed list, and its outcome.
- On what. The type and id of the record or account the action was aimed at. The cheat sheet lists the object among the attributes to consider recording, with a user account, a data resource and a file as examples.
- Why it went that way. A reason from a fixed list, such as not_owner. The cheat sheet's entry for it is the reason why the status occurred.
RFC 3339, published in July 2002 in the Standards Track category, defines a date and time format for use in Internet protocols. Its section 5.6 defines the Internet Date/Time Format, a profile of ISO 8601, and says it SHOULD be used in new protocols on the Internet: a date, the letter T, a time and an offset, which is either Z or a number of hours and minutes with a plus or minus sign.
Section 4.1 says that because the daylight saving rules for local time zones are so convoluted and can change based on local law at unpredictable times, true interoperability is best achieved by using UTC.
Section 5.1 points out a practical benefit: when the components run from least precise to most precise and every time uses the same zone, the same offset string and the same number of fraction digits, the strings sort into time order as plain text.
The lab's careless version shows what the RFC is warning about. It writes the computer's local time as day, month and year with no zone: 08/03/2026 09:58:00. Section 5.2 of the RFC says the date format 10/11/1996 is completely unsuitable for global interchange, because it is interpreted differently in different countries.
The careless line has the same weakness, and nothing in it says which zone the clock was in. The careful version writes the same instant as 2026-03-07T22:58:00.000Z.
A reader has to be told that the first line came from a run set to Melbourne time to know that the two describe one moment, which falls on a different calendar day in UTC.
One detail changed in 2024. RFC 9557 updates RFC 3339 in how the offset Z is interpreted: it now means the same as an offset written -00:00, that the time in UTC is known but the offset to local time is unknown, while the offset +00:00 keeps the implication that UTC is the preferred reference point. Either way, the instant is stated in UTC.
Format is half of it. A timestamp is only as good as the clock behind it. Requirement 16.2.2 begins with synchronised time sources. The cheat sheet's event collection advice includes synchronising time across all servers and devices. It notes that this is not always possible where the application runs on a device under some other party's control, and suggests in those cases attempting to measure the time offset or recording a confidence level in the event timestamp.
A detection rule, a dashboard and a search all key on the event's name. If one developer writes "login failed", another "Sign-in failure" and a third "auth error", each rule has to know all three spellings and will miss the fourth.
The cheat sheet asks each organisation to ensure it has a consistent, and documented, approach to the classification of events, the syntax of descriptions, and field lengths and data types, including the format used for dates and times.
OWASP's Application Logging Vocabulary Cheat Sheet proposes a list to start from. It says its intent is to simplify monitoring and alerting such that, assuming developers trap errors and log them using this vocabulary, monitoring and alerting would be improved by simply keying on these terms.
The vocabulary is that cheat sheet's proposal, and this page borrows five of its names: authn_login_success, authn_login_fail, authn_login_lock, authz_change and authz_fail. The vocabulary writes the user id into the event string after a colon.
The schema here keeps the name fixed and puts each identifier in a field of its own, which is this page's choice: a fixed string can be matched exactly, and an id in its own field can be counted.
A fixed list has a second use. In the lab, the detector reads a line only if its event name, outcome and field names are on the schema's lists. Any other line is counted as skipped, and that count deserves a place on a dashboard of its own. A log full of lines the detector cannot read looks exactly like a quiet day.
ASVS 5.0 requirement 16.2.4, at Level 2, asks that logs can be read and correlated by the log processor that is in use, preferably by using a common logging format. Requirement 16.2.3, at the same level, asks that the application only stores or broadcasts logs to the files and services that are documented in the log inventory.
The inventory is requirement 16.1.1, also Level 2: a document of the logging performed at each layer of the application's technology stack, what events are being logged, log formats, where that logging is stored, how it is used, how access to it is controlled, and for how long logs are kept.
A version field on every event, which is this page's advice, ties each line to the edition of that document it was written under.
In this guide, a correlation identifier is a random value created once for each request and written on every event that request produces. The cheat sheet has a wider idea it calls an interaction identifier: a method of linking all relevant events for a single user interaction, for which its examples are a desktop application form submission, a web page request, a mobile app button click and a web service call.
Its reasoning is that the application knows these events relate to the same interaction, and this should be recorded instead of losing the information and forcing subsequent correlation techniques to reconstruct the separate events.
It earns its place in three ways, and these are this page's reading. It groups the events of one request: in the lab, the fifth failed sign-in and the account lock it caused carry the same identifier, so nobody has to infer from two timestamps that they belong together.
It joins the application's log to any other record of the same request, provided each layer writes the same value.
And it gives a customer something safe to quote: the lab returns the identifier in a response header, so a support request can name the exact request without anyone reading out a session cookie.
Create it on the server, at the first component you control. If your services already propagate W3C Trace Context, an identifier of this kind exists already. That Recommendation, dated 23 November 2021, defines a traceparent HTTP header that identifies the incoming request in a tracing system. One of its four fields, the trace-id, is used to uniquely identify a distributed trace through a system.
Its privacy section says tracing vendors must not use the traceparent and tracestate fields for any personally identifiable or otherwise sensitive information. The same discipline suits an identifier of your own: random, and about nothing.
Log redaction: keeping passwords, tokens and card numbers out
A security log is written at the moments when secrets are close at hand.
A sign-in event is created by the code that holds the password, and a refused payment by the code that holds the card number. Personal details need the same care in a log as in a response, and the sensitive data exposure guide works through that field by field. This section is about the mechanism: how a secret gets into a log, and where to stop it.
ASVS 5.0 requirement 16.2.5, at Level 2, sets the rule: when logging sensitive data, the application enforces logging based on the data's protection level. Its first example is that it may not be allowed to log certain data, such as credentials or payment details. Its second is that other data, such as session tokens, may only be logged by being hashed or masked, either in full or partially.
MITRE's entry for the weakness is CWE-532, Insertion of Sensitive Information into Log File, whose mitigations include not writing secrets into the log files.
The Logging Cheat Sheet's section on data to exclude has two lists, and their verbs differ. Items on the first should usually not be recorded directly in the logs, but instead should be removed, masked, sanitised, hashed or encrypted; they include session identification values, access tokens, authentication passwords, encryption keys and bank account or payment card holder data.
Items on the second can sometimes also exist and, while useful for subsequent investigation, may also need to be treated in some special manner before the event is recorded. The section opens with a wider instruction: never log data unless it is legally sanctioned.
The lab's careless version shows one way in: a logger that is handed the request and writes all of it. After one story of nineteen logged entries, the correct password sits in one line of its log, the wrong password in six, one member's session cookie value in eight and the card number in one.
No line was written in order to record a secret. Each secret was copied in with a request body, or used as a convenient label for who was acting.
The wrong password belongs on that list as much as the right one, since a wrong password can be one keystroke away from the real thing.
Three places to stop it
The careful version stops the same secrets at three points, and the third is the one that keeps the first two honest.
- Build the event from named fields. The careful logger is handed the same request information as the careless one, and never reads the body or the session cookie from it. It takes ids and words from fixed lists. Code that never touches the body cannot copy a secret out of it.
- Enforce the field list at the last step before writing. One handler in the lab passes its logger the whole parsed request body as extra detail, which a handler should not do. The careful logger's last step rebuilds every event from the schema's lists, so the extra key is dropped, and the event records that one field was dropped without recording its name or value. The card number in that body reached the careless log and did not reach the careful one.
- Prove it with fixtures. The lab sends requests that carry a marked password, a session cookie, an invented card number and two awkward record ids, then searches the log for those exact values. A test that fails when a fixture value appears in the log catches the next logging change that copies a body, whoever makes it.
Stand-ins, and two limits
Where events have to be tied to a session, the sources point to a stand-in for the token. Requirement 16.2.5 allows hashing or masking, and the cheat sheet suggests considering a hashed value if one is needed to track session specific events. The lab's schema has no session field at all. It ties events together with the account id and the correlation identifier.
Two limits apply. A field list controls which fields exist, not what an allowed field holds: the record id in the lab's events is whatever the caller put in the address, cut to 200 characters. And none of this cleans a log that already holds a secret. Revoking the secret and redacting the copies is the subject of the secrets exposure guide.
Log injection: when a caller writes lines into your log
Log injection is a caller supplying text that the application writes into its log in a way that changes what the log appears to say.
MITRE's entry is CWE-117, Improper Output Neutralization for Logs: the product constructs a log message from external input, but it does not neutralise or incorrectly neutralises special elements when the message is written to a log file. The entry lists log forging as an alternate term, for cases in which the adversary can add additional log entries or modify how a log entry is parsed.
The special element that matters first is the line break. A text log is read one line at a time, so whoever can put a line break into a logged value can end the real entry and begin one of their own.
CWE-117's second example is code that logs a username when registration fails. A username with an encoded newline followed by the words Admin Access Granted produces a second line that reads exactly that, which the entry says could trick administrators into believing that false events occurred or even bypass security monitoring systems.
Its consequences go further than false entries. Forged or corrupted log files can be used to cover an attacker's tracks or to implicate another party, a log that is processed automatically can be made unusable by corrupting its format, and an attacker may inject code or other commands into the log file and take advantage of a vulnerability in the log processing utility.
The lab sends one such value: a record id made of an id that does not exist, a line break, a line shaped like the careless log's own sign-in line for the admin, another line break and a line shaped like a JSON event.
The careless version joins the id into a sentence. Its log gains two lines that no request wrote, so nineteen entries become twenty-one lines, and a reader counting sign-ins by the admin finds two where the story held one.
How structured logging and encoding address it
ASVS 5.0 requirement 16.4.1, at Level 2, asks that all logging components appropriately encode data to prevent log injection, and one of the controls on A09's prevention list is to ensure log data is encoded correctly to prevent injections or attacks on the logging or monitoring systems.
The Logging Cheat Sheet says to apply defences such as these: do not use unstructured text log formats, use structured log formats such as JSON, and limit the size of the user input value used to create the log message.
It notes that some logging frameworks do not consider log injection through line breaks in unstructured logs to be a vulnerability, which means it will not be fixed in them.
A structured format such as JSON helps when the writer follows its encoding rules, because JSON's rules leave no way to write a raw line break inside a string. RFC 8259, the JSON standard, says in section 7 that the characters that must be escaped inside a string are the quotation mark, the reverse solidus and the control characters from U+0000 through U+001F.
Its grammar gives line feed, U+000A, a two-character escape: a reverse solidus followed by the letter n. In the lab's careful log the forged value is one string inside one event.
The line breaks appear as those two characters, the quotation marks in the forged JSON are escaped as well, nineteen entries are nineteen lines, and no line begins with forged text.
Three cautions belong with that result. A plain text search of the careful log still finds the forged words, because they are there, inside the id field of one denied request: read a structured log by its fields, not by searching for phrases.
Encoding for the log file does not make a value safe for every later reader, and the cheat sheet says that in web applications the logs should not be exposed in web-accessible locations and, if they are, should have restricted access and be configured with a plain text MIME type, not HTML.
And length matters as well as content: the lab's careful logger cuts a 500-character record id to 200 characters and marks the cut.
Audit log security: protecting the log from the person it records
A log is evidence about people, and some of those people can reach it.
An administrator whose role change is recorded may be able to edit the file. Someone who takes over the application server takes over whatever that server can write. ASVS 5.0 section V16.4 says that if logs can be easily modified or deleted, they lose their integrity and become unreliable for incident investigations or legal proceedings, and it sets three requirements at Level 2. Requirement 16.4.1 is the encoding rule from the last section. The other two are about custody.
- 16.4.2:logs are protected from unauthorised access and cannot be modified.
- 16.4.3:logs are securely transmitted to a logically separate system for analysis, detection, alerting and escalation. The requirement states its aim: to ensure that if the application is breached, the logs are not compromised.
The Logging Cheat Sheet turns custody into working rules. At rest: build in tamper detection so you know if a record has been modified or deleted, store or copy log data to read-only media as soon as possible, record and monitor all access to the logs, and restrict the privileges to read log data and review them periodically.
When logs go to a database, it prefers a separate database account that is only used for writing log data and has very restrictive permissions.
In operation, it asks for processes to detect whether logging has stopped, and to identify tampering or unauthorised access and deletion.
A09 counts two custody failures among its signs: logs whose integrity is not properly protected from tampering, and logs that are only stored locally and not properly backed up. One of the controls on its prevention list is to ensure all transactions have an audit trail with integrity controls to prevent tampering or deletion, such as append-only database tables or similar.
Two questions shape the design, and both are this page's advice. Who can read the log? It names accounts, addresses and what each person tried to do, so the list of readers should be short and written down.
Who can delete or change it? If the answer includes the application's own server account, or the administrators the log is meant to hold to account, the log can be edited by the people it records. Requirement 16.4.3 asks that logs are securely transmitted to a logically separate system.
In this page's reading, that is what moves a copy out of their reach. Sending each event as it is written is this page's advice.
One form of tamper detection is a hash chain. Each entry stores the hash of the entry before it and a hash of its own content, so an entry cannot be changed or removed without the values after it ceasing to agree. The lab's careful logger writes three extra fields on every line for this, and its integrity check tries four kinds of interference on a log of nineteen entries.
- One entry changed so that it names a different account. The chain notices: the content of that line no longer matches its own hash.
- One entry deleted from the middle. The chain notices: the next line's link no longer matches the line before it.
- The last entry deleted. The chain alone notices nothing, because every remaining line still agrees with its neighbours.
- One entry changed, and every hash from that line onwards worked out again. The chain alone notices nothing, because the person editing ran the same arithmetic the logger does.
The last two kinds of interference are caught only by a value kept outside the log. The lab calls it the kept head: the number of entries written and the hash of the last one.
Checked against that, the shortened log holds eighteen lines where the head says nineteen, and the rewritten log ends in a hash the head does not match. A chain inside the log is therefore worth having only together with a head stored where the log's own writers cannot change it, which brings the design back to the separate system in 16.4.3. That reading of the result is this page's own.
Be clear about what the check proves. A hash chain shows that a record changed, or that records are missing. It does not show who changed them, when, or what the original said. The cheat sheet makes a related point about logs in general: non-repudiation is hard to achieve for logs, because their trustworthiness is often just based on the logging party being audited properly, while mechanisms like digital signatures are hard to utilise here.
This page gives no number, because the right one depends on obligations it cannot see. What the sources say is narrower. One of the controls on A09's prevention list is that login, access control and server-side input validation failures are held for enough time to allow delayed forensic analysis.
ASVS 16.1.1 asks that the logging inventory documents for how long logs are kept. The Logging Cheat Sheet says log data, temporary debug logs, and backups, copies and extractions must not be destroyed before the duration of the required data retention period and must not be kept beyond this time, and that legal, regulatory and contractual obligations may impact on these periods.
This page's advice is to decide the period for each kind of log, write it into the inventory with the reason for it, and check that the oldest record you can still read is as old as the period says.
For the wider work of running log infrastructure, both OWASP cheat sheets refer to NIST SP 800-92, Guide to Computer Security Log Management. NIST's page gives its date of publication as September 2006 and lists it as final. A revision, SP 800-92 Rev. 1, retitled Cybersecurity Log Management Planning Guide, was published as an initial public draft on 11 October 2023.
NIST's page for the draft says comments were due on 29 November 2023 and that the public comment period is closed, and when this guide was written the draft was still the only entry in that page's document history.
From record to alert: choosing what someone will act on
A record helps with today's incident only if it reaches a person in time.
The 2025 edition changed the category's name to say so, and several of A09's signs are about the last step and not the first: thresholds and escalation processes that are not in place or not effective, alerts that are not received or reviewed within a reasonable amount of time, use cases for alerts that are missing or outdated, and playbooks that are incomplete, out of date or missing.
The opposite failure is on the list as well: too many false positive alerts, so that important ones are recognised too late or not at all. It is a close relative of the alarm fog the Logging Cheat Sheet warns about. The aim is a short list of alerts that are each read.
One of the controls on A09's prevention list is that DevSecOps and security teams should establish effective monitoring and alerting use cases, including playbooks, such that suspicious activities are detected and responded to quickly by the security operations centre team.
For each alert, write down four things before it is switched on. These four are this page's advice.
- What it means, in one sentence a non-specialist can read: five failed sign-ins for one account inside ten minutes.
- Who receives it, by name or by roster, and through which channel.
- What they do first. Each rule in the lab carries its first step inside the alert, so the person who receives it does not start from a blank page.
- Why the threshold is what it is. A number chosen from your own normal traffic can be explained and adjusted. A number copied from an example cannot.
The lab has three rules. Every number in them is the lab's own choice, not a recommendation.
- Failed sign-ins: five failures for one account inside 600 seconds. It stays quiet on four failures followed by the right password, which is what a member who has half-forgotten a password looks like.
- Privilege change: every role change raises an alert, and the priority rises from high to urgent when the account that made the change is the account that was changed. A refused attempt to change a role raises nothing under this rule.
- Repeated object denials: four refused record requests by one account inside 300 seconds. It stays quiet on three inside the window and a fourth after it.
Rules 1 and 3 stay quiet for an account, once they have fired for it, until a full window has passed, so one burst produces one alert and not one for every further request. Rule 2 alerts on every privilege change.
Each alert names the rule, the priority, the accounts involved, the event times and the correlation identifiers of the events that caused it, so its receiver can go from the alert to the exact lines.
A09's prevention list has further techniques. Honeytokens are traps placed in the application, for which its examples include a real or technical user identity. Because they are not used in normal business, any access generates logging data that can be alerted on with nearly no false positives.
Behaviour analysis and AI support, it says, could optionally be an additional technique to support low rates of false positives for alerts.
Another control on the list is to establish or adopt an incident response and recovery plan, and to teach developers what application attacks and incidents look like, so they can report them.
The phrase intrusion detection covers several different kinds of system, and it helps to know what each can see. NIST's glossary gives three definitions of an intrusion detection system. One of them is a security service that monitors and analyses network or system events for the purpose of finding, and providing real-time or near real-time warning of, attempts to access system resources in an unauthorised manner.
NIST SP 800-94, its guide to intrusion detection and prevention systems, was published in February 2007, and a planning note on NIST's page says a draft revision released in 2012 never became final and has been retired.
Its abstract describes four classes of these systems: network-based, wireless, network behaviour analysis software and host-based. It also describes security information and event management software as a complementary technology that can detect intrusions.
Those classes watch networks and hosts. What this guide describes is detection inside the application, built on events the application itself emits, and the difference is what each layer knows. The Logging Cheat Sheet lists network and host intrusion detection systems and web application firewalls among the other sources of event data, and says the application has the most information about the user and the context of the event.
Rule 3 in the lab, repeated requests for other members' invoices, depends on knowing which account owns which invoice. In the lab, only the portal knows that.
OWASP has a project that names the approach. The AppSensor project's page says it defines a conceptual framework and methodology that offers prescriptive guidance to implement application layer intrusion detection and automated response. This page mentions it to place the term, not to recommend it or any product.
A managed monitoring service is a third thing again: people employed by another business who receive your alerts and respond to them. A service like that can act only on what reaches it. If the application does not emit the three families of event in this guide, nothing that arrives says which account was refused which record.
Whichever arrangement you choose, the events have to exist first, and ASVS is explicit that its requirements stop at that point, with alerting and correlation handled by operational and monitoring systems.
How to test that an alert fires
An alert that has never fired is a hope.
A09 puts the test into its list of signs: penetration testing and scans by DAST tools do not trigger alerts. The Logging Cheat Sheet's verification section says logging functionality and systems must be included in code review, application testing and security verification processes. Its items include ensuring the logging is working correctly and as specified, checking that events are being classified consistently, ensuring logging is implemented and enabled during application security, fuzz, penetration and performance testing, and testing that the mechanisms are not susceptible to injection attacks.
The most direct test plays the behaviour and asserts the alert. The lab's detection tests do that for each rule, in pairs, with one extra check that is easy to forget.
- A test that fires the rule. It sends the requests, five wrong passwords a minute apart for example, runs the detector over the log they left, and asserts that one alert was raised with the expected account, count, priority and times.
- A test that comes close and has to stay quiet. Four wrong passwords and then the right one. A refused role change. Three refused invoice requests inside the window and a fourth 390 seconds after the first.
- A check that the detector could read the log. A quiet result means something only if the events were read. The careless log raises no alerts at all, because the detector can read none of its lines, so a near-miss test that asserted only silence would pass on it. The lab's near-miss tests also assert that no line was skipped and that the expected number of events was read.
Time, pipelines and the parts a unit test cannot reach
Time is the awkward part of testing a window, since nobody wants a test that waits ten minutes. The lab's portal takes its clock from the test, which moves it forward between requests. If your application reads the system clock directly in the code that stamps events, changing that is the first step towards alerts you can test. This is this page's advice.
Run the tests where a failure blocks a release. The Node.js test runner documentation says the process exit code is set to 1 if any tests fail. In the lab, all thirteen tests fail against the careless version and all thirteen pass against the careful one.
A test like this stops at the detector. Two more checks belong to the running system. The first is this page's advice: send a marked test event through the real pipeline on a schedule and confirm that the named person received the alert, because rules, forwarding and paging each fail separately.
The second is the cheat sheet's: test the effect on the application of logging failures, for which its examples include simulated database connectivity loss, lack of file system space and missing write permissions, and ensure logging cannot be used to deplete system resources.
It adds that if log data is used in any action against users, such as blocking access or account lock-out, you should ensure this cannot be used to cause denial of service of other users.
The lab: one member portal, a careless log and a careful log
The lab is an illustration written for this page: an invented application, an in-memory log and a detector written for the purpose.
It is not a monitoring product, its thresholds are not recommendations, and nothing in it is a client's code or a client's result.
The application is the member portal of an invented community garden, with four accounts and five plot-fee invoices. It has four routes: sign in, read an invoice, pay an invoice and change an account's roles. It comes in two versions that run the same routes and the same sign-in, lockout, role and ownership checks, and make the same five calls to their logger with the same information.
The careless logger writes free-text lines with the computer's local time, the session cookie value and the whole request body.
The careful logger writes one JSON object per line in the schema from the asset, with a UTC time, a correlation identifier and three hash-chain fields, and never reads the body or the cookie.
It is eight files that import only node:assert/strict, node:crypto, node:http and node:test and call the global fetch() function, and it installs no packages. The portal listens on 127.0.0.1 and asks for port 0, and the Node.js documentation says that when the port is omitted or is 0, the operating system will assign an arbitrary unused port.
The log lives in memory and nothing is written to disk. Passwords are labels, stored as salted scrypt hashes, the card number is zeros with a counter, and usernames end in .example.
One story of requests is sent to both versions: an allowed read, five failed sign-ins that end in a lock, three role changes by the admin, a refused role change, four refused invoice requests, two awkward record ids and one request with no session.
Each version wrote nineteen entries. From the careful log the detector read all nineteen and raised five alerts: one for the failed sign-ins, three for the role changes, of which the admin's grant to their own account was urgent, and one for the refused invoice requests. From the careless log it read none of twenty-one lines and raised nothing.
The scan found every fixture value in the careless log and none in the careful one. The integrity check had nothing to verify in the careless log. In the careful log it caught all four kinds of interference once the kept head was used, two of them by the chain alone.
The lab was written and run on Node.js 26.0.0 on macOS, the only version and system it was run on, and every output in the asset comes from that run. The story's clock is set by the script, so its times are the same on every run.
Correlation identifiers, session values, hashes, ports and test timings will differ on your computer. The careless log's local times shown in the asset are from the recorded run of run-lab.mjs, which was made with the TZ environment variable set to Australia/Melbourne.
What logging cannot tell you
A log is the application's own account of what it believed at the time, and it has edges.
- It holds only what was instrumented. CWE-778's definition has two halves, an event not recorded and an event recorded without important details. The lab writes no event for an allowed read or a completed payment, so its log cannot say who looked at an invoice they were entitled to see. ASVS 16.3.2 asks for that at Level 3, as the logging of all authorisation decisions.
- It records the identity the application saw. A request made with a stolen session cookie is logged under the victim's account id, and the cheat sheet lists an attacker causing the wrong identity to be logged, in order to conceal the responsible party, among the attacks on accountability.
- Silence is not a clean result. No alert can mean no attack, an attack below every threshold, a rule that reads a field nobody writes any more, or a log that stopped. The count of skipped lines and a scheduled test event are how you tell these apart.
- It does not prevent anything. The refused requests in the lab were refused by the access check, not by the log. A09's own description is about detection and response.
- An integrity check says that something changed, not who changed it.
- It starts when you turn it on. Nothing reconstructs the events from before the schema existed.
What this guide leaves to other guides
Several guides in this library take the questions next to this one:
- Sensitive data exposure: which personal details belong in a log line, a response, an error, an export or a cache, field by field.
- Credential stuffing: telling password guessing, spraying and stuffing apart in a login log, and the controls that slow them without locking real customers out.
- Broken access control and IDOR: the check that produces a denial in the first place, and what a 404 or a 403 gives away.
- Security misconfiguration: settings that leave a debug page, a status endpoint or a log reachable from the internet.
- Methodology: how a Clever Ops web application test runs from scope to retest, including the record kept of each request sent.
- Secrets exposure: what to do when a key or token is already in a log, starting with revoking or rotating it.
- Hacked website: the first hour of a suspected compromise, including which logs to copy before anything changes.
Where an outside security test fits
Clever Ops does not monitor applications and does not provide incident response.
If an alert tells you someone is inside your application now, an incident response specialist comes first. What a test offers sits before that day.
A Clever Ops web application and API penetration test sends its requests from the source addresses listed in the signed authorisation letter, during an agreed testing window, and each request is logged, so a question about what was sent, and when, can be answered from the record. That gives you something to hold your own logging against.
OWASP counts penetration testing that does not trigger alerts among A09's signs, and the Logging Cheat Sheet says events from pen testers should never be excluded from the log, though you may want to include a classification flag for them.
Tell whoever watches your alerts the window and the addresses beforehand. Afterwards, compare what your log recorded and which alerts fired with what the test sent. The comparison is yours to make, because a test run from outside can describe only what the tester could observe.
Destructive actions are off by default for real people's accounts and records that existed before the test, and reset, lockout and deletion checks run only on test accounts and test records where the rules of engagement allow them. Lockout and rate-limit checks use a small, agreed number of requests, and Clever Ops does not run load or denial-of-service testing.
A report records what was tested and what was found. It is evidence for your decisions, not a certificate, and it is not monitoring: an alert rule broken by a release the week after the test is outside what it can speak to.
If Clever Ops built or hosts your application, the work is a security review rather than an independent test, and the report says who built it and who hosts it.
Frequently asked questions
Security logging is an application recording security-relevant events, such as sign-ins, refusals and permission changes. Monitoring is reading those records while they are fresh, and alerting is telling a person. OWASP's Top 10:2025 covers failures of all three under A09, whose description says that without logging and monitoring, attacks and breaches cannot be detected, and without alerting it is very difficult to respond quickly and effectively during a security incident.
ASVS 5.0 section V16.3 asks, at Level 2, for all authentication operations, failed authorisation attempts, the security events defined in your documentation together with attempts to bypass security controls, and unexpected errors and security control failures. OWASP's Logging Cheat Sheet adds a longer list, including input validation failures, session management failures and use of higher-risk functionality such as changes to privileges. This guide builds its schema around three families: sign-in, privilege change and object denial.
ASVS 5.0 requirement 16.2.1 asks for necessary metadata, such as when, where, who and what, that would allow a detailed investigation of the timeline. In this guide's schema that is a UTC timestamp, the application and route, an event name from a fixed list, the outcome, an account identifier for the actor, the type and id of the object, the source address, a correlation identifier shared by every event of the request, a reason from a fixed list and the schema version.
ASVS 5.0 requirement 16.2.5 gives credentials and payment details as examples of data it may not be allowed to log, and session tokens as data that may only be logged hashed or masked. OWASP's Logging Cheat Sheet lists data that should usually not be recorded directly in the logs, including authentication passwords, access tokens, session identification values, encryption keys and payment card holder data. In the lab on this page, the careless logger puts passwords, a session cookie value and a card number into its log.
It is a caller supplying text, such as a line break followed by a made-up entry, that changes what the log appears to say. MITRE catalogues it as CWE-117. ASVS 5.0 requirement 16.4.1 asks that all logging components appropriately encode data to prevent it, and OWASP's Logging Cheat Sheet gives defences such as not using unstructured text log formats, using structured formats such as JSON, and limiting the size of user input used in a log message.
This guide gives no number. A09's prevention list, which opens by saying developers should implement some or all of its controls depending on the risk of the application, includes holding login, access control and server-side input validation failures for enough time to allow delayed forensic analysis. ASVS 5.0 requirement 16.1.1 asks that the logging inventory documents for how long logs are kept, and OWASP's Logging Cheat Sheet says legal, regulatory and contractual obligations may impact on retention periods. Decide the period for each kind of log, write down the reason, and check that the oldest record you can read matches it.
They work at different layers. The abstract of NIST SP 800-94 describes four classes of intrusion detection and prevention system: network-based, wireless, network behaviour analysis software and host-based. Detection inside an application is built on events the application emits, and OWASP's Logging Cheat Sheet says the application has the most information about the user and the context of an event. OWASP's AppSensor project uses the term application layer intrusion detection for that approach.
Play the behaviour and assert the alert. For each rule, write one test that sends the requests that should fire it and checks the alert's fields, and one near miss that has to stay quiet. Assert as well that the detector read every line, because a detector that reads nothing also raises nothing. Then send a marked test event through the real pipeline on a schedule, to confirm the alert reaches the person named to receive it.
Scope the right test for your application
Tell us what you have built, who uses it and when it needs to launch. You will get a plain-English recommendation: a website audit, a web application test or a referral to a specialist.