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

feat: |UI| add JUNK_MAIL_FORCE_PASS_LIST #539

Merged
merged 1 commit into from
Dec 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

- feat: |Github Action| 增加自动更新并部署功能
- feat: |UI| admin 用户设置,支持 oauth2 配置的删除
- feat: 增加垃圾邮件检测必须通过的列表 `JUNK_MAIL_FORCE_PASS_LIST` 配置

## v0.8.2

Expand Down
2 changes: 2 additions & 0 deletions vitepress-docs/docs/en/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,8 @@ ENABLE_AUTO_REPLY = false
# FRONTEND_URL = "https://xxxx.xxx"
# Enable check junk mail
# ENABLE_CHECK_JUNK_MAIL = false
# junk mail force check pass list, if no status or status is not pass, will be marked as junk mail
# JUNK_MAIL_FORCE_PASS_LIST = ["spf", "dkim", "dmarc"]

[[d1_databases]]
binding = "DB"
Expand Down
4 changes: 3 additions & 1 deletion vitepress-docs/docs/zh/guide/cli/worker.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,10 @@ ENABLE_AUTO_REPLY = false
# FORWARD_ADDRESS_LIST = ["[email protected]"]
# 前端地址,用于发送 webhook 的邮件 url
# FRONTEND_URL = "https://xxxx.xxx"
# 是否启用垃圾邮件检查
# 是否启用垃圾邮件检查,默认任何一项存在配置且不通过则被判定为垃圾邮件
# ENABLE_CHECK_JUNK_MAIL = false
# 垃圾邮件检查配置, 任何一项不存在或者不通过则被判定为垃圾邮件
# JUNK_MAIL_FORCE_PASS_LIST = ["spf", "dkim", "dmarc"]

# D1 数据库的名称和 ID 可以在 cloudflare 控制台查看
[[d1_databases]]
Expand Down
1 change: 1 addition & 0 deletions worker/src/admin_api/worker_config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ export default {
"DISABLE_SHOW_GITHUB": !getBooleanValue(c.env.DISABLE_SHOW_GITHUB),
"DISABLE_ADMIN_PASSWORD_CHECK": getBooleanValue(c.env.DISABLE_ADMIN_PASSWORD_CHECK),
"ENABLE_CHECK_JUNK_MAIL": getBooleanValue(c.env.ENABLE_CHECK_JUNK_MAIL),
"JUNK_MAIL_FORCE_PASS_LIST": getStringArray(c.env.JUNK_MAIL_FORCE_PASS_LIST),
})
}
}
44 changes: 27 additions & 17 deletions worker/src/email/check_junk.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Bindings } from "../types";
import { getBooleanValue } from "../utils";
import { getBooleanValue, getStringArray } from "../utils";
import { commonParseMail } from "../common";

export const check_if_junk_mail = async (
Expand All @@ -11,34 +11,44 @@ export const check_if_junk_mail = async (
}
const parsedEmail = await commonParseMail(raw_mail);
if (!parsedEmail?.headers) return false;

const forcePassList = getStringArray(env.JUNK_MAIL_FORCE_PASS_LIST);
const passedList: string[] = [];

const headers = parsedEmail.headers;
for (const header of headers) {
if (!header["key"]) continue;
if (!header["value"]) continue;

// check spf
if (header["key"].toLowerCase() == "received-spf"
&&
!header["value"].toLowerCase().includes("pass")
) {
return true;
if (header["key"].toLowerCase() == "received-spf") {
if (!header["value"].toLowerCase().includes("pass")) {
return true;
}
passedList.push("spf");
}

// check dkim and dmarc
if (header["key"].toLowerCase() == "authentication-results") {
if (header["value"].toLowerCase().includes("dkim=")
&&
!header["value"].toLowerCase().includes("dkim=pass")
) {
return true;
if (header["value"].toLowerCase().includes("dkim=")) {
if (!header["value"].toLowerCase().includes("dkim=pass")) {
return true;
}
passedList.push("dkim");
}
if (header["value"].toLowerCase().includes("dmarc=")
&&
!header["value"].toLowerCase().includes("dmarc=pass")
) {
return true;
if (header["value"].toLowerCase().includes("dmarc=")) {
if (!header["value"].toLowerCase().includes("dmarc=pass")) {
return true;
}
passedList.push("dmarc");
}
}
}
return false;

if (forcePassList?.length == 0) return false;

// check force pass list
return forcePassList.some(
(checkName) => !passedList.includes(checkName.toLowerCase())
);
}
1 change: 1 addition & 0 deletions worker/src/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ export type Bindings = {
FORWARD_ADDRESS_LIST: string | string[] | undefined

ENABLE_CHECK_JUNK_MAIL: string | boolean | undefined
JUNK_MAIL_FORCE_PASS_LIST: string | string[] | undefined

// s3 config
S3_ENDPOINT: string | undefined
Expand Down
2 changes: 2 additions & 0 deletions worker/wrangler.toml.template
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ ENABLE_AUTO_REPLY = false
# FRONTEND_URL = "https://xxxx.xxx"
# Enable check junk mail
# ENABLE_CHECK_JUNK_MAIL = false
# junk mail force check pass list, if no status or status is not pass, will be marked as junk mail
# JUNK_MAIL_FORCE_PASS_LIST = ["spf", "dkim", "dmarc"]

[[d1_databases]]
binding = "DB"
Expand Down
Loading