Skip to content

Commit

Permalink
Fix directory traversal in Timezone.get when using Ruby data source
Browse files Browse the repository at this point in the history
  • Loading branch information
kratob authored and philr committed Jul 16, 2022
1 parent 07b5941 commit 9905ca9
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 2 deletions.
2 changes: 1 addition & 1 deletion lib/tzinfo/ruby_data_source.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def initialize
# Raises InvalidTimezoneIdentifier if the timezone is not found or the
# identifier is invalid.
def load_timezone_info(identifier)
raise InvalidTimezoneIdentifier, 'Invalid identifier' if identifier !~ /^[A-Za-z0-9\+\-_]+(\/[A-Za-z0-9\+\-_]+)*$/
raise InvalidTimezoneIdentifier, 'Invalid identifier' if identifier !~ /\A[A-Za-z0-9\+\-_]+(\/[A-Za-z0-9\+\-_]+)*\z/

This comment has been minimized.

Copy link
@Sole-Journey

Sole-Journey Oct 18, 2022

Good catch @dependabot


identifier = identifier.gsub(/-/, '__m__').gsub(/\+/, '__p__')

Expand Down
1 change: 1 addition & 0 deletions test/assets/payload.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
raise 'This should never be executed'
6 changes: 6 additions & 0 deletions test/tc_ruby_data_source.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,12 @@ def test_load_timezone_info_invalid
@data_source.load_timezone_info('../Definitions/UTC')
end
end

def test_load_timezone_info_directory_traversal
test_data_depth = TZINFO_TEST_DATA_DIR.scan('/').size
payload_path = File.join(TESTS_DIR, 'assets', 'payload')
assert_raises(InvalidTimezoneIdentifier) { Timezone.get("foo\n#{'/..' * (test_data_depth + 4)}#{payload_path}") }
end

def test_load_timezone_info_nil
assert_raises(InvalidTimezoneIdentifier) do
Expand Down
2 changes: 1 addition & 1 deletion test/tc_timezone.rb
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ def test_get_not_exist
end

def test_get_invalid
assert_raises(InvalidTimezoneIdentifier) { Timezone.get('../Definitions/UTC') }

This comment has been minimized.

Copy link
@Sole-Journey

Sole-Journey Oct 18, 2022

thanks for keeping my files right and tight @dependabot

assert_raises(InvalidTimezoneIdentifier) { Timezone.get('../definitions/UTC') }
end

def test_get_nil
Expand Down

0 comments on commit 9905ca9

Please sign in to comment.