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

Verify emails when creating an email connector, even if allowedDomain… #133859

Merged
merged 6 commits into from Jun 28, 2022
Expand Up @@ -182,11 +182,13 @@ describe('validate_email_address', () => {
},
Object {
"address": "totally invalid",
"valid": true,
"reason": "invalid",
"valid": false,
},
Object {
"address": "{{sneaky}}",
"valid": true,
"reason": "invalid",
"valid": false,
},
]
`);
Expand Down Expand Up @@ -226,7 +228,8 @@ describe('validate_email_address', () => {
},
Object {
"address": "totally invalid",
"valid": true,
"reason": "invalid",
"valid": false,
},
Object {
"address": "{{sneaky}}",
Expand Down
40 changes: 17 additions & 23 deletions x-pack/plugins/actions/common/validate_email_addresses.ts
Expand Up @@ -27,13 +27,6 @@ export function validateEmailAddresses(
addresses: string[],
options: ValidateEmailAddressesOptions = {}
): ValidatedEmail[] {
// note: this is the legacy default, which would in theory allow
// mustache strings, so options.allowMustache is ignored in this
// case - everything is valid!
if (allowedDomains == null) {
return validateEmailAddressesAsAlwaysValid(addresses);
}

return addresses.map((address) => validateEmailAddress(allowedDomains, address, options));
}

Expand All @@ -60,7 +53,7 @@ export function invalidEmailsAsMessage(validatedEmails: ValidatedEmail[]): strin

// in case the npm email-addresses returns unexpected things ...
function validateEmailAddress(
allowedDomains: string[],
allowedDomains: string[] | null,
address: string,
options: ValidateEmailAddressesOptions
): ValidatedEmail {
Expand All @@ -80,32 +73,33 @@ function validateEmailAddress(
}
}

function validateEmailAddress_(allowedDomains: string[], address: string): ValidatedEmail {
function validateEmailAddress_(allowedDomains: string[] | null, address: string): ValidatedEmail {
const emailAddresses = parseAddressList(address);
if (emailAddresses == null) {
return { address, valid: false, reason: InvalidEmailReason.invalid };
}

const allowedDomainsSet = new Set(allowedDomains);
if (allowedDomains !== null) {
const allowedDomainsSet = new Set(allowedDomains);

for (const emailAddress of emailAddresses) {
let domains: string[] = [];
for (const emailAddress of emailAddresses) {
let domains: string[] = [];

if (emailAddress.type === 'group') {
domains = emailAddress.addresses.map((groupAddress) => groupAddress.domain);
} else if (emailAddress.type === 'mailbox') {
domains = [emailAddress.domain];
} else {
return { address, valid: false, reason: InvalidEmailReason.invalid };
}
if (emailAddress.type === 'group') {
domains = emailAddress.addresses.map((groupAddress) => groupAddress.domain);
} else if (emailAddress.type === 'mailbox') {
domains = [emailAddress.domain];
} else {
return { address, valid: false, reason: InvalidEmailReason.invalid };
}

for (const domain of domains) {
if (!allowedDomainsSet.has(domain)) {
return { address, valid: false, reason: InvalidEmailReason.notAllowed };
for (const domain of domains) {
if (!allowedDomainsSet.has(domain)) {
return { address, valid: false, reason: InvalidEmailReason.notAllowed };
}
}
}
}
ymao1 marked this conversation as resolved.
Show resolved Hide resolved

return { address, valid: true };
}

Expand Down
5 changes: 3 additions & 2 deletions x-pack/plugins/actions/public/plugin.test.ts
Expand Up @@ -12,7 +12,7 @@ describe('Actions Plugin', () => {
describe('setup()', () => {
const emails = ['bob@elastic.co', 'jim@somewhere.org', 'not an email'];

it('should allow all emails when not using email allowlist config', async () => {
it('should allow all the valid emails when not using email allowlist config', async () => {
const context = coreMock.createPluginInitializerContext({});
const plugin = new Plugin(context);
const pluginSetup = plugin.setup();
Expand All @@ -29,7 +29,8 @@ describe('Actions Plugin', () => {
},
Object {
"address": "not an email",
"valid": true,
"reason": "invalid",
"valid": false,
},
]
`);
Expand Down