Skip to content

Commit 6c206ed

Browse files
authored
🔀 Merge pull request #209 from oauth-xx/issue/156-fix-unsafe-comparison
Issue/156 fix unsafe comparison
2 parents 33bef12 + 9839886 commit 6c206ed

File tree

4 files changed

+36
-34
lines changed

4 files changed

+36
-34
lines changed

.rubocop_todo.yml

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# This configuration was generated by
22
# `rubocop --auto-gen-config`
3-
# on 2021-10-31 17:21:56 UTC using RuboCop version 1.22.3.
3+
# on 2021-10-31 19:10:34 UTC using RuboCop version 1.22.3.
44
# The point is for the user to remove these configuration records
55
# one by one as the offenses are removed from the code base.
66
# Note that changes in the inspected code, or installation of new
@@ -35,13 +35,12 @@ Layout/AccessModifierIndentation:
3535
- 'lib/oauth/tokens/request_token.rb'
3636
- 'test/cases/spec/1_0-final/test_parameter_encodings.rb'
3737

38-
# Offense count: 16
38+
# Offense count: 12
3939
# Cop supports --auto-correct.
4040
# Configuration parameters: EnforcedStyle, IndentationWidth.
4141
# SupportedStyles: with_first_argument, with_fixed_indentation
4242
Layout/ArgumentAlignment:
4343
Exclude:
44-
- 'lib/oauth/consumer.rb'
4544
- 'lib/oauth/server.rb'
4645
- 'test/units/test_em_http_request_proxy.rb'
4746
- 'test/units/test_rest_client_request_proxy.rb'
@@ -317,7 +316,7 @@ Layout/MultilineOperationIndentation:
317316
Exclude:
318317
- 'lib/oauth/consumer.rb'
319318

320-
# Offense count: 202
319+
# Offense count: 183
321320
# Cop supports --auto-correct.
322321
Layout/SpaceAfterComma:
323322
Enabled: false
@@ -452,13 +451,12 @@ Layout/TrailingWhitespace:
452451
Exclude:
453452
- 'lib/oauth/request_proxy/rest_client_request.rb'
454453

455-
# Offense count: 7
454+
# Offense count: 6
456455
# Cop supports --auto-correct.
457456
Lint/AmbiguousOperatorPrecedence:
458457
Exclude:
459458
- 'lib/oauth/cli/sign_command.rb'
460459
- 'lib/oauth/consumer.rb'
461-
- 'test/test_helper.rb'
462460

463461
# Offense count: 2
464462
# Configuration parameters: AllowSafeAssignment.
@@ -567,7 +565,7 @@ Metrics/AbcSize:
567565
# Offense count: 9
568566
# Configuration parameters: CountComments, CountAsOne.
569567
Metrics/ClassLength:
570-
Max: 274
568+
Max: 277
571569

572570
# Offense count: 7
573571
# Configuration parameters: IgnoredMethods.
@@ -1196,15 +1194,14 @@ Style/StderrPuts:
11961194
Exclude:
11971195
- 'lib/oauth/request_proxy/base.rb'
11981196

1199-
# Offense count: 17
1197+
# Offense count: 16
12001198
# Cop supports --auto-correct.
12011199
# Configuration parameters: Mode.
12021200
Style/StringConcatenation:
12031201
Exclude:
12041202
- 'lib/oauth/cli/sign_command.rb'
12051203
- 'lib/oauth/client/net_http.rb'
12061204
- 'test/integration/consumer_test.rb'
1207-
- 'test/test_helper.rb'
12081205
- 'test/units/test_net_http_client.rb'
12091206
- 'test/units/test_rsa_sha1.rb'
12101207

lib/oauth/consumer.rb

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -157,11 +157,14 @@ def get_request_token(request_options = {}, *arguments, &block)
157157
request_options[:oauth_callback] ||= OAuth::OUT_OF_BAND unless request_options[:exclude_callback]
158158

159159
if block_given?
160-
response = token_request(http_method,
161-
(request_token_url? ? request_token_url : request_token_path),
162-
nil,
163-
request_options,
164-
*arguments, &block)
160+
response = token_request(
161+
http_method,
162+
(request_token_url? ? request_token_url : request_token_path),
163+
nil,
164+
request_options,
165+
*arguments,
166+
&block
167+
)
165168
else
166169
response = token_request(http_method, (request_token_url? ? request_token_url : request_token_path), nil, request_options, *arguments)
167170
end

lib/oauth/signature/base.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,9 @@ def signature
5151
end
5252

