-
Notifications
You must be signed in to change notification settings - Fork 914
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add server side private IP blocking for data source endpoints validation
Signed-off-by: Kristen Tian <[email protected]>
- Loading branch information
1 parent
e74ab2d
commit 09bfbd1
Showing
8 changed files
with
137 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
src/plugins/data_source/server/util/endpoint_validator.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import * as validator from './endpoint_validator'; | ||
|
||
describe('endpoint_validator', function () { | ||
it('Url1 that should be blocked should return false', function () { | ||
expect(validator.isValidURL('http://127.0.0.1', ['127.0.0.0/8'])).toEqual(false); | ||
}); | ||
|
||
it('Url2 that is invalid should return false', function () { | ||
expect(validator.isValidURL('www.test.com', [])).toEqual(false); | ||
}); | ||
|
||
it('Url3 that is invalid should return false', function () { | ||
expect(validator.isValidURL('ftp://www.test.com', [])).toEqual(false); | ||
}); | ||
|
||
it('Url4 that should be blocked should return false', function () { | ||
expect( | ||
validator.isValidURL('http://169.254.169.254/latest/meta-data/', ['169.254.0.0/16']) | ||
).toEqual(false); | ||
}); | ||
|
||
it('Url5 that should not be blocked should return true', function () { | ||
expect(validator.isValidURL('https://www.opensearch.org', ['127.0.0.0/8'])).toEqual(true); | ||
}); | ||
|
||
it('Url6 that should not be blocked should return true when null IPs', function () { | ||
expect(validator.isValidURL('https://www.opensearch.org')).toEqual(true); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import dns from 'dns-sync'; | ||
import IPCIDR from 'ip-cidr'; | ||
|
||
export function isValidURL(endpoint: string, deniedIPs?: string[]) { | ||
// Check the format of URL, URL has be in the format as | ||
// scheme://server/path/resource otherwise an TypeError | ||
// would be thrown. | ||
let url; | ||
try { | ||
url = new URL(endpoint); | ||
} catch (err) { | ||
return false; | ||
} | ||
|
||
if (!(Boolean(url) && (url.protocol === 'http:' || url.protocol === 'https:'))) { | ||
return false; | ||
} | ||
|
||
const ip = getIpAddress(url); | ||
if (!ip) { | ||
return false; | ||
} | ||
|
||
// IP CIDR check if a specific IP address fall in the | ||
// range of an IP address block | ||
for (const deniedIP of deniedIPs ?? []) { | ||
const cidr = new IPCIDR(deniedIP); | ||
if (cidr.contains(ip)) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
|
||
/** | ||
* Resolve hostname to IP address | ||
* @param {object} urlObject | ||
* @returns {string} configuredIP | ||
* or null if it cannot be resolve | ||
* According to RFC, all IPv6 IP address needs to be in [] | ||
* such as [::1]. | ||
* So if we detect a IPv6 address, we remove brackets. | ||
*/ | ||
function getIpAddress(urlObject: URL) { | ||
const hostname = urlObject.hostname; | ||
const configuredIP = dns.resolve(hostname); | ||
if (configuredIP) { | ||
return configuredIP; | ||
} | ||
if (hostname.startsWith('[') && hostname.endsWith(']')) { | ||
return hostname.substr(1).slice(0, -1); | ||
} | ||
return null; | ||
} |