Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bugfix/cldsrv 505 ip handling fix #5533

Closed
wants to merge 2 commits into from
Closed
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
60 changes: 32 additions & 28 deletions lib/api/apiUtils/authorization/permissionChecks.js
Original file line number Diff line number Diff line change
Expand Up @@ -480,40 +480,44 @@ function validatePolicyResource(bucketName, policy) {
function checkIp(value) {
const errString = 'Invalid IP address in Conditions';

// these preliminary checks are validating the provided
// ip address against ipaddr.js, the library we use when
// evaluating IP condition keys. It ensures compatibility,
// but additional checks are required to enforce the right
// notation (e.g., xxx.xxx.xxx.xxx/xx for IPv4). Otherwise,
// we would accept different ip formats, which is not
// standard in an AWS use case.
try {
const values = Array.isArray(value) ? value : [value];

for (let i = 0; i < values.length; i++) {
// these preliminary checks are validating the provided
// ip address against ipaddr.js, the library we use when
// evaluating IP condition keys. It ensures compatibility,
// but additional checks are required to enforce the right
// notation (e.g., xxx.xxx.xxx.xxx/xx for IPv4). Otherwise,
// we would accept different ip formats, which is not
// standard in an AWS use case.
try {
parseCIDR(value);
try {
parseCIDR(values[i]);
} catch (err) {
isValid(values[i]);
}
} catch (err) {
isValid(value);
return errString; // Return immediately if an invalid IP is found
}
} catch (err) {
return errString;
}

// credit to Theodore John.S
// Medium article: Validating IPv4 and IPv6 Addresses with Ease
// — Unveiling the Power of Validation in JavaScript
const validateIpRegex = (ip) => {
if (constants.ipv4Regex.test(ip)) {
return ip.split('.').every(part => parseInt(part, 10) <= 255);
}
if (constants.ipv6Regex.test(ip)) {
return ip.split(':').every(part => part.length <= 4);
}
return false;
};
// Apply the existing IP validation logic to each element
const validateIpRegex = (ip) => {
if (constants.ipv4Regex.test(ip)) {
return ip.split('.').every(part => parseInt(part, 10) <= 255);
}
if (constants.ipv6Regex.test(ip)) {
return ip.split(':').every(part => part.length <= 4);
}
return false;
};

if (validateIpRegex(value) === true) {
return null;
if (validateIpRegex(values[i]) !== true) {
return errString; // Return immediately if an invalid IP is found
}
}
return errString;

// If the function hasn't returned by now, all elements are valid
return null;
}

// This function checks all bucket policy conditions if the values provided
Expand Down
39 changes: 36 additions & 3 deletions tests/unit/auth/permissionChecks.js
Original file line number Diff line number Diff line change
Expand Up @@ -382,17 +382,50 @@ describe('validatePolicyConditions', () => {
expected: 'Invalid IP address in Conditions',
},
{
description: 'Should return a relevant error message if the ' +
'condition value type does not match the expected type',
description: 'Should accept arrays of IPs',
inputPolicy: {
Statement: [{
Condition: {
IpAddress: { 'aws:SourceIp': [] }, // IP should be a string, not an array
IpAddress: {
'aws:SourceIp': [
'10.0.11.0/24',
'10.0.1.0/24',
],
},
},
}],
},
expected: null,
},
{
description: 'Should return relevant error if one of the IPs in the array is invalid',
inputPolicy: {
Statement: [{
Condition: {
IpAddress: {
'aws:SourceIp': [
'10.0.11.0/24',
'123',
],
},
},
}],
},
expected: 'Invalid IP address in Conditions',
},
{
description: 'Should not return error if array value in IP condition is empty', // this is AWS behavior
inputPolicy: {
Statement: [{
Condition: {
IpAddress: {
'aws:SourceIp': [],
},
},
}],
},
expected: null,
},
{
description: 'Should return null or a relevant error message ' +
'if multiple conditions are provided in a single statement',
Expand Down
Loading