HSCP-R-002 · recipe · Security
Write the log entry before the read, in the same transaction
Written entry, version 0.2, checked on 2026-11-15.
At a glance
| Implements | HSCP-G-002 Log access to health data |
|---|---|
| Case | Instantiates: applies an existing practice to a context. |
| Context | software used during care (patient record, teleconsultation, care coordination) whose data are read through several paths |
| Verification | open test · review |
Implements HSCP-G-002, Log access to health data. Case: instantiates a generic practice for the health context.
When
The product logs changes and lets reads through, or logs in the user interface and forgets the other paths: API called by a third-party tool, MCP server, agent, overnight batch, administration account. A read made without reason changes nothing: without a read entry, it leaves no trace.
Six paths lead to the data. All go through the same layer, except direct database access:
How
- Route every read through a single server-side function, the only path to the table being read.
- Require an access context at the entry of this function: the person accountable for the access, the path taken, the basis of the access. Without a context, the read is refused, including for internal processing.
- In a single transaction: check the right, write the log entry, read the data, commit. If the log refuses the entry, the data is not returned.
- Have the database itself enforce the two rules on the basis: an access on the basis of care designates which care episode (appointment, stay, care team); any other access carries a written reason.
- Give the application account the right to add entries to the log, without the right to read, modify or delete them. Restrict reading to the people in charge of the review.
- Set the retention period of the log and implement it.
- Run the review queries at a fixed interval (accesses outside a care episode, accesses where the user bears the patient's name). A designated person reads the result and signs it.
A read, step by step:
Variants:
- Refused attempts: log them in a separate transaction, since the failure of the read rolls back the main transaction and its entry.
- Export: write the entry at the time of export, as a file taken out of the database no longer leaves any trace afterwards.
- Direct database access (administration, maintenance): named accounts, database engine log for these accounts, access opened for a set duration and reason.
Data model
One entry per access. The fields answer four questions.
| Question | Field | Content |
|---|---|---|
| Who | acteur_id |
the person accountable for the access |
| Who | chemin |
user interface, third-party API, MCP, agent, administration, service provider |
| Who | agent_id |
identity of the agent or MCP client, see HSCP-R-003 |
| What | patient_id |
the record concerned |
| What | ressource, ressource_id |
report, result, prescription |
| What | action |
read, list, export, create, modify, delete |
| When | horodatage |
set by the database |
| On what basis | titre |
care episode, break-glass, support |
| On what basis | prise_en_charge_id |
the appointment or the stay |
| On what basis | motif |
written by the user |
| Grouping | requete_id |
shared by the reads of a same request |
Examples (PostgreSQL 16)
The two rules, enforced by the database:
CONSTRAINT titre_justifie CHECK (
(titre = 'prise_en_charge' AND prise_en_charge_id IS NOT NULL)
OR (titre <> 'prise_en_charge' AND motif IS NOT NULL AND length(trim(motif)) >= 10)
),
CONSTRAINT agent_nomme CHECK (
chemin NOT IN ('agent', 'mcp') OR agent_id IS NOT NULL
)
The log as append-only:
CREATE FUNCTION acces_journal_fige() RETURNS trigger
LANGUAGE plpgsql AS $$
BEGIN
RAISE EXCEPTION 'acces_journal : ajout seul, % refusé', TG_OP;
END $$;
CREATE TRIGGER acces_journal_ajout_seul
BEFORE UPDATE OR DELETE OR TRUNCATE ON acces_journal
FOR EACH STATEMENT EXECUTE FUNCTION acces_journal_fige();
REVOKE ALL ON acces_journal FROM PUBLIC;
GRANT INSERT ON acces_journal TO appli;
GRANT SELECT ON acces_journal, utilisateur, patient TO revue;
A review query, accesses outside a care episode over the last seven days:
SELECT j.horodatage::date AS jour, u.nom AS acteur, j.chemin,
p.nom AS patient, j.ressource, j.titre, j.motif
FROM acces_journal j
JOIN utilisateur u ON u.id = j.acteur_id
JOIN patient p ON p.id = j.patient_id
WHERE j.horodatage >= now() - interval '7 days'
AND j.titre <> 'prise_en_charge'
ORDER BY j.horodatage;
Writing before reading, in Go (excerpt from code that compiles, not run against the database):
tx, err := d.db.BeginTx(ctx, nil)
if err != nil {
return "", err
}
defer tx.Rollback()
_, err = tx.ExecContext(ctx, `
INSERT INTO acces_journal
(acteur_id, chemin, agent_id, requete_id, patient_id,
ressource, ressource_id, action, titre, prise_en_charge_id, motif)
VALUES ($1, $2, $3, $4, $5, 'compte_rendu', $6, 'lire', $7, $8, $9)`,
a.ActeurID, a.Chemin, vide(a.AgentID), a.RequeteID, patientID,
id, a.Titre, vide(a.PriseEnChargeID), vide(a.Motif))
if err != nil {
return "", fmt.Errorf("journal des accès : %w", err) // entry refused: nothing is read
}
Verification
- CI test: a read without an access context is refused; an access outside a care episode without a reason is refused by the database; after a successful read, the entry exists with all its fields.
- Permissions test: the application account can neither read, modify nor delete an entry of the log.
- Pitfall to test: a database constraint whose expression evaluates to NULL is accepted. The reason rule therefore first checks that the reason is filled in, and the test covers the missing reason.
- Audit agent: identify in the code any query that reads a health data table outside the single function.
Evidence produced
Dated test report, schema of the log table with its constraints, list of the accounts' permissions on the log, signed review results.
Evidence items
Each item attests the recipe at a given date. Frequency given for a product in service.
- Test report Dated test report: read without context refused, missing reason refused, row written after a read
- Configuration or schema Schema of the log table with its constraints
- Survey List of account privileges on the log
- Written record Signed results of the log review
Own mappings
In addition to those of the practice:
| Identifier | What the recipe contributes |
|---|---|
pgssi_s_imputabilite_v1_0/6.2.2-4 |
one entry per access makes it possible to reconstruct the audit trail by person, by action, by date and by patient |
pgssi_s_imputabilite_v1_0/6.2.1.4-1 |
logs user activity, reads included |
ehds_2025_327/annexeII-p13 |
records at each access the person, the data category and the date, and provides review queries (published text, phased application) |
reglement_exec_2024_2690/annexe1-p35 |
covers access to applications and accesses by administration accounts (text not applicable in France at the date of the entry) |
Versions
| Version | Status | Date |
|---|---|---|
| 0.2 | Draft | 2026-11-15 |
| 0.1 | Draft | 2026-11-15 |
How to cite: “Health Software Common Practices”, HSCP-R-002, version 0.2, https://healthtechpractices.org/en/recettes/hscp-r-002/, under the CC BY-NC-SA 4.0 licence. Licence