Skip to content

Commit fcea422

Browse files
committed
fix: add iinterpret resource before publishing updates to prevent unauthorized access to resource
1 parent f507bdc commit fcea422

1 file changed

Lines changed: 30 additions & 8 deletions

File tree

adminforth/dataConnectors/baseConnector.ts

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@ import {
33
AdminForthResourceColumn,
44
IAdminForthSort, IAdminForthSingleFilter, IAdminForthAndOrFilter,
55
AdminForthConfig,
6-
IAggregationRule, IGroupByRule, IGroupByDateTrunc,
6+
IAggregationRule, IGroupByRule, IGroupByDateTrunc
77
} from "../types/Back.js";
88

9-
9+
import type { AdminUser } from "../types/Common.js"
1010

1111
import { suggestIfTypo } from "../modules/utils.js";
12-
import { AdminForthDataTypes, AdminForthFilterOperators, AdminForthSortDirections } from "../types/Common.js";
12+
import { interpretResource } from "../modules/restApi.js";
13+
import { ActionCheckSource, AdminForthDataTypes, AdminForthFilterOperators, AdminForthSortDirections, AllowedActionsEnum } from "../types/Common.js";
1314
import { randomUUID } from "crypto";
1415
import dayjs from "dayjs";
1516
import { afLogger } from '../modules/logger.js';
@@ -24,11 +25,32 @@ type AdminForthFilterNormalizationResult = {
2425
};
2526

2627
async function publishShowPageUpdate(resource: AdminForthResource, recordId: string, updates: Record<string, any>) {
27-
await global.adminforth.websocket.publish(`/showPage/${resource.resourceId}/${String(recordId)}`, {
28-
resourceId: resource.resourceId,
29-
recordId,
30-
updates,
31-
});
28+
await global.adminforth.websocket.publish(`/showPage/${resource.resourceId}/${String(recordId)}`,
29+
{
30+
resourceId: resource.resourceId,
31+
recordId,
32+
updates,
33+
},
34+
async (adminUser: AdminUser): Promise<boolean> => {
35+
if (!adminUser) {
36+
// anonymous clients should never receive record updates
37+
return false;
38+
}
39+
try {
40+
const { allowedActions } = await interpretResource(
41+
adminUser,
42+
resource,
43+
{ requestBody: null, pk: recordId },
44+
ActionCheckSource.ShowRequest,
45+
global.adminforth,
46+
);
47+
return allowedActions[AllowedActionsEnum.show] === true;
48+
} catch (e) {
49+
afLogger.error(`Error while checking show access for ${resource.resourceId} record ${recordId}, assuming update should not be sent: ${e}`);
50+
return false;
51+
}
52+
}
53+
);
3254
}
3355

3456

0 commit comments

Comments
 (0)