New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Base64.decode64 incorrect result when missing paddings #4028

Closed
Jeiwan opened this Issue Jul 26, 2016 · 0 comments

Comments

Projects
None yet
2 participants
@Jeiwan

Jeiwan commented Jul 26, 2016

Environment

JRuby version:
jruby 9.1.2.0 (2.3.0) 2016-05-26 7357c8f OpenJDK 64-Bit Server VM 25.91-b14 on 1.8.0_91-8u91-b14-1~bpo8+1-b14 +jit [linux-x86_64]
No special command line arguments.

OS:
Linux 29ea782aaebe 4.4.15-boot2docker #1 SMP Wed Jul 13 14:05:46 UTC 2016 x86_64 GNU/Linux

Expected Behavior

MRI (ruby 2.2.2p95 (2015-04-13 revision 50295) [x86_64-darwin14]):

f = "eyJzY2hlbWEiOiJpZ2x1OmNvbS5zbm93cGxvd2FuYWx5dGljcy5zbm93cGxvdy9jb250ZXh0cy9qc29uc2NoZW1hLzEtMC0wIiwiZGF0YSI6W3t9XX0"
f.size
 => 115
require 'base64'
Base64.decode64(f)
 => "{\"schema\":\"iglu:com.snowplowanalytics.snowplow/contexts/jsonschema/1-0-0\",\"data\":[{}]}"

As you can see, MRI correctly determines if a string is missing Base64 paddings and correctly decodes the string.

Actual Behavior

JRuby:

f = "eyJzY2hlbWEiOiJpZ2x1OmNvbS5zbm93cGxvd2FuYWx5dGljcy5zbm93cGxvdy9jb250ZXh0cy9qc29uc2NoZW1hLzEtMC0wIiwiZGF0YSI6W3t9XX0"
f.size
 => 115
require 'base64'
Base64.decode64(f)
=> "{\"schema\":\"iglu:com.snowplowanalytics.snowplow/contexts/jsonschema/1-0-0\",\"data\":[{}"

As you can see, JRuby doesn't detect missing paddings and incorrectly decodes the string (ignores the last three-octet group). Here's a workaround I use:

...
Base64.decode64(f + '=' * (f.size % 4))
 => "{\"schema\":\"iglu:com.snowplowanalytics.snowplow/contexts/jsonschema/1-0-0\",\"data\":[{}]}"

Base64.decode64 uses String#unpack under the hood. Here's the piece in MRI source codes responsible for the correct implementation: https://github.com/ruby/ruby/blob/trunk/pack.c#L1618

@enebo enebo closed this in #4438 Jan 12, 2017

@enebo enebo added this to the JRuby 9.1.8.0 milestone Jan 12, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment