Skip to content
Merged
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
2 changes: 2 additions & 0 deletions lib/mcapi/encryption/field-level-encryption.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ function encrypt(endpoint, header, body) {
let bodyMap = body;
const fleConfig = utils.hasConfig(this.config, endpoint);
if (fleConfig) {
if (!fleConfig.toEncrypt) return { header: header, body: body };
if (!this.isWithHeader) {
bodyMap = fleConfig.toEncrypt.map((v) => {
return encryptBody.call(this, v, body);
Expand Down Expand Up @@ -81,6 +82,7 @@ function decrypt(response) {
let bodyMap = response.body;
const fleConfig = utils.hasConfig(this.config, response.request.url);
if (fleConfig) {
if (!fleConfig.toDecrypt) return body;
bodyMap = fleConfig.toDecrypt.map((v) => {
if (!this.isWithHeader) {
return decryptBody.call(this, v, body);
Expand Down
4 changes: 3 additions & 1 deletion lib/mcapi/encryption/jwe-encryption.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ function encrypt(endpoint, header, body) {
let bodyMap = body;
const fleConfig = utils.hasConfig(this.config, endpoint);
if (fleConfig) {
if (!fleConfig.toEncrypt) return { header: header, body: body };
bodyMap = fleConfig.toEncrypt.map((v) => {
return encryptBody.call(this, v, body);
});
Expand All @@ -49,7 +50,8 @@ function decrypt(response) {
let bodyMap = response.body;
const fleConfig = utils.hasConfig(this.config, response.request.url);
if (fleConfig) {
bodyMap = fleConfig.toDecrypt.map((v) => {
if (!fleConfig.toDecrypt) return body;
bodyMap = (fleConfig.toDecrypt).map((v) => {
return decryptBody.call(this, v, body);
});
}
Expand Down
2 changes: 1 addition & 1 deletion lib/mcapi/utils/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@
} else {
return JSON.stringify(data);
}
} catch (e) {

Check warning on line 84 in lib/mcapi/utils/utils.js

View workflow job for this annotation

GitHub Actions / lint

'e' is defined but never used
throw new Error("Json not valid"); // eslint-disable-line preserve-caught-error
}
};
Expand All @@ -103,7 +103,7 @@
}
try {
return JSON.parse(data);
} catch (e) {

Check warning on line 106 in lib/mcapi/utils/utils.js

View workflow job for this annotation

GitHub Actions / lint

'e' is defined but never used
return data.valueOf();
}
};
Expand All @@ -111,7 +111,7 @@
function isJson(str) {
try {
JSON.parse(str);
} catch (e) {

Check warning on line 114 in lib/mcapi/utils/utils.js

View workflow job for this annotation

GitHub Actions / lint

'e' is defined but never used
return false;
}
return true;
Expand Down Expand Up @@ -372,7 +372,7 @@
}

config.paths.forEach((path) => {
if (multipleRoots(path.toEncrypt) || multipleRoots(path.toDecrypt)) {
if (multipleRoots(path.toEncrypt || []) || multipleRoots(path.toDecrypt || [])) {
throw Error(
"Config not valid: found multiple configurations encrypt/decrypt with root mapping"
);
Expand Down Expand Up @@ -406,7 +406,7 @@
node: obj,
parent: parent,
};
} catch (e) {

Check warning on line 409 in lib/mcapi/utils/utils.js

View workflow job for this annotation

GitHub Actions / lint

'e' is defined but never used
return null;
}
};
Expand Down
19 changes: 19 additions & 0 deletions test/field-level-encryption.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,16 @@ describe("Field Level Encryption", () => {
assert.ok(!res.body.elem1.encryptedData.accountNumber);
});

it("returns body untouched when toEncrypt is not defined", () => {
const config = JSON.parse(JSON.stringify(testConfig));
delete config.paths[0].toEncrypt;
const fle = new FieldLevelEncryption(config);
const body = { elem1: { encryptedData: { accountNumber: "5123456789012345" } } };
const res = encrypt.call(fle, "/resource", null, body);
assert.ok(res.header === null);
assert.ok(res.body === body);
});

it("encrypt body payload with readme config", () => {
const testConfigReadme = require("./mock/config-readme");
const fle = new FieldLevelEncryption(testConfigReadme);
Expand Down Expand Up @@ -197,6 +207,15 @@ describe("Field Level Encryption", () => {
assert.ok(res === response.body);
});

it("returns body untouched when toDecrypt is not defined", () => {
const config = JSON.parse(JSON.stringify(testConfig));
delete config.paths[0].toDecrypt;
const fle = new FieldLevelEncryption(config);
const response = JSON.parse(JSON.stringify(require("./mock/response")));
const res = decrypt.call(fle, response);
assert.ok(res === response.body);
});

it("decrypt response replacing whole body", () => {
const config = JSON.parse(JSON.stringify(testConfig));
config.paths[0].toDecrypt[0].element = "";
Expand Down
23 changes: 23 additions & 0 deletions test/jwe-encryption.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,19 @@ describe("JWE Encryption", () => {
});
});

describe("#encrypt without toEncrypt", () => {
it("returns body untouched", () => {
const config = JSON.parse(JSON.stringify(testConfig));
delete config.paths[0].toEncrypt;
const encryption = new JweEncryption(config);
const encrypt = JweEncryption.__get__("encrypt");
const body = { elem1: { encryptedData: { accountNumber: "5123456789012345" } } };
const res = encrypt.call(encryption, "/resource", null, body);
assert.ok(res.header === null);
assert.ok(res.body === body);
});
});

describe("#decrypt", () => {
it("decrypt response", () => {
const encryption = new JweEncryption(testConfig);
Expand All @@ -51,6 +64,16 @@ describe("JWE Encryption", () => {
);
});

it("returns body untouched when toDecrypt is not defined", () => {
const config = JSON.parse(JSON.stringify(testConfig));
delete config.paths[0].toDecrypt;
const encryption = new JweEncryption(config);
const decrypt = JweEncryption.__get__("decrypt");
const response = JSON.parse(JSON.stringify(require("./mock/jwe-response")));
const res = decrypt.call(encryption, response);
assert.ok(res === response.body);
});

it("decrypt first level element in response", () => {
const encryption = new JweEncryption(testConfig);
const decrypt = JweEncryption.__get__("decrypt");
Expand Down
Loading