Skip to content

Commit 516864b

Browse files
committed
fix: keep undeclared columns out of the audit diff
1 parent 34bd1c5 commit 516864b

2 files changed

Lines changed: 27 additions & 42 deletions

File tree

‎index.ts‎

Lines changed: 25 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import type {
77
import dayjs from 'dayjs'
88
import utc from 'dayjs/plugin/utc.js';
99

10-
import { AdminForthPlugin, AllowedActionsEnum, AdminForthSortDirections, AdminForthDataTypes, HttpExtra, ActionCheckSource, Filters, afLogger, } from "adminforth";
10+
import { AdminForthPlugin, AllowedActionsEnum, AdminForthSortDirections, AdminForthDataTypes, HttpExtra, ActionCheckSource, Filters, afLogger, isBackendOnly, stripBackendOnly } from "adminforth";
1111
import { PluginOptions } from "./types.js";
1212

1313
dayjs.extend(utc);
@@ -99,12 +99,19 @@ export default class AuditLogPlugin extends AdminForthPlugin {
9999
const recordId = data?.[recordIdFieldName] || oldRecord?.[recordIdFieldName];
100100
const connector = this.adminforth.connectors[resource.dataSource];
101101

102-
const newRecord = action == AllowedActionsEnum.delete ? {} : (await connector.getRecordByPrimaryKey(resource, recordId)) || {};
103-
if (action !== AllowedActionsEnum.delete) {
104-
oldRecord = oldRecord ? JSON.parse(JSON.stringify(oldRecord)) : {};
105-
} else {
106-
oldRecord = data
107-
}
102+
const columnAccessCtx = {
103+
adminUser: user,
104+
resource,
105+
meta: { pk: recordId, requestBody: extra?.body },
106+
source: ActionCheckSource.ShowRequest,
107+
adminforth: this.adminforth,
108+
};
109+
110+
const rawNewRecord = action == AllowedActionsEnum.delete ? {} : (await connector.getRecordByPrimaryKey(resource, recordId)) || {};
111+
const rawOldRecord = action == AllowedActionsEnum.delete ? data : (oldRecord || {});
112+
// stripBackendOnly drops backendOnly values too, so the masking below compares the raw records
113+
const newRecord = await stripBackendOnly({ ...rawNewRecord }, columnAccessCtx);
114+
oldRecord = await stripBackendOnly({ ...rawOldRecord }, columnAccessCtx);
108115

109116
if (action !== AllowedActionsEnum.delete) {
110117
const columnsNamesList = resource.columns.map((c) => c.name);
@@ -116,39 +123,17 @@ export default class AuditLogPlugin extends AdminForthPlugin {
116123
});
117124
}
118125

119-
const checks = await Promise.all(
120-
resource.columns.map(async (c) => {
121-
if (typeof c.backendOnly === "function") {
122-
const result = await c.backendOnly({
123-
adminUser: user,
124-
resource,
125-
meta: {},
126-
source: ActionCheckSource.ShowRequest,
127-
adminforth: this.adminforth,
128-
});
129-
return { col: c, result };
130-
}
131-
return { col: c, result: c.backendOnly ?? false };
132-
})
133-
);
134-
135-
const backendOnlyColumns = checks
136-
.filter(({ result }) => result === true)
137-
.map(({ col }) => col);
138-
139-
backendOnlyColumns.forEach((c) => {
140-
if (JSON.stringify(oldRecord[c.name]) != JSON.stringify(newRecord[c.name])) {
141-
if (action !== AllowedActionsEnum.delete) {
142-
newRecord[c.name] = '<hidden value after>'
143-
}
144-
if (action !== AllowedActionsEnum.create) {
145-
oldRecord[c.name] = '<hidden value before>'
146-
}
147-
} else {
148-
delete oldRecord[c.name];
149-
delete newRecord[c.name];
150-
}
151-
});
126+
for (const c of resource.columns) {
127+
if (!(await isBackendOnly(c, columnAccessCtx)) || JSON.stringify(rawOldRecord[c.name]) == JSON.stringify(rawNewRecord[c.name])) {
128+
continue;
129+
}
130+
if (action !== AllowedActionsEnum.delete) {
131+
newRecord[c.name] = '<hidden value after>'
132+
}
133+
if (action !== AllowedActionsEnum.create) {
134+
oldRecord[c.name] = '<hidden value before>'
135+
}
136+
}
152137

153138
const { country, clientIp } = await this.getIpAndCountry(extra?.headers || {});
154139

‎package.json‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
"build": "tsc && rsync -av --exclude 'node_modules' custom dist/"
3535
},
3636
"peerDependencies": {
37-
"adminforth": "^3.7.1"
37+
"adminforth": "^3.18.0"
3838
},
3939
"release": {
4040
"plugins": [
@@ -63,7 +63,7 @@
6363
},
6464
"devDependencies": {
6565
"@types/node": "^22.10.7",
66-
"adminforth": "^3.7.1",
66+
"adminforth": "^3.18.0",
6767
"semantic-release": "^24.2.1",
6868
"semantic-release-slack-bot": "^4.0.2",
6969
"typescript": "^5.7.3"

0 commit comments

Comments
 (0)