Skip to content

Infisical is the open-source platform for securing secrets, certificates, access, and AI Agents.

improvement: add audit log and handle forbid

Scott Wilson65ccb2578dbb
diff --git a/backend/src/ee/services/audit-log/audit-log-types.ts b/backend/src/ee/services/audit-log/audit-log-types.ts
index 4d9579f806..49629952e7 100644
--- a/backend/src/ee/services/audit-log/audit-log-types.ts
+++ b/backend/src/ee/services/audit-log/audit-log-types.ts
@@ -311,6 +311,7 @@ export enum EventType {
ADD_PROJECT_MEMBER = "add-project-member",
ADD_BATCH_PROJECT_MEMBER = "add-project-members",
REMOVE_PROJECT_MEMBER = "remove-project-member",
+ GET_PROJECT_MEMBER_PERMISSION_AUDIT = "get-project-member-permission-audit",
CREATE_FOLDER = "create-folder",
UPDATE_FOLDER = "update-folder",
DELETE_FOLDER = "delete-folder",
@@ -2340,6 +2341,14 @@ interface RemoveProjectMemberEvent {
};
}
+interface GetProjectMemberPermissionAuditEvent {
+ type: EventType.GET_PROJECT_MEMBER_PERMISSION_AUDIT;
+ metadata: {
+ targetUserId: string;
+ membershipId: string;
+ };
+}
+
interface CreateFolderEvent {
type: EventType.CREATE_FOLDER;
metadata: {
@@ -7144,6 +7153,7 @@ export type Event =
| AddProjectMemberEvent
| AddBatchProjectMemberEvent
| RemoveProjectMemberEvent
+ | GetProjectMemberPermissionAuditEvent
| CreateFolderEvent
| UpdateFolderEvent
| DeleteFolderEvent
diff --git a/backend/src/server/routes/v1/project-membership-router.ts b/backend/src/server/routes/v1/project-membership-router.ts
index d9d55c4a40..ff7773097a 100644
--- a/backend/src/server/routes/v1/project-membership-router.ts
+++ b/backend/src/server/routes/v1/project-membership-router.ts
@@ -198,6 +198,18 @@ export const registerProjectMembershipRouter = async (server: FastifyZodProvider
targetUserId: userId
});
+ await server.services.auditLog.createAuditLog({
+ projectId: req.params.projectId,
+ ...req.auditLogInfo,
+ event: {
+ type: EventType.GET_PROJECT_MEMBER_PERMISSION_AUDIT,
+ metadata: {
+ targetUserId: userId,
+ membershipId: req.params.membershipId
+ }
+ }
+ });
+
return { sources };
}
});
diff --git a/frontend/src/hooks/api/auditLogs/constants.tsx b/frontend/src/hooks/api/auditLogs/constants.tsx
index 93bdb7f68c..0fac5c4b80 100644
--- a/frontend/src/hooks/api/auditLogs/constants.tsx
+++ b/frontend/src/hooks/api/auditLogs/constants.tsx
@@ -51,6 +51,7 @@ export const eventToNameMap: { [K in EventType]: string } = {
[EventType.DELETE_ENVIRONMENT]: "Delete environment",
[EventType.ADD_PROJECT_MEMBER]: "Add member",
[EventType.REMOVE_PROJECT_MEMBER]: "Remove member",
+ [EventType.GET_PROJECT_MEMBER_PERMISSION_AUDIT]: "View member permission audit",
[EventType.CREATE_FOLDER]: "Create folder",
[EventType.UPDATE_FOLDER]: "Update folder",
[EventType.DELETE_FOLDER]: "Delete folder",
@@ -457,6 +458,7 @@ export const userAgentTypeToNameMap: { [K in UserAgentType]: string } = {
const sharedProjectEvents = [
EventType.ADD_PROJECT_MEMBER,
EventType.REMOVE_PROJECT_MEMBER,
+ EventType.GET_PROJECT_MEMBER_PERMISSION_AUDIT,
EventType.CREATE_PROJECT_ROLE,
EventType.UPDATE_PROJECT_ROLE,
EventType.DELETE_PROJECT_ROLE,
diff --git a/frontend/src/hooks/api/auditLogs/enums.tsx b/frontend/src/hooks/api/auditLogs/enums.tsx
index a1e348ac54..7aa3339a5a 100644
--- a/frontend/src/hooks/api/auditLogs/enums.tsx
+++ b/frontend/src/hooks/api/auditLogs/enums.tsx
@@ -66,6 +66,7 @@ export enum EventType {
DELETE_ENVIRONMENT = "delete-environment",
ADD_PROJECT_MEMBER = "add-project-member",
REMOVE_PROJECT_MEMBER = "remove-project-member",
+ GET_PROJECT_MEMBER_PERMISSION_AUDIT = "get-project-member-permission-audit",
CREATE_FOLDER = "create-folder",
UPDATE_FOLDER = "update-folder",
DELETE_FOLDER = "delete-folder",
diff --git a/frontend/src/hooks/api/auditLogs/types.tsx b/frontend/src/hooks/api/auditLogs/types.tsx
index 3925a6433d..841ad1a02e 100644
--- a/frontend/src/hooks/api/auditLogs/types.tsx
+++ b/frontend/src/hooks/api/auditLogs/types.tsx
@@ -419,6 +419,14 @@ interface RemoveWorkspaceMemberEvent {
};
}
+interface GetProjectMemberPermissionAuditEvent {
+ type: EventType.GET_PROJECT_MEMBER_PERMISSION_AUDIT;
+ metadata: {
+ targetUserId: string;
+ membershipId: string;
+ };
+}
+
interface CreateFolderEvent {
type: EventType.CREATE_FOLDER;
metadata: {
@@ -957,6 +965,7 @@ export type Event =
| DeleteEnvironmentEvent
| AddWorkspaceMemberEvent
| RemoveWorkspaceMemberEvent
+ | GetProjectMemberPermissionAuditEvent
| CreateFolderEvent
| UpdateFolderEvent
| DeleteFolderEvent
diff --git a/frontend/src/pages/project/MemberDetailsByIDPage/components/MemberPermissionAuditSheet/MemberPermissionAuditSheet.tsx b/frontend/src/pages/project/MemberDetailsByIDPage/components/MemberPermissionAuditSheet/MemberPermissionAuditSheet.tsx
index 4a64d40001..a3119f1113 100644
--- a/frontend/src/pages/project/MemberDetailsByIDPage/components/MemberPermissionAuditSheet/MemberPermissionAuditSheet.tsx
+++ b/frontend/src/pages/project/MemberDetailsByIDPage/components/MemberPermissionAuditSheet/MemberPermissionAuditSheet.tsx
@@ -50,13 +50,18 @@ const STATE_FILTERS: { id: StateFilter; label: string }[] = [
{ id: "all", label: "All" },
{ id: "allow", label: "Allowed" },
{ id: "conditional", label: "Conditional" },
- { id: "deny", label: "Denied" }
+ { id: "forbid", label: "Forbidden" }
];
const actionMatchesSearch = (action: ResourceAudit["actions"][number], term: string): boolean => {
if (!term) return true;
const needle = term.toLowerCase();
- const haystack = [action.label, action.description ?? "", ...action.grantedBy.map((s) => s.name)]
+ const haystack = [
+ action.label,
+ action.description ?? "",
+ ...action.grantedBy.map((s) => s.name),
+ ...action.forbiddenBy.map((s) => s.name)
+ ]
.join(" ")
.toLowerCase();
return haystack.includes(needle);
@@ -66,13 +71,13 @@ const countMatching = (resources: ResourceAudit[], state: AuditState): number =>
resources.reduce((sum, r) => sum + r.actions.filter((a) => a.state === state).length, 0);
const getCount = (
- counts: { all: number; allow: number; conditional: number; deny: number },
+ counts: { all: number; allow: number; conditional: number; forbid: number },
id: StateFilter
): number => {
if (id === "all") return counts.all;
if (id === "allow") return counts.allow;
if (id === "conditional") return counts.conditional;
- return counts.deny;
+ return counts.forbid;
};
export const MemberPermissionAuditSheet = ({
@@ -114,7 +119,7 @@ export const MemberPermissionAuditSheet = ({
all: resources.reduce((sum, r) => sum + r.actions.length, 0),
allow: countMatching(resources, "allow"),
conditional: countMatching(resources, "conditional"),
- deny: countMatching(resources, "deny")
+ forbid: countMatching(resources, "forbid")
}),
[resources]
);
@@ -213,9 +218,9 @@ export const MemberPermissionAuditSheet = ({
<SplitIcon className="size-3 text-warning" /> Conditional
</span>
<span className="flex items-center gap-1">
- <BanIcon className="size-3 text-danger" /> Deny
+ <BanIcon className="size-3 text-danger" /> Forbid
</span>
- <span className="flex items-center gap-1 text-muted">— N/A</span>
+ <span className="flex items-center gap-1 text-muted">— No Grant (Forbid)</span>
</div>
</div>
diff --git a/frontend/src/pages/project/MemberDetailsByIDPage/components/MemberPermissionAuditSheet/PermissionAuditRow.tsx b/frontend/src/pages/project/MemberDetailsByIDPage/components/MemberPermissionAuditSheet/PermissionAuditRow.tsx
index fb400eafbc..0539694d86 100644
--- a/frontend/src/pages/project/MemberDetailsByIDPage/components/MemberPermissionAuditSheet/PermissionAuditRow.tsx
+++ b/frontend/src/pages/project/MemberDetailsByIDPage/components/MemberPermissionAuditSheet/PermissionAuditRow.tsx
@@ -16,7 +16,10 @@ type Props = {
};
export const PermissionAuditRow = ({ audit }: Props) => {
- const [primarySource, ...extraSources] = audit.grantedBy;
+ const isForbidden = audit.grantedBy.length === 0 && audit.forbiddenBy.length > 0;
+ const displaySources = audit.grantedBy.length > 0 ? audit.grantedBy : audit.forbiddenBy;
+ const [primarySource, ...extraSources] = displaySources;
+ const overflowLabel = isForbidden ? "Also forbidden by" : "Also granted by";
return (
<TableRow className="h-12">
@@ -29,14 +32,14 @@ export const PermissionAuditRow = ({ audit }: Props) => {
</div>
</TableCell>
<TableCell>
- <StatePill state={audit.state} conditions={audit.conditions} />
+ <StatePill state={audit.state} conditions={audit.conditions} isForbidden={isForbidden} />
</TableCell>
<TableCell>
{!primarySource ? (
<span className="text-muted">—</span>
) : (
<div className="flex items-center gap-1">
- <SourcePill source={primarySource} />
+ <SourcePill source={primarySource} forbidding={isForbidden} />
{extraSources.length > 0 ? (
<HoverCard openDelay={150}>
<HoverCardTrigger asChild>
@@ -50,11 +53,11 @@ export const PermissionAuditRow = ({ audit }: Props) => {
</HoverCardTrigger>
<HoverCardContent align="end" className="w-auto min-w-56 p-2">
<div className="mb-2 text-xs font-semibold text-mineshaft-100">
- Also granted by
+ {overflowLabel}
</div>
<div className="flex flex-col items-start gap-1">
{extraSources.map((source) => (
- <SourcePill key={source.id} source={source} />
+ <SourcePill key={source.id} source={source} forbidding={isForbidden} />
))}
</div>
</HoverCardContent>
diff --git a/frontend/src/pages/project/MemberDetailsByIDPage/components/MemberPermissionAuditSheet/PermissionAuditSection.tsx b/frontend/src/pages/project/MemberDetailsByIDPage/components/MemberPermissionAuditSheet/PermissionAuditSection.tsx
index e21fa1dd33..73e6ca0158 100644
--- a/frontend/src/pages/project/MemberDetailsByIDPage/components/MemberPermissionAuditSheet/PermissionAuditSection.tsx
+++ b/frontend/src/pages/project/MemberDetailsByIDPage/components/MemberPermissionAuditSheet/PermissionAuditSection.tsx
@@ -23,7 +23,12 @@ type Props = {
const matchesSearch = (action: ActionAudit, term: string): boolean => {
if (!term) return true;
const needle = term.toLowerCase();
- const haystack = [action.label, action.description ?? "", ...action.grantedBy.map((s) => s.name)]
+ const haystack = [
+ action.label,
+ action.description ?? "",
+ ...action.grantedBy.map((s) => s.name),
+ ...action.forbiddenBy.map((s) => s.name)
+ ]
.join(" ")
.toLowerCase();
return haystack.includes(needle);
diff --git a/frontend/src/pages/project/MemberDetailsByIDPage/components/MemberPermissionAuditSheet/SourcePill.tsx b/frontend/src/pages/project/MemberDetailsByIDPage/components/MemberPermissionAuditSheet/SourcePill.tsx
index f5d3a0a5ef..b5f9d6f882 100644
--- a/frontend/src/pages/project/MemberDetailsByIDPage/components/MemberPermissionAuditSheet/SourcePill.tsx
+++ b/frontend/src/pages/project/MemberDetailsByIDPage/components/MemberPermissionAuditSheet/SourcePill.tsx
@@ -25,20 +25,22 @@ const displayName = (source: SourceRef): string => {
type Props = {
source: SourceRef;
+ forbidding?: boolean;
};
-export const SourcePill = ({ source }: Props) => {
+export const SourcePill = ({ source, forbidding = false }: Props) => {
const Icon = TYPE_ICON[source.type];
const tooltipParts: string[] = [TYPE_LABEL[source.type]];
if (source.groupName) tooltipParts.push(`via group "${source.groupName}"`);
if (source.isTemporary && source.temporaryAccessEndTime) {
tooltipParts.push(`Expires ${new Date(source.temporaryAccessEndTime).toLocaleString()}`);
}
+ if (forbidding) tooltipParts.unshift("Forbids this action");
return (
<Tooltip>
<TooltipTrigger asChild>
- <Badge variant="neutral">
+ <Badge variant={forbidding ? "danger" : "neutral"}>
<Icon />
<span>{displayName(source)}</span>
{source.isTemporary ? <ClockIcon /> : null}
diff --git a/frontend/src/pages/project/MemberDetailsByIDPage/components/MemberPermissionAuditSheet/StatePill.tsx b/frontend/src/pages/project/MemberDetailsByIDPage/components/MemberPermissionAuditSheet/StatePill.tsx
index d032c97be5..ede7a29342 100644
--- a/frontend/src/pages/project/MemberDetailsByIDPage/components/MemberPermissionAuditSheet/StatePill.tsx
+++ b/frontend/src/pages/project/MemberDetailsByIDPage/components/MemberPermissionAuditSheet/StatePill.tsx
@@ -1,6 +1,7 @@
import { BanIcon, CheckIcon, SplitIcon } from "lucide-react";
import {
+ Badge,
HoverCard,
HoverCardContent,
HoverCardTrigger,
@@ -12,21 +13,24 @@ import {
TableRow
} from "@app/components/v3";
-import { AuditState } from "./permission-audit.types";
+import { AuditCondition, AuditState } from "./permission-audit.types";
import { formatConditionEntries } from "./permission-audit.utils";
type Props = {
state: AuditState;
- conditions?: Record<string, unknown>[];
+ conditions?: AuditCondition[];
+ // When true, the forbid state was produced by an explicit inverted rule (red).
+ // When false, forbid means no rule grants the action (muted N/A).
+ isForbidden?: boolean;
};
const STATE_LABEL: Record<AuditState, string> = {
allow: "Allowed",
conditional: "Conditional",
- deny: "Denied"
+ forbid: "Forbidden"
};
-export const StatePill = ({ state, conditions }: Props) => {
+export const StatePill = ({ state, conditions, isForbidden }: Props) => {
if (state === "allow") {
return <CheckIcon className="size-4 text-success" aria-label={STATE_LABEL[state]} />;
}
@@ -49,6 +53,7 @@ export const StatePill = ({ state, conditions }: Props) => {
<Table>
<TableHeader>
<TableRow>
+ <TableHead className="w-20">Effect</TableHead>
<TableHead className="w-32">Field</TableHead>
<TableHead className="w-20">Operator</TableHead>
<TableHead>Value</TableHead>
@@ -56,7 +61,13 @@ export const StatePill = ({ state, conditions }: Props) => {
</TableHeader>
<TableBody>
{entries.map((entry) => (
- <TableRow key={`${entry.field}-${entry.operator}-${entry.value}`}>
+ <TableRow key={`${entry.kind}-${entry.field}-${entry.operator}-${entry.value}`}>
+ <TableCell>
+ <Badge variant={entry.kind === "allow" ? "success" : "danger"}>
+ {entry.kind === "allow" ? <CheckIcon /> : <BanIcon />}
+ <span>{entry.kind === "allow" ? "Allow" : "Forbid"}</span>
+ </Badge>
+ </TableCell>
<TableCell className="font-mono text-xs">{entry.field}</TableCell>
<TableCell className="font-mono text-xs text-mineshaft-300">
{entry.operator}
@@ -70,5 +81,12 @@ export const StatePill = ({ state, conditions }: Props) => {
</HoverCard>
);
}
- return <BanIcon className="size-4 text-danger" aria-label={STATE_LABEL[state]} />;
+ if (isForbidden) {
+ return <BanIcon className="size-4 text-danger" aria-label={STATE_LABEL[state]} />;
+ }
+ return (
+ <span className="text-xs text-muted" aria-label="No access">
+ —
+ </span>
+ );
};
diff --git a/frontend/src/pages/project/MemberDetailsByIDPage/components/MemberPermissionAuditSheet/permission-audit-export.ts b/frontend/src/pages/project/MemberDetailsByIDPage/components/MemberPermissionAuditSheet/permission-audit-export.ts
index fe1b46d7ec..d4076c680b 100644
--- a/frontend/src/pages/project/MemberDetailsByIDPage/components/MemberPermissionAuditSheet/permission-audit-export.ts
+++ b/frontend/src/pages/project/MemberDetailsByIDPage/components/MemberPermissionAuditSheet/permission-audit-export.ts
@@ -8,6 +8,7 @@ const HEADER: CsvRow = [
"Action",
"Action Description",
"Access",
+ "Source Role",
"Source Name",
"Source Type",
"Group Name",
@@ -19,7 +20,7 @@ const HEADER: CsvRow = [
const ACCESS_LABEL = {
allow: "Allowed",
conditional: "Conditional",
- deny: "Denied"
+ forbid: "Forbidden"
} as const;
const SOURCE_TYPE_LABEL = {
@@ -43,18 +44,23 @@ const toCsv = (rows: CsvRow[]): string =>
const conditionsToString = (audit: ActionAudit): string =>
audit.conditions
.flatMap((c) => formatConditionEntries(c))
- .map((e) => (e.value ? `${e.field} ${e.operator} ${e.value}` : e.field))
+ .map((e) => {
+ const base = e.value ? `${e.field} ${e.operator} ${e.value}` : e.field;
+ return e.kind === "forbid" ? `FORBID ${base}` : base;
+ })
.join("; ");
const rowForSource = (
resource: ResourceAudit,
audit: ActionAudit,
- source: SourceRef | undefined
+ source: SourceRef | undefined,
+ sourceRole: "Grants" | "Forbids" | ""
): CsvRow => [
resource.label,
audit.label,
audit.description ?? "",
ACCESS_LABEL[audit.state],
+ sourceRole,
source?.name ?? "",
source ? SOURCE_TYPE_LABEL[source.type] : "",
source?.groupName ?? "",
@@ -68,12 +74,15 @@ export const buildAuditCsv = (resources: ResourceAudit[]): string => {
resources.forEach((resource) => {
resource.actions.forEach((audit) => {
- if (audit.grantedBy.length === 0) {
- rows.push(rowForSource(resource, audit, undefined));
+ if (audit.grantedBy.length === 0 && audit.forbiddenBy.length === 0) {
+ rows.push(rowForSource(resource, audit, undefined, ""));
return;
}
audit.grantedBy.forEach((source) => {
- rows.push(rowForSource(resource, audit, source));
+ rows.push(rowForSource(resource, audit, source, "Grants"));
+ });
+ audit.forbiddenBy.forEach((source) => {
+ rows.push(rowForSource(resource, audit, source, "Forbids"));
});
});
});
diff --git a/frontend/src/pages/project/MemberDetailsByIDPage/components/MemberPermissionAuditSheet/permission-audit.types.ts b/frontend/src/pages/project/MemberDetailsByIDPage/components/MemberPermissionAuditSheet/permission-audit.types.ts
index 902a8e2740..d55fd8f64b 100644
--- a/frontend/src/pages/project/MemberDetailsByIDPage/components/MemberPermissionAuditSheet/permission-audit.types.ts
+++ b/frontend/src/pages/project/MemberDetailsByIDPage/components/MemberPermissionAuditSheet/permission-audit.types.ts
@@ -1,7 +1,7 @@
import { ProjectPermissionSub } from "@app/context/ProjectPermissionContext";
import { TPermissionAuditSource, TPermissionAuditSourceType } from "@app/hooks/api/projects/types";
-export type AuditState = "allow" | "conditional" | "deny";
+export type AuditState = "allow" | "conditional" | "forbid";
export type ActionDescriptor = {
action: string;
@@ -27,6 +27,11 @@ export type SourceRef = {
temporaryAccessEndTime?: string;
};
+export type AuditCondition = {
+ kind: "allow" | "forbid";
+ conditions: Record<string, unknown>;
+};
+
export type ActionAudit = {
action: string;
label: string;
@@ -34,7 +39,10 @@ export type ActionAudit = {
isLegacy?: boolean;
state: AuditState;
grantedBy: SourceRef[];
- conditions: Record<string, unknown>[];
+ // Sources whose unconditional inverted rules cause an explicit forbid.
+ // Distinguishes "no rule grants this" (N/A, muted) from "a role forbids this" (red).
+ forbiddenBy: SourceRef[];
+ conditions: AuditCondition[];
};
export type ResourceAudit = {
diff --git a/frontend/src/pages/project/MemberDetailsByIDPage/components/MemberPermissionAuditSheet/permission-audit.utils.ts b/frontend/src/pages/project/MemberDetailsByIDPage/components/MemberPermissionAuditSheet/permission-audit.utils.ts
index 2aeebfeac5..2e0a48c642 100644
--- a/frontend/src/pages/project/MemberDetailsByIDPage/components/MemberPermissionAuditSheet/permission-audit.utils.ts
+++ b/frontend/src/pages/project/MemberDetailsByIDPage/components/MemberPermissionAuditSheet/permission-audit.utils.ts
@@ -5,6 +5,7 @@ import { TPermissionAuditSource } from "@app/hooks/api/projects/types";
import {
ActionAudit,
+ AuditCondition,
AuditState,
ResolvedSource,
ResourceAudit,
@@ -54,74 +55,86 @@ const evaluateActionForSubject = (
isLegacy: boolean,
sources: ResolvedSource[]
): ActionAudit => {
- const grantedBy: SourceRef[] = [];
- const conditions: Record<string, unknown>[] = [];
- let hasUnconditionalAllow = false;
- let hasConditionalAllow = false;
-
- sources.forEach((source) => {
- const matchingRules = source.rules.filter((rule) => ruleMatches(rule, action, subject));
- const hasConditions = (r: { conditions?: Record<string, unknown> }) =>
- Boolean(r.conditions && Object.keys(r.conditions).length > 0);
-
- const unconditionalAllowRule = matchingRules.find((r) => !r.inverted && !hasConditions(r));
- const conditionalAllowRules = matchingRules.filter((r) => !r.inverted && hasConditions(r));
- const unconditionalDenyRule = matchingRules.find((r) => r.inverted && !hasConditions(r));
- const conditionalDenyRules = matchingRules.filter((r) => r.inverted && hasConditions(r));
-
- // CASL applies inverted (forbid) rules after allow rules to subtract from
- // them. An unconditional forbid fully revokes any grant from this source.
- if (unconditionalDenyRule) return;
-
- let sourceState: "unconditional" | "conditional" | "none";
- if (unconditionalAllowRule) {
- sourceState = conditionalDenyRules.length > 0 ? "conditional" : "unconditional";
- } else if (conditionalAllowRules.length > 0) {
- sourceState = "conditional";
- } else {
- sourceState = "none";
- }
+ const hasConditions = (r: { conditions?: Record<string, unknown> }) =>
+ Boolean(r.conditions && Object.keys(r.conditions).length > 0);
+
+ // CASL combines rules across ALL sources into a single ability and applies
+ // inverted (forbid) rules to subtract from allows, regardless of which source
+ // declared them. So a forbid in one role overrides an allow in another.
+ type SourcedRule = ResolvedSource["rules"][number] & { source: ResolvedSource };
+ const matchingRules: SourcedRule[] = sources.flatMap((source) =>
+ source.rules
+ .filter((rule) => ruleMatches(rule, action, subject))
+ .map((rule) => ({ ...rule, source }))
+ );
- if (sourceState === "none") return;
+ const unconditionalAllowRules = matchingRules.filter((r) => !r.inverted && !hasConditions(r));
+ const conditionalAllowRules = matchingRules.filter((r) => !r.inverted && hasConditions(r));
+ const unconditionalDenyRules = matchingRules.filter((r) => r.inverted && !hasConditions(r));
+ const conditionalDenyRules = matchingRules.filter((r) => r.inverted && hasConditions(r));
+
+ const toSourceRef = (source: ResolvedSource): SourceRef => ({
+ id: source.id,
+ type: source.type,
+ name: source.name,
+ slug: source.slug,
+ groupName: source.groupName,
+ isTemporary: source.isTemporary,
+ temporaryAccessEndTime: source.temporaryAccessEndTime
+ });
- conditionalAllowRules.forEach((r) => {
- if (r.conditions) conditions.push(r.conditions);
- });
- conditionalDenyRules.forEach((r) => {
- if (r.conditions) conditions.push(r.conditions);
+ const dedupeSources = (rules: SourcedRule[]): SourceRef[] => {
+ const seen = new Set<string>();
+ const refs: SourceRef[] = [];
+ rules.forEach((r) => {
+ if (seen.has(r.source.id)) return;
+ seen.add(r.source.id);
+ refs.push(toSourceRef(r.source));
});
+ return refs;
+ };
- grantedBy.push({
- id: source.id,
- type: source.type,
- name: source.name,
- slug: source.slug,
- groupName: source.groupName,
- isTemporary: source.isTemporary,
- temporaryAccessEndTime: source.temporaryAccessEndTime
- });
- if (sourceState === "unconditional") hasUnconditionalAllow = true;
- else hasConditionalAllow = true;
+ const baseResult = {
+ action,
+ label,
+ description,
+ isLegacy
+ };
+
+ // An unconditional forbid anywhere fully revokes the action — attribute it to
+ // the source(s) that declared the inverted rule(s) so the UI can show them.
+ if (unconditionalDenyRules.length > 0) {
+ return {
+ ...baseResult,
+ state: "forbid",
+ grantedBy: [],
+ forbiddenBy: dedupeSources(unconditionalDenyRules),
+ conditions: []
+ };
+ }
+
+ if (unconditionalAllowRules.length === 0 && conditionalAllowRules.length === 0) {
+ return { ...baseResult, state: "forbid", grantedBy: [], forbiddenBy: [], conditions: [] };
+ }
+
+ const conditions: AuditCondition[] = [];
+ conditionalAllowRules.forEach((r) => {
+ if (r.conditions) conditions.push({ kind: "allow", conditions: r.conditions });
+ });
+ conditionalDenyRules.forEach((r) => {
+ if (r.conditions) conditions.push({ kind: "forbid", conditions: r.conditions });
});
+ const grantedBy = dedupeSources([...unconditionalAllowRules, ...conditionalAllowRules]);
+
let state: AuditState;
- if (hasUnconditionalAllow) {
+ if (unconditionalAllowRules.length > 0 && conditionalDenyRules.length === 0) {
state = "allow";
- } else if (hasConditionalAllow) {
- state = "conditional";
} else {
- state = "deny";
+ state = "conditional";
}
- return {
- action,
- label,
- description,
- isLegacy,
- state,
- grantedBy,
- conditions
- };
+ return { ...baseResult, state, grantedBy, forbiddenBy: [], conditions };
};
export const evaluateResource = (
@@ -142,7 +155,7 @@ export const evaluateResource = (
// Hide legacy actions unless this user actually has them granted — keeps the
// table focused on the modern action set while still surfacing legacy grants
// for users who haven't migrated.
- const actions = allActions.filter((a) => !a.isLegacy || a.state !== "deny");
+ const actions = allActions.filter((a) => !a.isLegacy || a.state !== "forbid");
const allowedCount = actions.filter((a) => a.state === "allow").length;
const conditionalCount = actions.filter((a) => a.state === "conditional").length;
@@ -173,26 +186,27 @@ export const evaluateAllResources = (
): ResourceAudit[] => descriptors.map((d) => evaluateResource(d, sources));
export type ConditionEntry = {
+ kind: "allow" | "forbid";
field: string;
operator: string;
value: string;
};
-export const formatConditionEntries = (conditions: Record<string, unknown>): ConditionEntry[] => {
- return Object.entries(conditions).map(([field, value]) => {
+export const formatConditionEntries = (condition: AuditCondition): ConditionEntry[] => {
+ return Object.entries(condition.conditions).map(([field, value]) => {
if (value && typeof value === "object") {
const entries = Object.entries(value as Record<string, unknown>);
- if (entries.length === 0) return { field, operator: "eq", value: "" };
+ if (entries.length === 0) return { kind: condition.kind, field, operator: "eq", value: "" };
const [operator, operand] = entries[0];
const opLabel = operator.replace(/^\$/, "");
const operandStr = Array.isArray(operand) ? operand.join(", ") : String(operand);
- return { field, operator: opLabel, value: operandStr };
+ return { kind: condition.kind, field, operator: opLabel, value: operandStr };
}
- return { field, operator: "eq", value: String(value) };
+ return { kind: condition.kind, field, operator: "eq", value: String(value) };
});
};
-export const formatCondition = (conditions: Record<string, unknown>): string[] =>
- formatConditionEntries(conditions).map((e) =>
+export const formatCondition = (condition: AuditCondition): string[] =>
+ formatConditionEntries(condition).map((e) =>
e.value ? `${e.field} ${e.operator} ${e.value}` : e.field
);