Skip to content

Commit

Permalink
fix: Some fixes for misskey-dev#12850
Browse files Browse the repository at this point in the history
- refinement the error message when trueMail validation fails
- the settings of trueMail are not displayed after saving
- changing how `Active Email Validation` is saved
  • Loading branch information
MarryDream committed Dec 30, 2023
1 parent 30594dd commit 760d041
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 32 deletions.
76 changes: 51 additions & 25 deletions packages/backend/src/core/EmailService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ export class EmailService {
@bindThis
public async validateEmailForAccount(emailAddress: string): Promise<{
available: boolean;
reason: null | 'used' | 'format' | 'disposable' | 'mx' | 'smtp' | 'banned' | 'network' | 'blacklist';
reason: null | string;
}> {
const meta = await this.metaService.fetch();

Expand All @@ -165,10 +165,17 @@ export class EmailService {
email: emailAddress,
});

if (exist !== 0) {
return {
available: false,
reason: 'used',
};
}

let validated: {
valid: boolean,
reason?: string | null,
};
} = { valid: true, reason: null };

if (meta.enableActiveEmailValidation) {
if (meta.enableVerifymailApi && meta.verifymailAuthKey != null) {
Expand All @@ -185,33 +192,38 @@ export class EmailService {
validateSMTP: false, // 日本だと25ポートが殆どのプロバイダーで塞がれていてタイムアウトになるので
});
}
} else {
validated = { valid: true, reason: null };
}

if (!validated.valid) {
let reason = validated.reason ?? null;
if (reason === 'regex') {
reason = 'format';
}
return {
available: false,
reason,
};
}

const emailDomain: string = emailAddress.split('@')[1];
const isBanned = this.utilityService.isBlockedHost(meta.bannedEmailDomains, emailDomain);

const available = exist === 0 && validated.valid && !isBanned;
if (isBanned) {
return {
available: false,
reason: 'banned',
};
}

return {
available,
reason: available ? null :
exist !== 0 ? 'used' :
isBanned ? 'banned' :
validated.reason === 'regex' ? 'format' :
validated.reason === 'disposable' ? 'disposable' :
validated.reason === 'mx' ? 'mx' :
validated.reason === 'smtp' ? 'smtp' :
validated.reason === 'network' ? 'network' :
validated.reason === 'blacklist' ? 'blacklist' :
null,
available: true,
reason: null,
};
}

private async verifyMail(emailAddress: string, verifymailAuthKey: string): Promise<{
valid: boolean;
reason: 'used' | 'format' | 'disposable' | 'mx' | 'smtp' | null;
reason: 'used' | 'format' | 'disposable' | 'mx' | 'smtp' | 'key' | null;
}> {
const endpoint = 'https://verifymail.io/api/' + emailAddress + '?key=' + verifymailAuthKey;
const res = await this.httpRequestService.send(endpoint, {
Expand All @@ -223,6 +235,7 @@ export class EmailService {
});

const json = (await res.json()) as {
message?: string;
block: boolean;
catch_all: boolean;
deliverable_email: boolean;
Expand All @@ -239,6 +252,12 @@ export class EmailService {
related_domains: string[];
};

if (json.message) {
return {
valid: false,
reason: 'key',
};
}
if (json.email_address === undefined) {
return {
valid: false,
Expand Down Expand Up @@ -272,7 +291,7 @@ export class EmailService {

private async trueMail<T>(truemailInstance: string, emailAddress: string, truemailAuthKey: string): Promise<{
valid: boolean;
reason: 'used' | 'format' | 'blacklist' | 'mx' | 'smtp' | 'network' | T | null;
reason: 'key' | 'used' | 'format' | 'blacklist' | 'mx' | 'smtp' | 'network' | T | null;
}> {
const endpoint = truemailInstance + '?email=' + emailAddress;
try {
Expand All @@ -284,22 +303,29 @@ export class EmailService {
Authorization: truemailAuthKey
},
});

const json = (await res.json()) as {
email: string;
success: boolean;
errors?: {
error?: string;
errors?: {
list_match?: string;
regex?: string;
mx?: string;
smtp?: string;
} | null;
};
if (json.email === undefined || (json.email !== undefined && json.errors?.regex)) {

if (json.error) {
return {
valid: false,
reason: 'format',
valid: false,
reason: 'key',
};
}
if (json.email === undefined || json.errors?.regex) {
return {
valid: false,
reason: 'format',
};
}
if (json.errors?.smtp) {
Expand All @@ -320,7 +346,7 @@ export class EmailService {
reason: json.errors?.list_match as T || 'blacklist',
};
}

return {
valid: true,
reason: null,
Expand Down
18 changes: 11 additions & 7 deletions packages/frontend/src/pages/admin/security.vue
Original file line number Diff line number Diff line change
Expand Up @@ -70,27 +70,28 @@ SPDX-License-Identifier: AGPL-3.0-only

<div class="_gaps_m">
<span>{{ i18n.ts.activeEmailValidationDescription }}</span>
<MkSwitch v-model="enableActiveEmailValidation" @update:modelValue="save">
<MkSwitch v-model="enableActiveEmailValidation">
<template #label>Enable</template>
</MkSwitch>
<MkSwitch v-model="enableVerifymailApi" @update:modelValue="save">
<MkSwitch v-model="enableVerifymailApi">
<template #label>Use Verifymail.io API</template>
</MkSwitch>
<MkInput v-model="verifymailAuthKey" @update:modelValue="save">
<MkInput v-model="verifymailAuthKey">
<template #prefix><i class="ti ti-key"></i></template>
<template #label>Verifymail.io API Auth Key</template>
</MkInput>
<MkSwitch v-model="enableTruemailApi" @update:modelValue="save">
<MkSwitch v-model="enableTruemailApi">
<template #label>Use TrueMail API</template>
</MkSwitch>
<MkInput v-model="truemailInstance" @update:modelValue="save">
<MkInput v-model="truemailInstance">
<template #prefix><i class="ti ti-key"></i></template>
<template #label>TrueMail API Instance</template>
</MkInput>
<MkInput v-model="truemailAuthKey" @update:modelValue="save">
<MkInput v-model="truemailAuthKey">
<template #prefix><i class="ti ti-key"></i></template>
<template #label>TrueMail API Auth Key</template>
</MkInput>
<MkButton primary @click="save"><i class="ti ti-device-floppy"></i> {{ i18n.ts.save }}</MkButton>
</div>
</MkFolder>

Expand Down Expand Up @@ -188,7 +189,10 @@ async function init() {
enableActiveEmailValidation.value = meta.enableActiveEmailValidation;
enableVerifymailApi.value = meta.enableVerifymailApi;
verifymailAuthKey.value = meta.verifymailAuthKey;
bannedEmailDomains.value = meta.bannedEmailDomains.join('\n');
enableTruemailApi.value = meta.enableTruemailApi;
truemailInstance.value = meta.truemailInstance;
truemailAuthKey.value = meta.truemailAuthKey;
bannedEmailDomains.value = meta.bannedEmailDomains?.join('\n') || "";
}
function save() {
Expand Down
3 changes: 3 additions & 0 deletions packages/misskey-js/src/autogen/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4456,6 +4456,9 @@ export type operations = {
enableActiveEmailValidation: boolean;
enableVerifymailApi: boolean;
verifymailAuthKey: string | null;
enableTruemailApi: boolean;
truemailInstance: string | null;
truemailAuthKey: string | null;
enableChartsForRemoteUser: boolean;
enableChartsForFederatedInstances: boolean;
enableServerMachineStats: boolean;
Expand Down

0 comments on commit 760d041

Please sign in to comment.