feat: add ADDRESS_CHECK_REGEX (#415)

This commit is contained in:
Dream Hunter
2024-08-17 00:11:28 +08:00
committed by GitHub
parent 9888f98d74
commit 3664028e06
14 changed files with 74 additions and 20 deletions

View File

@@ -1,6 +1,10 @@
<!-- markdownlint-disable-file MD004 MD024 MD034 MD036 -->
# CHANGE LOG
## main(v0.7.3)
- feat: worker 增加 `ADDRESS_CHECK_REGEX`, address name 的正则表达式, 只用于检查,符合条件将通过检查
## v0.7.2
### Breaking Changes
@@ -10,7 +14,7 @@
### Changes
- fix: worker 增加 `NO_LIMIT_SEND_ROLE` 配置, 加载失败的问题
- feat: worker 增加 `# ADDRESS_REGEX = "[^a-z.0-9]"` 配置, 用于配置地址的正则表达式,如果不设置,默认为 [^a-z0-9], 需谨慎使用, 有些符号可能导致无法收件
- feat: worker 增加 `# ADDRESS_REGEX = "[^a-z.0-9]"` 配置, 替换非法符号的正则表达式,如果不设置,默认为 [^a-z0-9], 需谨慎使用, 有些符号可能导致无法收件
- feat: worker 优化 webhook 逻辑, 支持 admin 配置全局 webhook, 添加 `message pusher` 集成示例
## v0.7.1

View File

@@ -1,6 +1,6 @@
{
"name": "cloudflare_temp_email",
"version": "0.7.2",
"version": "0.7.3",
"private": true,
"type": "module",
"scripts": {

View File

@@ -1,6 +1,6 @@
{
"name": "temp-email-pages",
"version": "1.0.0",
"version": "0.7.3",
"description": "",
"main": "index.js",
"scripts": {

View File

@@ -78,7 +78,9 @@ node_compat = true
PREFIX = "tmp" # The mailbox name prefix to be processed
# (min, max) length of the adderss, if not set, the default is (1, 30)
# ANNOUNCEMENT = "Custom Announcement"
# address name REGEX, if not set, the default is [^a-z0-9]
# address check REGEX, if not set, will not check
# ADDRESS_CHECK_REGEX = "^(?!.*admin).*"
# address name replace REGEX, if not set, the default is [^a-z0-9]
# ADDRESS_REGEX = "[^a-z0-9]"
# MIN_ADDRESS_LEN = 1
# MAX_ADDRESS_LEN = 30

View File

@@ -46,7 +46,9 @@ node_compat = true
PREFIX = "tmp" # 要处理的邮箱名称前缀,不需要后缀可配置为空字符串
# (min, max) adderss的长度如果不设置默认为(1, 30)
# ANNOUNCEMENT = "Custom Announcement" # 自定义公告
# address name 的正则表达式,如果不设置,默认为 [^a-z0-9], 需谨慎使用, 有些符号可能导致无法收件
# address name 的正则表达式, 只用于检查,符合条件将通过检查
# ADDRESS_CHECK_REGEX = "^(?!.*admin).*"
# address name 替换非法符号的正则表达式, 不在其中的符号将被替换,如果不设置,默认为 [^a-z0-9], 需谨慎使用, 有些符号可能导致无法收件
# ADDRESS_REGEX = "[^a-z0-9]"
# MIN_ADDRESS_LEN = 1
# MAX_ADDRESS_LEN = 30

View File

@@ -1,7 +1,7 @@
{
"name": "temp-mail-docs",
"private": true,
"version": "0.2.6",
"version": "0.7.3",
"type": "module",
"devDependencies": {
"@types/node": "^22.3.0",

View File

@@ -1,6 +1,6 @@
{
"name": "cloudflare_temp_email",
"version": "0.0.0",
"version": "0.7.3",
"private": true,
"type": "module",
"scripts": {

View File

@@ -41,7 +41,13 @@ api.post('/admin/new_address', async (c) => {
return c.text("Please provide a name", 400)
}
try {
const res = await newAddress(c, name, domain, enablePrefix, false, null, false);
const res = await newAddress(c, {
name, domain, enablePrefix,
checkLengthByConfig: false,
addressPrefix: null,
checkAllowDomains: false,
enableCheckNameRegex: false,
});
return c.json(res);
} catch (e) {
return c.text(`Failed create address: ${(e as Error).message}`, 400)

View File

@@ -9,6 +9,24 @@ import { AdminWebhookSettings, WebhookMail, WebhookSettings } from './models';
const DEFAULT_NAME_REGEX = /[^a-z0-9]/g;
const checkNameRegex = (c: Context<HonoCustomType>, name: string) => {
let error = null;
try {
const regexStr = getStringValue(c.env.ADDRESS_CHECK_REGEX);
if (!regexStr) return;
const regex = new RegExp(regexStr);
if (!regex.test(name)) {
error = new Error(`Name not match regex: /${regexStr}/`);
}
}
catch (e) {
console.error("Failed to check address regex", e);
}
if (error) {
throw error;
}
}
const getNameRegex = (c: Context<HonoCustomType>): RegExp => {
try {
const regex = getStringValue(c.env.ADDRESS_REGEX);
@@ -25,12 +43,25 @@ const getNameRegex = (c: Context<HonoCustomType>): RegExp => {
export const newAddress = async (
c: Context<HonoCustomType>,
name: string, domain: string | undefined | null,
enablePrefix: boolean,
checkLengthByConfig: boolean = true,
addressPrefix: string | undefined | null = null,
checkAllowDomains: boolean = true
{
name,
domain,
enablePrefix,
checkLengthByConfig = true,
addressPrefix = null,
checkAllowDomains = true,
enableCheckNameRegex = true,
}: {
name: string, domain: string | undefined | null,
enablePrefix: boolean,
checkLengthByConfig?: boolean,
addressPrefix?: string | undefined | null,
checkAllowDomains?: boolean,
enableCheckNameRegex?: boolean,
}
): Promise<{ address: string, jwt: string }> => {
// check name
if (enableCheckNameRegex) checkNameRegex(c, name);
// remove special characters
name = name.replace(getNameRegex(c), '')
// name min length min 1

View File

@@ -1,5 +1,5 @@
export const CONSTANTS = {
VERSION: 'v0.7.2',
VERSION: 'v0.7.3',
// DB settings
ADDRESS_BLOCK_LIST_KEY: 'address_block_list',

View File

@@ -121,7 +121,12 @@ api.post('/api/new_address', async (c) => {
}
try {
const addressPrefix = await getAddressPrefix(c);
const res = await newAddress(c, name, domain, true, true, addressPrefix);
const res = await newAddress(c, {
name, domain,
enablePrefix: true,
checkLengthByConfig: true,
addressPrefix
});
return c.json(res);
} catch (e) {
return c.text(`Failed create address: ${(e as Error).message}`, 400)

View File

@@ -29,10 +29,11 @@ export const tgUserNewAddress = async (
if (blockList.some((item) => name.includes(item))) {
throw Error(`Name[${name}]is blocked`);
}
const res = await newAddress(c,
name || Math.random().toString(36).substring(2, 15),
domain, true
);
const res = await newAddress(c, {
name: name || Math.random().toString(36).substring(2, 15),
domain,
enablePrefix: true
});
// for mail push to telegram
await c.env.KV.put(`${CONSTANTS.TG_KV_PREFIX}:${userId}`, JSON.stringify([...jwtList, res.jwt]));
await c.env.KV.put(`${CONSTANTS.TG_KV_PREFIX}:${res.address}`, userId.toString());

View File

@@ -15,6 +15,7 @@ export type Bindings = {
TITLE: string | undefined
ANNOUNCEMENT: string | undefined | null
PREFIX: string | undefined
ADDRESS_CHECK_REGEX: string | undefined
ADDRESS_REGEX: string | undefined
MIN_ADDRESS_LEN: string | number | undefined
MAX_ADDRESS_LEN: string | number | undefined

View File

@@ -19,7 +19,9 @@ node_compat = true
# TITLE = "Custom Title" # custom title
# ANNOUNCEMENT = "Custom Announcement"
PREFIX = "tmp"
# address name REGEX, if not set, the default is [^a-z0-9]
# address check REGEX, if not set, will not check
# ADDRESS_CHECK_REGEX = "^(?!.*admin).*"
# address name replace REGEX, if not set, the default is [^a-z0-9]
# ADDRESS_REGEX = "[^a-z0-9]"
# (min, max) length of the adderss, if not set, the default is (1, 30)
# MIN_ADDRESS_LEN = 1