メールのAuthentication-Resultsは一番上の自社分だけ信じる

迷惑メールの調査で生のヘッダを貼ると、Authentication-Results: が2本以上出てくることがある。片方は spf=fail、もう片方は spf=pass というように、結果が食い違うこともある。どちらを信じるかを決めずに「pass がある」と読むと判断を誤る。

この記事では、ハシトシステムの メールヘッダ解析 がヘッダをどう読むかを確かめ、RFC 8601(Authentication-Results)と RFC 5321・RFC 5322 で、信じてよい行と Received の日時の決まりを確認する。最後に、偽の結果が混ざる例と、日時が読めなくなる例を Node.js で動かす。

検証環境は Windows 11、Node.js v24.11.1、Python 3.11.9 である。

ツールの読み方

公開中のページのスクリプト(2026-12-01 確認)は次のように動く。

  • 行頭が空白かタブの行を前の行の続きとしてつなぐ(RFC 5322 の unfolding)。空行でヘッダの終わりとみなす。
  • Received: を下から順に並べ直し、各行の最後の ; より後ろを日時として new Date() で読む。読む前に丸括弧のコメントを取り除く。
  • Authentication-Results: は、すべての行から spf= dkim= dmarc= arc= compauth= の結果を拾い、同じ組み合わせの重複だけを除いて並べる。

つまり、認証結果は「どの行に書かれていたか」を区別せずに一覧にする。ヘッダの中身をざっと見るには便利だが、送信者が書き込んだ行も同じように並ぶ点は知っておく必要がある。

どの Authentication-Results を信じるか

RFC 8601 では、Authentication-Results の先頭にその結果を出したサーバの名前(authserv-id)を書く。そして次のように決めている。

  • 受信側の MTA は、自分の管理範囲の authserv-id を名乗る既存の Authentication-Results を、信頼できる内部の MTA から直接届いたもの以外は削除するか無効にしなければならない(5節)。
  • 読む側は、authserv-id が自分の管理範囲で使っているものかを確かめてから結果を使う(4.1節)。
  • このフィールドは trace フィールドとして扱い、メッセージの先頭に付け足す。並べ替えてはならない(4.1節)。自分で付けるものは、他の trace フィールドより上に置く(4節)。

Received も同じく先頭に付け足される。RFC 5321 の 4.4 節は、SMTP サーバは Received を「メッセージの先頭に」挿入し、既存の Received を変更・削除してはならないとしている。したがって、受け取ったサーバが付けた本物の結果は、最初の Received より上にある。Received より下にある Authentication-Results は、途中のサーバか送信者が書いたものである。

自社のサーバが RFC 8601 5節どおりに古い行を消していれば偽の行は届かない。しかし、ヘッダを貼り付けて調べる場面では、その前提が成り立っているかが分からないことが多い。そこで「最初の Received より上」と「自分の authserv-id」の2つで絞る。

Received の日時の決まり

RFC 5321 の Received の書式では、; の後ろに RFC 5322 の date-time を書き、2桁の年などの古い形式は使えない。RFC 5322 では次のように決まっている。

  • +0000 は UTC。-0000 も UTC を表すが、「生成したシステムの地方時が分からない」ことを示す(3.3節)。
  • 古い形式で認められる文字の時間帯は UT GMT と米国の EST EDT CST CDT MST MDT PST PDT、軍用の1文字だけで、意味の分からない時間帯は -0000 とみなす(4.3節)。JST はこの一覧に無い。
  • 2桁の年は 00〜49 なら 2000 を足し、50〜99 なら 1900 を足す(4.3節)。

実際に試す

準備

1
2
mkdir mailheader && cd mailheader
curl -s -o live.html https://hashitosystem.com/tools/mailheader/

Node.js 18 以降と Python 3 があればよい。追加のパッケージは使わない。

ページのスクリプトから、画面に依存しない関数だけを取り出す。認証結果の拾い方はページの render() の中にあるので、同じ正規表現で書き写す。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// tool.mjs
import { readFileSync } from 'node:fs';
import vm from 'node:vm';

const html = readFileSync('live.html', 'utf8');
const start = html.indexOf('function unfold');
const end = html.indexOf('function human');
const ctx = { atob, TextDecoder };
vm.createContext(ctx);
vm.runInContext(html.slice(start, end), ctx);

export const { unfold, decodeWords, receivedDate, field } = ctx;

