Skip to content

Commit b495b4e

Browse files
Merge pull request rails#45550 from jonathanhefner/rename-urlsafe-to-url_safe
Rename `:urlsafe` option to `:url_safe`
2 parents 6f20be9 + 7094d0f commit b495b4e

File tree

5 files changed

+30
-30
lines changed

5 files changed

+30
-30
lines changed

activesupport/CHANGELOG.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,22 @@
88

99
*Ryo Nakamura*
1010

11-
* Support `:urlsafe` option for `MessageEncryptor`.
11+
* Support `:url_safe` option for `MessageEncryptor`.
1212

13-
The `MessageEncryptor` constructor now accepts a `:urlsafe` option, similar
13+
The `MessageEncryptor` constructor now accepts a `:url_safe` option, similar
1414
to the `MessageVerifier` constructor. When enabled, this option ensures
1515
that messages use a URL-safe encoding.
1616

1717
*Jonathan Hefner*
1818

19-
* Add `urlsafe` option to `ActiveSupport::MessageVerifier` initializer
19+
* Add `url_safe` option to `ActiveSupport::MessageVerifier` initializer
2020

21-
`ActiveSupport::MessageVerifier.new` now takes optional `urlsafe` argument.
22-
It can generate urlsafe strings by passing `urlsafe: true`.
21+
`ActiveSupport::MessageVerifier.new` now takes optional `url_safe` argument.
22+
It can generate URL-safe strings by passing `url_safe: true`.
2323

2424
```ruby
25-
verifier = ActiveSupport::MessageVerifier.new(urlsafe: true)
26-
message = verifier.generate(data) # => "urlsafe_string"
25+
verifier = ActiveSupport::MessageVerifier.new(url_safe: true)
26+
message = verifier.generate(data) # => URL-safe string
2727
```
2828

2929
This option is `false` by default to be backwards compatible.

