ullmark/hashids.net

Decoding input smaller than minHashLength unhandled exceptions are thrown

krstevkoki opened this issue · 4 comments

Seems like that when decoding input with length smaller than minHashLength the Decode throws System.ArgumentException & System.ArgumentOutOfRangeException exceptions

Expected Behavior

I guess it would be to validate the input before doing a .Decode() and either throw meaningful exception or return empty array

Current Behavior

Currently. the library sometimes throws (undocumented?) System.ArgumentException & System.ArgumentOutOfRangeException exceptions.

Possible Solution

Have a proper validation of the input before doing any actions or something else

Steps to Reproduce

  1. new HashidsNet.Hashids(salt: "Dqa2s3RJBYPHUzg&R5qkF3Z4HLaWp#A^kMc^DqKVmqag2tasQjhz-PSM23=4", minHashLength: 9)
  2. .Decode("5111111")
  3. System.ArgumentException: Destination is too short. (Parameter 'destination')

&

  1. new HashidsNet.Hashids(salt: "Dqa2s3RJBYPHUzg&R5qkF3Z4HLaWp#A^kMc^DqKVmqag2tasQjhz-PSM23=4", minHashLength: 10)
  2. .Decode("5111111")
  3. System.ArgumentOutOfRangeException: Specified argument was out of the range of valid values.

Context (Environment)

.NET version: ASP.NET Core 6.0 - basic API controller
Hashids.net version: 1.6.1
StackTrace 1:

System.ArgumentException: Destination is too short. (Parameter 'destination')
   at System.Text.StringBuilder.CopyTo(Int32 sourceIndex, Span`1 destination, Int32 count)
   at HashidsNet.Hashids.GenerateHashFrom(ReadOnlySpan`1 numbers, Span`1& result)
   at HashidsNet.Hashids.GetNumbersFrom(String hash)
   at HashidsNet.Hashids.Decode(String hash)
   at WebApplication1.Controllers.WeatherForecastsApi2Controller.GetWeatherForecast2()

StackTrace 2:

System.ArgumentOutOfRangeException: Specified argument was out of the range of valid values.
   at System.Text.StringBuilder.CopyTo(Int32 sourceIndex, Span`1 destination, Int32 count)
   at HashidsNet.Hashids.GenerateHashFrom(ReadOnlySpan`1 numbers, Span`1& result)
   at HashidsNet.Hashids.GetNumbersFrom(String hash)
   at HashidsNet.Hashids.Decode(String hash)
   at WebApplication1.Controllers.WeatherForecastsApi2Controller.GetWeatherForecast2()

Possible Implementation

/

Did some debugging, there seems to be an incorrect assumption that the hash which was passed is long enough.

Edit: Link to code

Thank you @krstevkoki for reporting.

@GKalamernikov Can you please link the actual line? You can get this by clicking on the line number to highlight, then use the menu to get the permalink.

image

I'm sorry, obviously I haven't used github

Here is the real link