// render() の中の認証結果の拾い方と同じ正規表現
export function authTags(headers) {
const out = [];
for (const h of headers) {
if (h.name.toLowerCase() !== 'authentication-results') continue;
const re = /\b(spf|dkim|dmarc|arc|compauth)\s*=\s*([A-Za-z]+)/g;
let m;
while ((m = re.exec(h.value)) !== null) out.push(m[1].toUpperCase() + '=' + m[2].toLowerCase());
}
return [...new Set(out)];
}

偽の結果が混ざる例

受信サーバ mail.example.org が付けた本物の結果(fail)が一番上にあり、送信者が自分で書き込んだ偽の結果(pass)が Received の下にある、という例を作る。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
// forged.mjs
import { unfold, authTags } from './tool.mjs';

const raw = [
'Authentication-Results: mail.example.org;',
'\tspf=fail smtp.mailfrom=example.net;',
'\tdkim=none; dmarc=fail header.from=example.com',
'Received: from smtp.example.net (smtp.example.net [198.51.100.10])',
'\tby mail.example.org with ESMTPS id 4A1B2C3D4E;',
'\tTue, 1 Dec 2026 23:10:05 +0900',
'Authentication-Results: mail.example.org;',
'\tspf=pass smtp.mailfrom=example.com; dkim=pass header.d=example.com;',
'\tdmarc=pass header.from=example.com',
'From: "Support" <support@example.com>',
'Subject: test',
].join('\r\n');

const headers = unfold(raw);
console.log('tool:', authTags(headers).join(' '));

// 信じるのは「自分の authserv-id」で、最初の Received より上にあるものだけ
function trusted(headers, myId) {
const res = [];
for (const h of headers) {
const n = h.name.toLowerCase();
if (n === 'received') break;
if (n !== 'authentication-results') continue;
const id = h.value.split(';')[0].trim().split(/\s+/)[0].toLowerCase();
if (id === myId) res.push(h.value.replace(/\s+/g, ' '));
}
return res;
}
console.log('trusted:', trusted(headers, 'mail.example.org'));
1
2
3
4
5
$ node forged.mjs
tool: SPF=fail DKIM=none DMARC=fail SPF=pass DKIM=pass DMARC=pass
trusted: [
'mail.example.org; spf=fail smtp.mailfrom=example.net; dkim=none; dmarc=fail header.from=example.com'
]

偽の行も同じ authserv-id(mail.example.org)を名乗っているので、名前だけでは区別できない。ツールの一覧には fail と pass が両方並ぶ。位置(最初の Received より上か)で絞ると、本物の fail だけが残った。

日時が読めなくなる例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// dates.mjs
import { receivedDate } from './tool.mjs';

const lines = [
'from a by b with ESMTP id 1; Tue, 1 Dec 2026 23:10:05 +0900',
'from a by b with ESMTP id 1; Tue, 1 Dec 2026 23:10:05 +0900 (JST)',
'from a by b with ESMTP id 1; Tue, 1 Dec 2026 14:10:05 -0000',
'from a by b with ESMTP id 1; Tue, 1 Dec 2026 23:10:05 JST',
'from a by b with ESMTP id 1; Tue, 1 Dec 26 23:10:05 +0900',
'from a by b with ESMTP id 1; 1 Dec 2026 23:10:05 +0900',
'from a by b with ESMTP id 1; Tue, 1 Dec 2026 23:10:05 +0900 (x; y)',
'from a by b with ESMTP id 1',
];
for (const v of lines) {
const d = receivedDate(v);
console.log(v.slice(v.indexOf(';') + 1).trim().padEnd(40), '->', d ? d.toISOString() : null);
}
1
2
3
4
5
6
7
8
9
$ node dates.mjs
Tue, 1 Dec 2026 23:10:05 +0900 -> 2026-12-01T14:10:05.000Z
Tue, 1 Dec 2026 23:10:05 +0900 (JST) -> 2026-12-01T14:10:05.000Z
Tue, 1 Dec 2026 14:10:05 -0000 -> 2026-12-01T14:10:05.000Z
Tue, 1 Dec 2026 23:10:05 JST -> null
Tue, 1 Dec 26 23:10:05 +0900 -> 2026-12-01T14:10:05.000Z
1 Dec 2026 23:10:05 +0900 -> 2026-12-01T14:10:05.000Z
Tue, 1 Dec 2026 23:10:05 +0900 (x; y) -> null
from a by b with ESMTP id 1 -> null
  • +0900 (JST) のように JST がコメントにあるだけなら、コメントを取り除くので読める。
  • 時間帯を JST だけで書くと、Node.js の Date は読めず null になった(ツールでは「日時を読めません」と出る)。EST なら読める。
  • 2桁の年 26 は 2026 として読まれた。RFC 5322 の 00〜49 の規則と同じ結果である。
  • 日時の後ろのコメントに ; があると、最後の ; から後ろが y) だけになり、読めなくなった。
  • ; の無い Received も null になる。