activesupport/lib/active_support/message_encryptor.rb

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -143,9 +143,9 @@ class InvalidMessage < StandardError; end
143143
# * <tt>:digest</tt> - String of digest to use for signing. Default is
144144
# +SHA1+. Ignored when using an AEAD cipher like 'aes-256-gcm'.
145145
# * <tt>:serializer</tt> - Object serializer to use. Default is +JSON+.
146-
# * <tt>:urlsafe</tt> - Whether to encode messages using a URL-safe
146+
# * <tt>:url_safe</tt> - Whether to encode messages using a URL-safe
147147
# encoding. Default is +false+ for backward compatibility.
148-
def initialize(secret, sign_secret = nil, cipher: nil, digest: nil, serializer: nil, urlsafe: false)
148+
def initialize(secret, sign_secret = nil, cipher: nil, digest: nil, serializer: nil, url_safe: false)
149149
@secret = secret
150150
@sign_secret = sign_secret
151151
@cipher = cipher || self.class.default_cipher
@@ -159,7 +159,7 @@ def initialize(secret, sign_secret = nil, cipher: nil, digest: nil, serializer:
159159
elsif @@default_message_encryptor_serializer.equal?(:json)
160160
JSON
161161
end
162-
@urlsafe = urlsafe
162+
@url_safe = url_safe
163163
@verifier = resolve_verifier
164164
end
165165

@@ -190,11 +190,11 @@ def deserialize(value)
190190
end
191191

192192
def encode(data)
193-
@urlsafe ? ::Base64.urlsafe_encode64(data, padding: false) : ::Base64.strict_encode64(data)
193+
@url_safe ? ::Base64.urlsafe_encode64(data, padding: false) : ::Base64.strict_encode64(data)
194194
end
195195

196196
def decode(data)
197-
@urlsafe ? ::Base64.urlsafe_decode64(data) : ::Base64.strict_decode64(data)
197+
@url_safe ? ::Base64.urlsafe_decode64(data) : ::Base64.strict_decode64(data)
198198
end
199199

200200
def _encrypt(value, **metadata_options)
@@ -242,7 +242,7 @@ def _decrypt(encrypted_message, purpose)
242242
end
243243

244244
def length_after_encode(length_before_encode)
245-
if @urlsafe
245+
if @url_safe
246246
(4 * length_before_encode / 3.0).ceil # length without padding
247247
else
248248
4 * (length_before_encode / 3.0).ceil # length with padding
@@ -295,7 +295,7 @@ def resolve_verifier
295295
if aead_mode?
296296
NullVerifier
297297
else
298-
MessageVerifier.new(@sign_secret || @secret, digest: @digest, serializer: NullSerializer, urlsafe: @urlsafe)
298+
MessageVerifier.new(@sign_secret || @secret, digest: @digest, serializer: NullSerializer, url_safe: @url_safe)
299299
end
300300
end
301301
end

activesupport/lib/active_support/message_verifier.rb

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -108,16 +108,16 @@ module ActiveSupport
108108
#
109109
# verifier.rotate(old_secret, digest: "SHA256", serializer: Marshal)
110110
#
111-
# === Generating URL safe strings
111+
# === Generating URL-safe strings
112112
#
113113
# By default MessageVerifier generates RFC 4648 compliant strings which are
114-
# not URL safe. In other words, they can contain "+" and "/". If you want to
115-
# generate URL safe strings (in compliance with "Base 64 Encoding with URL and
116-
# Filename Safe Alphabet" in RFC 4648), you can pass <tt>urlsafe: true</tt>
114+
# not URL-safe. In other words, they can contain "+" and "/". If you want to
115+
# generate URL-safe strings (in compliance with "Base 64 Encoding with URL and
116+
# Filename Safe Alphabet" in RFC 4648), you can pass <tt>url_safe: true</tt>
117117
# to the constructor:
118118
#
119-
# @verifier = ActiveSupport::MessageVerifier.new("secret", urlsafe: true)
120-
# @verifier.generate("signed message") #=> "urlsafe_string"
119+
# @verifier = ActiveSupport::MessageVerifier.new("secret", url_safe: true)
120+
# @verifier.generate("signed message") #=> URL-safe string
121121
class MessageVerifier
122122
prepend Messages::Rotator::Verifier
123123

@@ -128,7 +128,7 @@ class InvalidSignature < StandardError; end
128128

129129
cattr_accessor :default_message_verifier_serializer, instance_accessor: false, default: :marshal
130130

131-
def initialize(secret, digest: nil, serializer: nil, urlsafe: false)
131+
def initialize(secret, digest: nil, serializer: nil, url_safe: false)
132132
raise ArgumentError, "Secret should not be nil." unless secret
133133
@secret = secret
134134
@digest = digest&.to_s || "SHA1"
@@ -140,7 +140,7 @@ def initialize(secret, digest: nil, serializer: nil, urlsafe: false)
140140
elsif @@default_message_verifier_serializer.equal?(:json)
141141
JSON
142142
end
143-
@urlsafe = urlsafe
143+
@url_safe = url_safe
144144
end
145145

146146
# Checks if a signed message could have been generated by signing an object
@@ -221,11 +221,11 @@ def generate(value, expires_at: nil, expires_in: nil, purpose: nil)
221221

222222
private
223223
def encode(data)
224-
@urlsafe ? Base64.urlsafe_encode64(data, padding: false) : Base64.strict_encode64(data)
224+
@url_safe ? Base64.urlsafe_encode64(data, padding: false) : Base64.strict_encode64(data)
225225
end
226226

227227
def decode(data)
228-
@urlsafe ? Base64.urlsafe_decode64(data) : Base64.strict_decode64(data)
228+
@url_safe ? Base64.urlsafe_decode64(data) : Base64.strict_decode64(data)
229229
end
230230

231231
def generate_digest(data)

activesupport/test/message_encryptor_test.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -84,11 +84,11 @@ def test_message_obeys_strict_encoding
8484
end
8585

8686
test "supports URL-safe encoding when using authenticated encryption" do
87-
encryptor = ActiveSupport::MessageEncryptor.new(@secret, urlsafe: true, cipher: "aes-256-gcm")
87+
encryptor = ActiveSupport::MessageEncryptor.new(@secret, url_safe: true, cipher: "aes-256-gcm")
8888

8989
# Because encrypted data appears random, we cannot control whether it will
9090
# contain bytes that _would_ be encoded as non-URL-safe characters (i.e. "+"
91-
# or "/") if `urlsafe: true` were broken. Therefore, to make our test
91+
# or "/") if `url_safe: true` were broken. Therefore, to make our test
9292
# falsifiable, we use a large string so that the encrypted data will almost
9393
# certainly contain such bytes.
9494
data = "x" * 10001
@@ -99,16 +99,16 @@ def test_message_obeys_strict_encoding
9999
end
100100

101101
test "supports URL-safe encoding when using unauthenticated encryption" do
102-
encryptor = ActiveSupport::MessageEncryptor.new(@secret, urlsafe: true, cipher: "aes-256-cbc")
102+
encryptor = ActiveSupport::MessageEncryptor.new(@secret, url_safe: true, cipher: "aes-256-cbc")
103103

104104
# When using unauthenticated encryption, messages are double encoded: once
105105
# when encrypting and once again when signing with a MessageVerifier. The
106106
# 1st encode eliminates the possibility of a 6-bit aligned occurrence of
107107
# `0b111110` or `0b111111`, which the 2nd encode _would_ map to a
108-
# non-URL-safe character (i.e. "+" or "/") if `urlsafe: true` were broken.
108+
# non-URL-safe character (i.e. "+" or "/") if `url_safe: true` were broken.
109109
# Therefore, to ensure our test is falsifiable, we also assert that the
110110
# message payload _would_ have padding characters (i.e. "=") if
111-
# `urlsafe: true` were broken.
111+
# `url_safe: true` were broken.
112112
data = 1
113113
message = encryptor.encrypt_and_sign(data)
114114

activesupport/test/message_verifier_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ def test_verify_exception_on_invalid_message
5050
end
5151

5252
test "supports URL-safe encoding" do
53-
verifier = ActiveSupport::MessageVerifier.new(@secret, urlsafe: true, serializer: JSON)
53+
verifier = ActiveSupport::MessageVerifier.new(@secret, url_safe: true, serializer: JSON)
5454

5555
# To verify that the message payload uses a URL-safe encoding (i.e. does not
5656
# use "+" or "/"), the unencoded bytes should have a 6-bit aligned

0 commit comments

Comments
 (0)