Skip to content

Commit 7487765

Browse files
author
Gerald Baulig
committed
refactor(context_query): check whether context_query is truly empty
1 parent c7a52b2 commit 7487765

File tree

2 files changed

+53
-23
lines changed

2 files changed

+53
-23
lines changed

src/core/accessController.ts

Lines changed: 48 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,23 @@ export class AccessController {
3030
resourceAdapter: ResourceAdapter;
3131
redisClient: RedisClientType<any, any>;
3232
userTopic: Topic;
33-
waiting: any[];
33+
waiting: any;
3434
cfg: any;
3535
userService: UserServiceClient;
36-
constructor(private logger: Logger, opts: AccessControlConfiguration,
37-
userTopic: Topic, cfg: any, userService: UserServiceClient) {
36+
37+
constructor(
38+
private logger: Logger,
39+
opts: AccessControlConfiguration,
40+
userTopic: Topic,
41+
cfg: any,
42+
userService: UserServiceClient
43+
) {
3844
this.policySets = new Map<string, PolicySetWithCombinables>();
3945
this.combiningAlgorithms = new Map<string, any>();
4046

4147
logger.info('Parsing combining algorithms from access control configuration...');
4248
// parsing URNs and mapping them to functions
43-
const combiningAlgorithms: CombiningAlgorithm[] = opts?.combiningAlgorithms || [];
49+
const combiningAlgorithms: CombiningAlgorithm[] = opts?.combiningAlgorithms ?? [];
4450
for (let ca of combiningAlgorithms) {
4551
const urn = ca.urn;
4652
const method = ca.method;
@@ -115,22 +121,29 @@ export class AccessController {
115121

116122
// policyEffect needed to evalute if the properties should be PERMIT / DENY
117123
let policyEffect: Effect;
118-
if ((!!policySet.target && await this.targetMatches(policySet.target, request, 'isAllowed', obligations))
119-
|| !policySet.target) {
124+
if (
125+
!policySet.target
126+
|| await this.targetMatches(policySet.target, request, 'isAllowed', obligations)
127+
) {
120128
let exactMatch = false;
121129
for (let [, policyValue] of policySet.combinables) {
122130
const policy: Policy = policyValue;
123131
if (policy.effect) {
124132
policyEffect = policy.effect;
125-
} else if (policy.combining_algorithm) {
133+
}
134+
else if (policy.combining_algorithm) {
126135
const method = this.combiningAlgorithms.get(policy.combining_algorithm);
127136
if (method === 'permitOverrides') {
128137
policyEffect = Effect.PERMIT;
129138
} else if (method === 'denyOverrides') {
130139
policyEffect = Effect.DENY;
131140
}
132141
}
133-
if (!!policy.target && await this.targetMatches(policy.target, request, 'isAllowed', obligations, policyEffect)) {
142+
143+
if (
144+
policy.target
145+
&& await this.targetMatches(policy.target, request, 'isAllowed', obligations, policyEffect)
146+
) {
134147
exactMatch = true;
135148
break;
136149
}
@@ -151,11 +164,18 @@ export class AccessController {
151164
continue;
152165
}
153166
const ruleEffects: EffectEvaluation[] = [];
154-
if ((!!policy.target && exactMatch && await this.targetMatches(policy.target, request, 'isAllowed', obligations, policyEffect))
167+
if (
168+
!policy.target
169+
|| (
170+
exactMatch
171+
&& await this.targetMatches(policy.target, request, 'isAllowed', obligations, policyEffect)
172+
)
155173
// regex match
156-
|| (!!policy.target && !exactMatch && await this.targetMatches(policy.target, request, 'isAllowed', obligations, policyEffect, true))
157-
|| !policy.target) {
158-
174+
|| (
175+
!exactMatch
176+
&& await this.targetMatches(policy.target, request, 'isAllowed', obligations, policyEffect, true)
177+
)
178+
) {
159179
const rules: Map<string, Rule> = policy.combinables;
160180
this.logger.verbose(`Checking policy ${policy.name}`);
161181
// only apply a policy effect if there are no rules
@@ -184,19 +204,26 @@ export class AccessController {
184204
}
185205

186206
if (matches) {
187-
this.logger.verbose(`Checking rule ${rule.name}`);
207+
this.logger.verbose(`Checking rule HR Scope for ${rule.name}`);
188208
if (matches && rule.target) {
189209
matches = await checkHierarchicalScope(rule.target, request, this.urns, this, this.logger);
190210
}
191211

192212
try {
193-
if (matches && !_.isEmpty(rule.condition)) {
213+
if (matches && rule.condition?.length) {
194214
// context query is only checked when a rule exists
195215
let context: any;
196-
if (!_.isEmpty(rule.context_query) && this.resourceAdapter) {
216+
if (
217+
this.resourceAdapter
218+
&& (
219+
rule.context_query?.filters?.length
220+
|| rule.context_query?.query?.length
221+
)
222+
) {
197223
context = await this.pullContextResources(rule.context_query, request);
198224

199225
if (_.isNil(context)) {
226+
this.logger.debug('Context query response is empty!');
200227
return { // deny by default
201228
decision: Response_Decision.DENY,
202229
obligations,
@@ -209,12 +236,12 @@ export class AccessController {
209236
}
210237
}
211238

212-
request.context = context || request.context;
239+
request.context = context ?? request.context;
213240
this.logger.debug('Validating rule condition', { name: rule.name, condition: rule.condition });
214241
matches = conditionMatches(rule.condition, request);
215242
this.logger.debug('condition validation response', { matches });
216243
}
217-
} catch (err) {
244+
} catch (err: any) {
218245
this.logger.error('Caught an exception while applying rule condition to request', { code: err.code, message: err.message, stack: err.stack });
219246
return { // if an exception is caught deny by default
220247
decision: Response_Decision.DENY,
@@ -296,7 +323,10 @@ export class AccessController {
296323
let obligations: Attribute[] = [];
297324
for (let [, value] of this.policySets) {
298325
let pSet: PolicySetRQ;
299-
if (_.isEmpty(value.target) || await this.targetMatches(value.target, request, 'whatIsAllowed', obligations)) {
326+
if (
327+
_.isEmpty(value.target)
328+
|| await this.targetMatches(value.target, request, 'whatIsAllowed', obligations)
329+
) {
300330
pSet = _.merge({}, { combining_algorithm: value.combining_algorithm }, _.pick(value, ['id', 'target', 'effect'])) as any;
301331
pSet.policies = [];
302332

src/core/hierarchicalScope.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ export const checkHierarchicalScope = async (ruleTarget: Target,
148148
}
149149

150150
if (_.isNil(entityOrOperation) || _.isEmpty(entityOrOperation)) {
151-
logger.debug('No Entity or operation name found');
151+
logger.debug('No entity or operation name found');
152152
// return false; // no entity found
153153
}
154154

@@ -179,7 +179,7 @@ export const checkHierarchicalScope = async (ruleTarget: Target,
179179
for (let roleScopeInstObj of attribute.attributes) { // role-attributes-attributes -> roleScopingInstance
180180
if (roleScopeInstObj.id == urns.get('roleScopingInstance') && !!scopingEntity) { // if scoping instance is found within the attributes
181181
const instances = entities.get(scopingEntity);
182-
if (!_.isEmpty(_.remove(instances, i => i == roleScopeInstObj.value))) { // if any element was removed
182+
if (!_.isEmpty(_.remove(instances, (i: string) => i == roleScopeInstObj.value))) { // if any element was removed
183183
if (_.isEmpty(instances)) {
184184
entities.delete(scopingEntity);
185185
if (entities.size == 0) {
@@ -217,11 +217,11 @@ export const checkHierarchicalScope = async (ruleTarget: Target,
217217
if (!check && hierarchicalRoleScopeCheck && hierarchicalRoleScopeCheck === 'true') {
218218
const hierarchicalScopes = context.subject.hierarchical_scopes;
219219
for (let hierarchicalScope of hierarchicalScopes) {
220-
let subTreeRole = null;
220+
let subTreeRole: string = null;
221221
let level = -1;
222222
traverse(hierarchicalScope).forEach(function (node: any): void { // depth-first search
223223
let subtreeFound = false;
224-
if (!!node.id) {
224+
if (node.id) {
225225
if (level > -1 && this.level >= level) {
226226
subTreeRole = null;
227227
level = -1;
@@ -239,7 +239,7 @@ export const checkHierarchicalScope = async (ruleTarget: Target,
239239
}
240240
if (subtreeFound) {
241241
const entities = scopedRoles.get(subTreeRole);
242-
let eligibleOrgScopes = [];
242+
let eligibleOrgScopes: string[] = [];
243243
getAllValues(node, eligibleOrgScopes);
244244
if (entities) {
245245
for (let [entity, instances] of entities) {

0 commit comments

Comments
 (0)