Python の結果と比べる

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# pydate.py
import sys
from email.utils import parsedate_to_datetime

print('python', sys.version.split()[0])
for s in [
'Tue, 1 Dec 2026 23:10:05 +0900',
'Tue, 1 Dec 2026 14:10:05 -0000',
'Tue, 1 Dec 2026 23:10:05 JST',
'Tue, 1 Dec 2026 09:10:05 EST',
'Tue, 1 Dec 26 23:10:05 +0900',
]:
try:
d = parsedate_to_datetime(s)
print(s.ljust(32), '->', d.isoformat(), 'tzinfo=', d.tzinfo)
except Exception as e:
print(s.ljust(32), '->', type(e).__name__, e)
1
2
3
4
5
6
7
$ python pydate.py
python 3.11.9
Tue, 1 Dec 2026 23:10:05 +0900 -> 2026-12-01T23:10:05+09:00 tzinfo= UTC+09:00
Tue, 1 Dec 2026 14:10:05 -0000 -> 2026-12-01T14:10:05 tzinfo= None
Tue, 1 Dec 2026 23:10:05 JST -> 2026-12-01T23:10:05 tzinfo= None
Tue, 1 Dec 2026 09:10:05 EST -> 2026-12-01T09:10:05-05:00 tzinfo= UTC-05:00
Tue, 1 Dec 26 23:10:05 +0900 -> 2026-12-01T23:10:05+09:00 tzinfo= UTC+09:00

Python の email.utils.parsedate_to_datetime は、-0000 を「時間帯の無い(naive な)日時」として返す。JST も同じく naive になり、エラーにはならない。naive な日時は他の日時と引き算できないので、ホップごとの所要時間を計算する段階で TypeError になる。Node.js の Date は -0000 を UTC として読み、JST は読めない。同じヘッダでも、言語によって失敗する場所が違う。

どう使うか

  • 認証結果を見るときは、自社の受信サーバの authserv-id を先に確かめ、最初の Received より上にあるその行だけを読む。メールヘッダ解析 の一覧は全行の結果をまとめたものなので、pass と fail が両方出たら、「すべてのヘッダ」の表で行の位置を見る。
  • 自社でメールを受けるなら、受信サーバが外から来た自社名義の Authentication-Results を消しているか(RFC 8601 5節)を確かめる。
  • Received の日時で遅延を調べるときは、各サーバの時計のずれも結果に入る。所要時間がマイナスになったら、遅延ではなく時計のずれを疑う。
  • 日時を自前で読むなら、JST のような一覧外の時間帯と、コメントの中の ; を想定する。読めない行は飛ばし、前後の行から所要時間を計算する。

SPF の評価の上限については SPFの10回制限はinclude先まで数える:Node.jsで再帰計測 で、件名の =?UTF-8?B? については メール件名の=?UTF-8?B?を75文字で分割するとき文字を切らない方法 で扱っている。

まとめ

  • Authentication-Results は誰でも書き込める。RFC 8601 は、受信側が自分の authserv-id を名乗る既存の行を消し、読む側は authserv-id を確かめてから使うことを求めている。
  • 本物の結果は最初の Received より上にある。偽の行が同じ authserv-id を名乗っていても、位置で区別できた。
  • メールヘッダ解析は全行の認証結果をまとめて並べるので、食い違ったときは行の位置を確かめる。
  • Received の日時は JST だけの時間帯やコメントの中の ; で読めなくなった。Python では -0000 と JST が naive な日時になった。

結果は 2026-12-01 時点の上記の環境で得たものである。

参考

  • RFC 8601「Message Header Field for Indicating Message Authentication Status」4節・4.1節・5節・7.1節
  • RFC 5321「Simple Mail Transfer Protocol」4.4節 Trace Information
  • RFC 5322「Internet Message Format」3.3節 Date and Time Specification・4.3節 Obsolete Date and Time
  • Python ドキュメント「email.utils」parsedate_to_datetime
  • Node.js v24.11.1、Python 3.11.9 での上記スクリプトによる実測
  • ハシトシステム メールヘッダ解析のページのスクリプト(2026-12-01 確認)

メールのAuthentication-Resultsは一番上の自社分だけ信じる
https://blog.hashito.biz/2026/12/01/mail-authentication-results-forged-authserv-id-received-date-jst/
著者
hashito
作成日
2026年12月1日
著作権