ArgumentError: utc_offset out of range in Git Commit Date Processing

Summary

GitLab encounters an ArgumentError: utc_offset out of range when processing commit dates from Git repositories, specifically when calling Time.strptime with timezone offset values that exceed Ruby's acceptable range.

Root Cause

The error occurs in lib/gitlab/git/commit.rb at line 464 in the init_date_from_gitaly method:

def init_date_from_gitaly(author)
  if author.timezone.present?
    Time.strptime("#{author.date.seconds} #{author.timezone}", '%s %z')
  else
    Time.at(author.date.seconds).utc
  end
end

The issue happens when:

  1. Gitaly provides commit author/committer data with timezone offsets that are outside Ruby's valid range
  2. Ruby's Time.strptime method with the %z format specifier expects timezone offsets within ±18 hours (±1800 minutes)
  3. Some Git repositories may contain commits with malformed or extreme timezone values that exceed this range

Stack Trace

ArgumentError: utc_offset out of range (ArgumentError)

          t.localtime(offset)
                      ^^^^^^
  from time.rb:131:in `localtime'
  from time.rb:131:in `force_zone!'
  from time.rb:465:in `strptime'
  from lib/gitlab/git/commit.rb:464:in `init_date_from_gitaly'
  from lib/gitlab/git/commit.rb:290:in `block in authored_date'
  from gitlab/utils/strong_memoize.rb:34:in `strong_memoize'
  from lib/gitlab/git/commit.rb:289:in `authored_date'
  from lib/gitlab/git/commit.rb:321:in `block in to_hash'
  from lib/gitlab/git/commit.rb:320:in `map'
  from lib/gitlab/git/commit.rb:320:in `with_object'
  from lib/gitlab/git/commit.rb:320:in `to_hash'
  from app/models/commit.rb:351:in `method_missing'

Impact

Affected Operations: Any operation that accesses commit dates, including:

  • Viewing commit details (authored_date, committed_date)
  • Commit serialization (to_hash)
  • Repository browsing and commit history
  • Merge request diff views
  • API responses containing commit data

Error Propagation: The error bubbles up through:

  • authored_dateinit_date_from_gitalyTime.strptime
  • committed_dateinit_date_from_gitalyTime.strptime
  • to_hashauthored_date/committed_date → exception

Reproduction Scenario

This typically occurs with:

  • Repositories containing commits with malformed timezone data
  • Commits created with incorrect system clock/timezone settings
  • Imported repositories from other version control systems
  • Commits with timezone offsets beyond ±18:00 (e.g., ±2500, extreme values)

Proposed Solution

Implement timezone offset validation and fallback handling in init_date_from_gitaly:

  1. Validate timezone offset range before calling Time.strptime
  2. Fallback to UTC for invalid timezone values
  3. Log warnings for malformed timezone data to aid debugging
  4. Preserve original timestamp while normalizing timezone

This would ensure GitLab remains functional even when processing repositories with malformed commit metadata, while maintaining data integrity for valid timezone values.

Edited by 🤖 GitLab Bot 🤖