camo: include subdomains of whitelisted domains in whitelist
This commit is contained in:
parent
07179d6c83
commit
637bcad816
5 changed files with 53 additions and 4 deletions
|
|
@ -6,9 +6,9 @@ import { CamoConfig } from './configuration/camoconfig';
|
|||
const LOGGER = require('@calzoneman/jsli')('camo');
|
||||
|
||||
function isWhitelisted(camoConfig: CamoConfig, url: string): boolean {
|
||||
const whitelistedDomains = camoConfig.getWhitelistedDomains();
|
||||
const whitelistedDomains = camoConfig.getWhitelistedDomainsRegexp();
|
||||
const parsed = urlparse.parse(url);
|
||||
return whitelistedDomains.includes(parsed.hostname);
|
||||
return whitelistedDomains.test('.' + parsed.hostname);
|
||||
}
|
||||
|
||||
export function camoify(camoConfig: CamoConfig, url: string): string {
|
||||
|
|
|
|||
|
|
@ -1,3 +1,5 @@
|
|||
const SPECIALCHARS = /([\\\.\?\+\*\$\^\|\(\)\[\]\{\}])/g;
|
||||
|
||||
class CamoConfig {
|
||||
constructor(config = { camo: { enabled: false } }) {
|
||||
this.config = config.camo;
|
||||
|
|
@ -30,6 +32,17 @@ class CamoConfig {
|
|||
return this.config['whitelisted-domains'] || [];
|
||||
}
|
||||
|
||||
getWhitelistedDomainsRegexp() {
|
||||
const domains = this.getWhitelistedDomains()
|
||||
.map(d => '\\.' + d.replace(SPECIALCHARS, '\\$1') + '$');
|
||||
if (domains.length === 0) {
|
||||
// If no whitelist, match nothing
|
||||
return new RegExp('$^');
|
||||
}
|
||||
|
||||
return new RegExp(domains.join('|'), 'i');
|
||||
}
|
||||
|
||||
getEncoding() {
|
||||
return this.config.encoding || 'url';
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue