Skip to content

Commit

Permalink
fix(baseget): validate value as object instead of null
Browse files Browse the repository at this point in the history
allow to evaluate non-existed paths for more than 1 step
  • Loading branch information
roggervalf committed May 24, 2020
1 parent ee8cbb7 commit 6998f6d
Show file tree
Hide file tree
Showing 13 changed files with 353 additions and 291 deletions.
3 changes: 3 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@
}
],
"@typescript-eslint/no-explicit-any": "off",

// "@typescript-eslint/ban-ts-ignore": "off",

"no-console": "off",

"no-param-reassign": [
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,12 @@ const contextForAllowExample = { user: { id: 456 } };

allowExample.evaluate({
action: 'read',
resource: 'secrets:456:ultrasecret',
resource: 'secrets:456:ultraSecret',
context: contextForAllowExample
}); // true
allowExample.evaluate({
action: 'create',
resource: 'secrets:456:ultrasecret',
resource: 'secrets:456:ultraSecret',
context: contextForAllowExample
}); // false
allowExample.evaluate({
Expand Down
4 changes: 2 additions & 2 deletions example/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@ const contextForAllowExample = { user: { id: 456 } };
console.log(
allowExample.evaluate({
action: 'read',
resource: 'secrets:456:ultrasecret',
resource: 'secrets:456:ultraSecret',
context: contextForAllowExample
})
);
// false
console.log(
allowExample.evaluate({
action: 'create',
resource: 'secrets:456:ultrasecret',
resource: 'secrets:456:ultraSecret',
context: contextForAllowExample
})
);
Expand Down
2 changes: 1 addition & 1 deletion src/Matcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ export class Matcher {
? this.expand(balance.post, false)
: [''];

parts.forEach(part => {
parts.forEach((part: string) => {
postParts.forEach(postPart => {
const expansion = pre + part + postPart;
if (!isTop || expansion) expansions.push(expansion);
Expand Down
1 change: 0 additions & 1 deletion src/Policies.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
//import { Statement, StatementInterface, ConditionResolver } from './Statement';
import {
IdentityBasedType,
ResourceBasedType,
Expand Down
12 changes: 11 additions & 1 deletion src/Statement.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,26 @@ describe('Statement Class', () => {
expect(applyContext('secrets:${user.bestFriends}:account', context)).toBe(
'secrets:{123,532,987}:account'
);

expect(
applyContext(
'secrets:${user.id}:bestFriends:${user.bestFriends}',
context
)
).toBe('secrets:456:bestFriends:{123,532,987}');
});

it('can match undefined path', () => {
it('can match non-existed path', () => {
const context = {
user: { id: 456, bestFriends: [123, 987] }
};

expect(applyContext('secrets:${}:account', context)).toBe(
'secrets:undefined:account'
);
expect(applyContext('secrets:${company.address}:account', context)).toBe(
'secrets:undefined:account'
);
});
});

Expand Down
14 changes: 6 additions & 8 deletions src/Statement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@ import {
EffectBlock,
ConditionBlock,
StatementInterface,
// ConditionKey,
Context,
MatchConditionInterface
} from './types';
import { getValueFromPath } from './utils/getValueFromPath';

const reDelimiters = /\${([^}])*}/g;
const reDelimiters = /\${([^}]*)}/g;
const trim = / +(?= )|^\s+|\s+$/g;

const specialTrim = (str: string): string => str.replace(trim, '');
Expand All @@ -17,13 +16,12 @@ export function applyContext(str: string, context?: Context): string {
if (!context) return str;

return specialTrim(
str.replace(reDelimiters, match => {
const path = match.substr(2, match.length - 3);
let value = getValueFromPath(context, path);
if (Array.isArray(value)) value = `{${value}}`;
if (value instanceof Object) value = undefined;
str.replace(reDelimiters, (_, path: string) => {
const value = getValueFromPath(context, path);
if (Array.isArray(value)) return `{${value}}`;
if (value instanceof Object) return String(undefined);

return match ? String(value) : '';
return String(value);
})
);
}
Expand Down
2 changes: 2 additions & 0 deletions src/utils/decomposeString.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { DecomposeString } from '../types';
* Get index range where separators are found.
*
* @private
* @since 3.1.1
* @param {string} initialSeparator First string to be found.
* @param {string} finalSeparator Second string to be found.
* @param {string} str String to be decomposed.
Expand Down Expand Up @@ -47,6 +48,7 @@ function getIndexRange(
* Decompose string in pre, body and post strings by using separators.
*
* @private
* @since 3.1.1
* @param {string} initialSeparator First string to be found.
* @param {string} finalSeparator Second string to be found.
* @param {string} str String to be decomposed.
Expand Down
1 change: 1 addition & 0 deletions src/utils/getValueFromPath.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ describe('getValueFromPath', () => {
};
expect(getValueFromPath(context, 'user.id.pets')).toBe(undefined);
expect(getValueFromPath(context, 'company')).toBe(undefined);
expect(getValueFromPath(context, 'company.address')).toBe(undefined);
});
});

Expand Down
2 changes: 1 addition & 1 deletion src/utils/getValueFromPath.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export function baseGet<T>(object: object, path: Array<T> | string): any {
const length = newPath.length;

let value: any = object;
while (value !== null && index < length) {
while (value instanceof Object && index < length) {
value = value[toKey(newPath[index++])];
}

Expand Down
2 changes: 1 addition & 1 deletion src/utils/isKey.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { isSymbol } from './isSymbol';

/** Used to match property names within property paths. */
const reIsDeepProp = /\.|\[(?:[^[\]]*|(["'])(?:(?!\1)[^\\]|\\.)*?\1)\]/;
const reIsPlainProp = /^\w*$/; //maches any word caracter (alphanumeric and underscore)
const reIsPlainProp = /^\w*$/; //matches any word caracter (alphanumeric and underscore)

/**
* Checks if `value` is a property name and not a property path.
Expand Down
4 changes: 2 additions & 2 deletions translations/README.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,12 @@ const contextForAllowExample = { user: { id: 456 } };

allowExample.evaluate({
action: 'read',
resource: 'secrets:456:ultrasecret',
resource: 'secrets:456:ultraSecret',
context: contextForAllowExample
}); // true
allowExample.evaluate({
action: 'create',
resource: 'secrets:456:ultrasecret',
resource: 'secrets:456:ultraSecret',
context: contextForAllowExample
}); // false
allowExample.evaluate({
Expand Down

0 comments on commit 6998f6d

Please sign in to comment.