5353
def ==(cmp_signature)
54-
signature == cmp_signature
54+
check = signature.bytesize ^ cmp_signature.bytesize
55+
signature.bytes.zip(cmp_signature.bytes) { |x, y| check |= x ^ y.to_i }
56+
check.zero?
5557
end
5658

5759
def verify

test/integration/consumer_test.rb

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ def test_step_by_step_token_request
138138
assert_equal "GET", request.method
139139
assert_nil request.body
140140
response=@consumer.http.request(request)
141-
assert_equal "200",response.code
141+
assert_equal "200", response.code
142142
assert_equal "oauth_token=requestkey&oauth_token_secret=requestsecret",response.body
143143
end
144144

@@ -163,24 +163,24 @@ def test_get_token_sequence
163163

164164
@request_token=@consumer.get_request_token
165165
assert @request_token
166-
assert_equal "requestkey",@request_token.token
167-
assert_equal "requestsecret",@request_token.secret
166+
assert_equal "requestkey", @request_token.token
167+
assert_equal "requestsecret", @request_token.secret
168168
assert_equal "http://term.ie/oauth/example/authorize.php?oauth_token=requestkey",@request_token.authorize_url
169169

170170
@access_token=@request_token.get_access_token
171171
assert @access_token
172-
assert_equal "accesskey",@access_token.token
173-
assert_equal "accesssecret",@access_token.secret
172+
assert_equal "accesskey", @access_token.token
173+
assert_equal "accesssecret", @access_token.secret
174174

175175
@response=@access_token.get("/oauth/example/echo_api.php?ok=hello&test=this")
176176
assert @response
177-
assert_equal "200",@response.code
178-
assert_equal( "ok=hello&test=this",@response.body)
177+
assert_equal "200", @response.code
178+
assert_equal( "ok=hello&test=this", @response.body)
179179

180180
@response=@access_token.post("/oauth/example/echo_api.php",{"ok"=>"hello","test"=>"this"})
181181
assert @response
182-
assert_equal "200",@response.code
183-
assert_equal( "ok=hello&test=this",@response.body)
182+
assert_equal "200", @response.code
183+
assert_equal( "ok=hello&test=this", @response.body)
184184
end
185185

186186
def test_get_token_sequence_using_fqdn
@@ -195,33 +195,33 @@ def test_get_token_sequence_using_fqdn
195195
:access_token_url=>"http://term.ie/oauth/example/access_token.php",
196196
:authorize_url=>"http://term.ie/oauth/example/authorize.php"
197197
})
198-
assert_equal "http://term.ie/oauth/example/request_token.php",@consumer.request_token_url
199-
assert_equal "http://term.ie/oauth/example/access_token.php",@consumer.access_token_url
198+
assert_equal "http://term.ie/oauth/example/request_token.php", @consumer.request_token_url
199+
assert_equal "http://term.ie/oauth/example/access_token.php", @consumer.access_token_url
200200

201201
assert @consumer.request_token_url?, "Should use fully qualified request token url"
202202
assert @consumer.access_token_url?, "Should use fully qualified access token url"
203203
assert @consumer.authorize_url?, "Should use fully qualified url"
204204

205205
@request_token=@consumer.get_request_token
206206
assert @request_token
207-
assert_equal "requestkey",@request_token.token
208-
assert_equal "requestsecret",@request_token.secret
209-
assert_equal "http://term.ie/oauth/example/authorize.php?oauth_token=requestkey",@request_token.authorize_url
207+
assert_equal "requestkey", @request_token.token
208+
assert_equal "requestsecret", @request_token.secret
209+
assert_equal "http://term.ie/oauth/example/authorize.php?oauth_token=requestkey", @request_token.authorize_url
210210

211211
@access_token=@request_token.get_access_token
212212
assert @access_token
213-
assert_equal "accesskey",@access_token.token
214-
assert_equal "accesssecret",@access_token.secret
213+
assert_equal "accesskey", @access_token.token
214+
assert_equal "accesssecret", @access_token.secret
215215

216216
@response=@access_token.get("/oauth/example/echo_api.php?ok=hello&test=this")
217217
assert @response
218-
assert_equal "200",@response.code
219-
assert_equal( "ok=hello&test=this",@response.body)
218+
assert_equal "200", @response.code
219+
assert_equal( "ok=hello&test=this", @response.body)
220220

221221
@response=@access_token.post("/oauth/example/echo_api.php",{"ok"=>"hello","test"=>"this"})
222222
assert @response
223223
assert_equal "200",@response.code
224-
assert_equal( "ok=hello&test=this",@response.body)
224+
assert_equal( "ok=hello&test=this", @response.body)
225225
end
226226

227227

0 commit comments

Comments
 (0)