From 16b2390abdffb876af017f7c3cd3d480a9bd8dfa Mon Sep 17 00:00:00 2001 From: Mradul Singh Date: Wed, 18 Nov 2026 09:58:16 +0530 Subject: [PATCH 1/2] Do not read a longer zone name as a military zone in Time.rfc2822 The zone part of the pattern had no right boundary, so "CET" matched the military zone "C" (+03:00), "HST" matched "H" (+08:00), "IST" matched "I" (+09:00) and so on. The date was parsed with a wrong offset instead of raising ArgumentError, as it already does for "JST". Require the zone not to be followed by a letter or a digit. Comments and other separators after the zone are still accepted. Also accept "UTC", which used to match only as "UT" followed by "C". --- lib/time.rb | 5 ++++- test/test_time.rb | 18 ++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/lib/time.rb b/lib/time.rb index 679cc82..1951b68 100644 --- a/lib/time.rb +++ b/lib/time.rb @@ -524,8 +524,11 @@ def rfc2822(date) :\s*(\d{2}) (?:\s*:\s*(\d\d))?\s+ ([+-]\d{4}| - UT|GMT|EST|EDT|CST|CDT|MST|MDT|PST|PDT|[A-IK-Z])/ix =~ date + UTC|UT|GMT|EST|EDT|CST|CDT|MST|MDT|PST|PDT|[A-IK-Z]) + (?![a-z\d])/ix =~ date # Since RFC 2822 permit comments, the regexp has no right anchor. + # The zone must still end there, so that "CET" is not read as the + # military zone "C". day = $1.to_i mon = MonthValue[$2.upcase] year = $3.to_i diff --git a/test/test_time.rb b/test/test_time.rb index 4054bc4..447fac0 100644 --- a/test/test_time.rb +++ b/test/test_time.rb @@ -62,6 +62,24 @@ def test_rfc2822 assert_equal(true, t.utc?) end + def test_rfc2822_zone_end + t = Time.rfc2822("Sat, 01 Jan 2000 12:00:00 UTC") + assert_equal(Time.utc(2000, 1, 1, 12, 0, 0), t) + assert_equal(true, t.utc?) + t = Time.rfc2822("Sat, 01 Jan 2000 12:00:00 GMT(comment)") + assert_equal(Time.utc(2000, 1, 1, 12, 0, 0), t) + t = Time.rfc2822("Sat, 01 Jan 2000 12:00:00 Z") + assert_equal(Time.utc(2000, 1, 1, 12, 0, 0), t) + + # A longer zone name is not a military zone with extra letters. + %w[CET CEST HST IST BST MSK EET UTX GMTX ESTX].each do |zone| + assert_raise(ArgumentError, zone) { + Time.rfc2822("Sat, 01 Jan 2000 12:00:00 #{zone}") + } + end + assert_raise(ArgumentError) { Time.rfc2822("Sat, 01 Jan 2000 12:00:00 +09001") } + end + def test_rfc2822_nonlinear pre = ->(n) {"0 Feb 00 00 :00" + " " * n} assert_linear_performance([100, 500, 5000, 50_000], pre: pre) do |s| From 0f22c53774aa84ce1151e0ee9a691bec46a6c504 Mon Sep 17 00:00:00 2001 From: Mradul Singh Date: Wed, 18 Nov 2026 22:09:37 +0530 Subject: [PATCH 2/2] Use \b for the end of the zone in Time.rfc2822 Suggested by nobu. --- lib/time.rb | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/time.rb b/lib/time.rb index 1951b68..c4abf93 100644 --- a/lib/time.rb +++ b/lib/time.rb @@ -524,8 +524,7 @@ def rfc2822(date) :\s*(\d{2}) (?:\s*:\s*(\d\d))?\s+ ([+-]\d{4}| - UTC|UT|GMT|EST|EDT|CST|CDT|MST|MDT|PST|PDT|[A-IK-Z]) - (?![a-z\d])/ix =~ date + UTC|UT|GMT|EST|EDT|CST|CDT|MST|MDT|PST|PDT|[A-IK-Z])\b/ix =~ date # Since RFC 2822 permit comments, the regexp has no right anchor. # The zone must still end there, so that "CET" is not read as the # military zone "C".