Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/scimPatch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ const IS_ARRAY_SEARCH = /(\[|\])/;
const ARRAY_SEARCH = /^(.+)\[(.+)\]$/;
// Split path on periods
const SPLIT_PERIOD = /(?!\B"[^[]*)\.(?![^\]]*"\B)/g;
// Regex to fetch the URN from a path.
const URN_MATCH = /^((?:[A-Za-z0-9][A-Za-z0-9.+-]*:)+[A-Za-z0-9.+-]*)/;
// Valid patch operation, value needs to be in lowercase here.
const AUTHORIZED_OPERATION = ['remove', 'add', 'replace'];

Expand Down Expand Up @@ -144,7 +146,10 @@ function validatePatchOperation(operation: ScimPatchOperation): void {
}

function resolvePaths(path: string): string[] {
const uriIndex = path.lastIndexOf(':');
// Identify the URN prefix if present
const urnPath = path.match(URN_MATCH);
// Find the last colon in the urn path
const uriIndex = urnPath ? urnPath[1].lastIndexOf(':') : -1;

if (uriIndex < 0) {
// No schema prefix - this is a core schema path
Expand Down
17 changes: 17 additions & 0 deletions test/scimPatch.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1195,6 +1195,23 @@ describe('SCIM PATCH', () => {
expect(afterPatch.emails[3].newProperty).to.be.an('undefined');
return done();
});

it('REPLACE: should handle colon in quoted filter values', done => {
scimUser.roles = [
{ value: "3675b69e-8324-4110-bdca-059031aa8da3:admin", type: "admin" },
{ value: "other-role", type: "user" }
];
const expected = "updated";
const patch: ScimPatchAddReplaceOperation = {
op: 'replace',
value: expected,
path: 'roles[value eq "3675b69e-8324-4110-bdca-059031aa8da3:admin"].value'
};
const afterPatch = scimPatch(scimUser, [patch]);
expect(afterPatch.roles![0].value).to.be.eq(expected);
expect(afterPatch.roles![1].value).to.be.eq("other-role");
return done();
});
Comment on lines +1199 to +1214

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

This is a great test case for the replace operation. To ensure the fix is robust and covers all scenarios, I recommend adding similar tests for the add and remove operations, as they also rely on the resolvePaths function. This will help prevent future regressions.

Here are some examples:

For add:

it('ADD: should handle colon in quoted filter values', done => {
    scimUser.roles = [
        { value: "3675b69e-8324-4110-bdca-059031aa8da3:admin", type: "admin" }
    ];
    const patch: ScimPatchAddReplaceOperation = {
        op: 'add',
        value: 'new-type',
        path: 'roles[value eq "3675b69e-8324-4110-bdca-059031aa8da3:admin"].type'
    };
    const afterPatch = scimPatch(scimUser, [patch]);
    // 'add' on a single-valued attribute replaces it
    expect(afterPatch.roles![0].type).to.be.eq('new-type');
    return done();
});

For remove:

it('REMOVE: should handle colon in quoted filter values', done => {
    scimUser.roles = [
        { value: "3675b69e-8324-4110-bdca-059031aa8da3:admin", type: "admin" },
        { value: "other-role", type: "user" }
    ];
    const patch: ScimPatchRemoveOperation = {
        op: 'remove',
        path: 'roles[value eq "3675b69e-8324-4110-bdca-059031aa8da3:admin"]'
    };
    const afterPatch = scimPatch(scimUser, [patch]);
    expect(afterPatch.roles!.length).to.be.eq(1);
    expect(afterPatch.roles![0].value).to.be.eq("other-role");
    return done();
});

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems unnecessary. imo, resolvePaths function could be exported so we could have unit test for it instead of integration tests across all operation methods.

});

describe('invalid requests', () => {
Expand Down