Skip to content

Commit

Permalink
fix: use proxy on metadata permissions check to handle missing keys
Browse files Browse the repository at this point in the history
  • Loading branch information
scott-ray-wilson committed Nov 14, 2024
1 parent ccad684 commit c79f84c
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 12 deletions.
16 changes: 15 additions & 1 deletion backend/src/ee/services/permission/permission-fns.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,18 @@ function validateOrgSSO(actorAuthMethod: ActorAuthMethod, isOrgSsoEnforced: TOrg
}
}

export { isAuthMethodSaml, validateOrgSSO };
const escapeHandlebarsMissingMetadata = (obj: Record<string, string>) => {
const handler = {
get(target: Record<string, string>, prop: string) {
if (!(prop in target)) {
// eslint-disable-next-line no-param-reassign
target[prop] = `{{identity.metadata.${prop}}}`; // Add missing key as an "own" property
}
return target[prop];
}
};

return new Proxy(obj, handler);
};

export { escapeHandlebarsMissingMetadata, isAuthMethodSaml, validateOrgSSO };
27 changes: 16 additions & 11 deletions backend/src/ee/services/permission/permission-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import { TServiceTokenDALFactory } from "@app/services/service-token/service-tok

import { orgAdminPermissions, orgMemberPermissions, orgNoAccessPermissions, OrgPermissionSet } from "./org-permission";
import { TPermissionDALFactory } from "./permission-dal";
import { validateOrgSSO } from "./permission-fns";
import { escapeHandlebarsMissingMetadata, validateOrgSSO } from "./permission-fns";
import { TBuildOrgPermissionDTO, TBuildProjectPermissionDTO } from "./permission-service-types";
import {
buildServiceTokenProjectPermission,
Expand Down Expand Up @@ -227,11 +227,13 @@ export const permissionServiceFactory = ({
})) || [];

const rules = buildProjectPermissionRules(rolePermissions.concat(additionalPrivileges));
const templatedRules = handlebars.compile(JSON.stringify(rules), { data: false, strict: true });
const metadataKeyValuePair = objectify(
userProjectPermission.metadata,
(i) => i.key,
(i) => i.value
const templatedRules = handlebars.compile(JSON.stringify(rules), { data: false });
const metadataKeyValuePair = escapeHandlebarsMissingMetadata(
objectify(
userProjectPermission.metadata,
(i) => i.key,
(i) => i.value
)
);
const interpolateRules = templatedRules(
{
Expand Down Expand Up @@ -292,12 +294,15 @@ export const permissionServiceFactory = ({
})) || [];

const rules = buildProjectPermissionRules(rolePermissions.concat(additionalPrivileges));
const templatedRules = handlebars.compile(JSON.stringify(rules), { data: false, strict: true });
const metadataKeyValuePair = objectify(
identityProjectPermission.metadata,
(i) => i.key,
(i) => i.value
const templatedRules = handlebars.compile(JSON.stringify(rules), { data: false });
const metadataKeyValuePair = escapeHandlebarsMissingMetadata(
objectify(
identityProjectPermission.metadata,
(i) => i.key,
(i) => i.value
)
);

const interpolateRules = templatedRules(
{
identity: {
Expand Down

0 comments on commit c79f84c

Please sign in to comment.