Skip to content

Commit

Permalink
Merge pull request #1748 from gfx/urlsafe_decode64_for_forward_compat…
Browse files Browse the repository at this point in the history
…ibility

Use Base64.urlsafe_decode64 for forward compatibility
  • Loading branch information
Robert Mosolgo authored Aug 9, 2018
2 parents 2e88a41 + 53f6e6d commit 0f0eef8
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 3 deletions.
1 change: 0 additions & 1 deletion lib/graphql/relay.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
# frozen_string_literal: true
require 'base64'

require 'graphql/relay/page_info'
require 'graphql/relay/edge'
Expand Down
25 changes: 25 additions & 0 deletions lib/graphql/schema/base_64_bp.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# frozen_string_literal: true

require 'base64'

# backport from ruby v2.5 to v2.2 that has no `padding` things
# @api private
module Base64Bp
extend Base64

module_function

def urlsafe_encode64(bin, padding:)
str = strict_encode64(bin).tr("+/", "-_")
str = str.delete("=") unless padding
str
end

def urlsafe_decode64(str)
str = str.tr("-_", "+/")
if !str.end_with?("=") && str.length % 4 != 0
str = str.ljust((str.length + 3) & ~3, "=")
end
strict_decode64(str)
end
end
6 changes: 5 additions & 1 deletion lib/graphql/schema/base_64_encoder.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
# frozen_string_literal: true

require 'graphql/schema/base_64_bp'

module GraphQL
class Schema
# @api private
Expand All @@ -8,7 +11,8 @@ def self.encode(plaintext, nonce: false)
end

def self.decode(ciphertext, nonce: false)
Base64.decode64(ciphertext)
# urlsafe_decode64 is for forward compatibility
Base64Bp.urlsafe_decode64(ciphertext)
end
end
end
Expand Down
5 changes: 4 additions & 1 deletion lib/graphql/schema/unique_within_type.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# frozen_string_literal: true
require 'graphql/schema/base_64_bp'

module GraphQL
class Schema
module UniqueWithinType
Expand All @@ -25,7 +27,8 @@ def encode(type_name, object_value, separator: self.default_id_separator)
# @param node_id [String] A unique ID generated by {.encode}
# @return [Array<(String, String)>] The type name & value passed to {.encode}
def decode(node_id, separator: self.default_id_separator)
Base64.decode64(node_id).split(separator, 2)
# urlsafe_decode64 is for forward compatibility
Base64Bp.urlsafe_decode64(node_id).split(separator, 2)
end
end
end
Expand Down

0 comments on commit 0f0eef8

Please sign in to comment.