boxr 1.24.0 → 1.24.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 61cea90fb968b5080ce842d650aafd74224bdc6caf7cbe11e168aa0f3ef7c4c3
4
- data.tar.gz: 9de8543da3e1029de8ea4d9c0c346cfd6a05563a8119f7a3a81356bafd7a6831
3
+ metadata.gz: cd53bf1526acf4b05dcf83b888767dea4a2f6f3d9bcb9147b9a10778e5f0d859
4
+ data.tar.gz: ceab9581e73753f3f7125c9aaf3b811877c47c1128a17c50afa989966f86e8a3
5
5
  SHA512:
6
- metadata.gz: 4b16ccaec58eaa114cfb7c41f62e10cc05e8d07c45ce3e73c4e03d5ebf9407e022cb381cde5ceab0c88ace21a1d3b27b3564cf3b3b49be6fd8a3955e4006f771
7
- data.tar.gz: c3eb8f6e86e09ceffd43f6a6eccd6d38f1dac13b32a924fc3ef588168e0dc2d76672bc982a0f2036a362db1bc75431c9df0b851fe41b6040016689b0c2fb9330
6
+ metadata.gz: fa545cd50ffcb06f1afc54453b79db053e38e8646badf6a66c9ab8b13658c178b4c44a07ffeb6aec1905931be9e5a75473115f8742741e31877eb00a51137179
7
+ data.tar.gz: ddb3b721b49af57b392923c272b397732a27ed4426c5acaed858a6f0e525c173ea600d271912d265027c70217ae54bb5516d2fcc8c716ba7cfab77cf37c78521
data/.env.example CHANGED
@@ -17,7 +17,5 @@ BOX_CLIENT_SECRET={client secret of your Box app}
17
17
  BOX_ENTERPRISE_ID={box enterprise id}
18
18
  JWT_PRIVATE_KEY_PATH={path to your JWT private key}
19
19
  JWT_PRIVATE_KEY_PASSWORD={JWT private key password}
20
- BOX_PRIMARY_SIGNATURE_KEY={primary key for webhooks}
21
- BOX_SECONDARY_SIGNATURE_KEY={secondary key for webhooks}
22
20
  BOX_OAUTH_CLIENT_ID={client_id for your server authentication app}
23
21
  BOX_OAUTH_CLIENT_SECRET={client_secret for your server authentication app}
@@ -38,6 +38,7 @@ module Boxr
38
38
  end
39
39
 
40
40
  def chunked_upload_get_session(session_id)
41
+ session_id = ensure_path_segment(session_id)
41
42
  uri = "#{UPLOAD_URI}/files/upload_sessions/#{session_id}"
42
43
  session_info, = get(uri)
43
44
  session_info
@@ -50,6 +51,7 @@ module Boxr
50
51
  end
51
52
 
52
53
  def chunked_upload_part_from_io(io, session_id, content_range) # rubocop:disable Metrics
54
+ session_id = ensure_path_segment(session_id)
53
55
  io.pos = content_range.min
54
56
  part_size = content_range.max - content_range.min + 1
55
57
  body = io.read(part_size)
@@ -67,6 +69,7 @@ module Boxr
67
69
  end
68
70
 
69
71
  def chunked_upload_list_parts(session_id, limit: nil, offset: nil)
72
+ session_id = ensure_path_segment(session_id)
70
73
  uri = "#{UPLOAD_URI}/files/upload_sessions/#{session_id}/parts"
71
74
  query = {}
72
75
  query[:limit] = limit unless limit.nil?
@@ -88,6 +91,7 @@ module Boxr
88
91
 
89
92
  def chunked_upload_commit_from_io(io, session_id, parts, content_created_at: nil, # rubocop:disable Metrics
90
93
  content_modified_at: nil, if_match: nil, if_non_match: nil)
94
+ session_id = ensure_path_segment(session_id)
91
95
  io.pos = 0
92
96
  cca = content_created_at
93
97
  cma = content_modified_at
@@ -113,6 +117,7 @@ module Boxr
113
117
  end
114
118
 
115
119
  def chunked_upload_abort_session(session_id)
120
+ session_id = ensure_path_segment(session_id)
116
121
  uri = "#{UPLOAD_URI}/files/upload_sessions/#{session_id}"
117
122
  abort_info, = delete(uri)
118
123
  abort_info
data/lib/boxr/client.rb CHANGED
@@ -94,7 +94,7 @@ module Boxr
94
94
  @jwt_private_key_password = jwt_private_key_password
95
95
  @jwt_public_key_id = jwt_public_key_id
96
96
  @identifier = identifier
97
- @as_user_id = ensure_id(as_user)
97
+ @as_user_id = ensure_id(as_user.is_a?(String) ? as_user.strip : as_user)
98
98
  @token_refresh_listener = token_refresh_listener
99
99
  BOX_CLIENT.proxy = proxy unless proxy.nil?
100
100
  end
@@ -111,7 +111,8 @@ module Boxr
111
111
  headers['BoxApi'] = box_api_header unless box_api_header.nil?
112
112
  headers.merge!(extra_headers) unless extra_headers.nil?
113
113
 
114
- BOX_CLIENT.get(uri, query: query, header: headers, follow_redirect: follow_redirect)
114
+ BOX_CLIENT.get(uri, query: query, header: validated_headers(headers),
115
+ follow_redirect: follow_redirect)
115
116
  end
116
117
 
117
118
  check_response_status(res, success_codes)
@@ -133,14 +134,20 @@ module Boxr
133
134
  query[:offset] = offset
134
135
  res = with_auto_token_refresh do
135
136
  headers = standard_headers
136
- BOX_CLIENT.get(uri, query: query, header: headers, follow_redirect: follow_redirect)
137
+ BOX_CLIENT.get(uri, query: query, header: validated_headers(headers),
138
+ follow_redirect: follow_redirect)
137
139
  end
138
140
 
139
141
  unless res.status == 200
140
142
  raise BoxrError.new(status: res.status, body: res.body, header: res.header)
141
143
  end
142
144
 
143
- body_json = JSON.load(res.body) # rubocop:disable Security/JSONLoad
145
+ body_json = parse_response_body(res.body)
146
+ if body_json.nil?
147
+ raise BoxrError.new(status: res.status, body: res.body, header: res.header,
148
+ boxr_message: 'Blank response body from paginated endpoint')
149
+ end
150
+
144
151
  total_count = body_json['total_count']
145
152
  offset += limit
146
153
 
@@ -166,7 +173,7 @@ module Boxr
166
173
  headers['Digest'] = digest unless digest.nil?
167
174
  headers.merge!(extra_headers) unless extra_headers.nil?
168
175
 
169
- BOX_CLIENT.post(uri, body: body, query: query, header: headers)
176
+ BOX_CLIENT.post(uri, body: body, query: query, header: validated_headers(headers))
170
177
  end
171
178
 
172
179
  check_response_status(res, success_codes)
@@ -187,7 +194,7 @@ module Boxr
187
194
  headers['Digest'] = digest unless digest.nil?
188
195
  headers.merge!(extra_headers) unless extra_headers.nil?
189
196
 
190
- BOX_CLIENT.put(uri, body: body, query: query, header: headers)
197
+ BOX_CLIENT.put(uri, body: body, query: query, header: validated_headers(headers))
191
198
  end
192
199
 
193
200
  check_response_status(res, success_codes)
@@ -203,7 +210,7 @@ module Boxr
203
210
  headers['If-Match'] = if_match unless if_match.nil?
204
211
  headers.merge!(extra_headers) unless extra_headers.nil?
205
212
 
206
- BOX_CLIENT.delete(uri, query: query, header: headers)
213
+ BOX_CLIENT.delete(uri, query: query, header: validated_headers(headers))
207
214
  end
208
215
 
209
216
  check_response_status(res, success_codes)
@@ -216,7 +223,7 @@ module Boxr
216
223
 
217
224
  res = with_auto_token_refresh do
218
225
  headers = standard_headers
219
- BOX_CLIENT.options(uri, body: JSON.dump(body), header: headers)
226
+ BOX_CLIENT.options(uri, body: JSON.dump(body), header: validated_headers(headers))
220
227
  end
221
228
 
222
229
  check_response_status(res, success_codes)
@@ -230,6 +237,15 @@ module Boxr
230
237
  headers
231
238
  end
232
239
 
240
+ def validated_headers(headers)
241
+ name, = headers.find do |_, value|
242
+ Array(value).flatten.any? { |part| part.to_s.match?(/[\r\n\0]/) }
243
+ end
244
+ raise BoxrError.new(boxr_message: "Invalid characters in #{name} header") unless name.nil?
245
+
246
+ headers
247
+ end
248
+
233
249
  def with_auto_token_refresh
234
250
  return yield unless @refresh_token || @jwt_private_key
235
251
 
@@ -270,10 +286,19 @@ module Boxr
270
286
  end
271
287
 
272
288
  def processed_response(res)
273
- body_json = JSON.load(res.body) # rubocop:disable Security/JSONLoad
289
+ body_json = parse_response_body(res.body)
274
290
  [BoxrMash.new(body_json), res]
275
291
  end
276
292
 
293
+ # Parses a JSON response body. Returns nil for a blank body (the Box API
294
+ # returns empty bodies for some responses), matching the prior leniency of
295
+ # JSON.load while avoiding its unsafe object deserialization (create_additions).
296
+ def parse_response_body(body)
297
+ return nil if body.to_s.empty?
298
+
299
+ JSON.parse(body)
300
+ end
301
+
277
302
  def build_fields_query(fields, all_fields_query)
278
303
  if fields == :all
279
304
  { fields: all_fields_query }
@@ -301,18 +326,30 @@ module Boxr
301
326
  def ensure_id(item)
302
327
  # Ruby 2.4 unified Fixnum and Bignum into Integer. This tests for Ruby 2.4
303
328
  if 1.instance_of?(Integer)
304
- return item if item.instance_of?(String) || item.instance_of?(Integer) || item.nil?
329
+ return ensure_path_segment(item) if item.instance_of?(String) ||
330
+ item.instance_of?(Integer) || item.nil?
305
331
  elsif item.instance_of?(String) || item.instance_of?(Integer) || item.instance_of?(Integer) || item.nil?
306
332
  if item.instance_of?(String) || item.instance_of?(Integer) || item.instance_of?(Integer) || item.nil?
307
- return item
333
+ return ensure_path_segment(item)
308
334
  end
309
335
  end
310
336
 
311
- return item.id if item.respond_to?(:id)
337
+ return ensure_path_segment(item.id) if item.respond_to?(:id)
312
338
 
313
339
  raise BoxrError.new(boxr_message: 'Expecting an id of class String or Fixnum, or object that responds to :id')
314
340
  end
315
341
 
342
+ def ensure_path_segment(value)
343
+ case value
344
+ when nil, Integer
345
+ return value
346
+ when String, Symbol
347
+ return value if value.match?(/\A[0-9A-Za-z_-]+\z/)
348
+ end
349
+
350
+ raise BoxrError.new(boxr_message: "Invalid ID or path segment: #{value.inspect}")
351
+ end
352
+
316
353
  def restore_trashed_item(uri, name, parent)
317
354
  parent_id = ensure_id(parent)
318
355
 
data/lib/boxr/metadata.rb CHANGED
@@ -1,9 +1,11 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Boxr
4
- class Client
4
+ class Client # rubocop:disable Metrics/ClassLength
5
5
  def create_metadata(file, metadata, scope: :global, template: :properties)
6
6
  file_id = ensure_id(file)
7
+ scope = ensure_path_segment(scope)
8
+ template = ensure_path_segment(template)
7
9
  uri = "#{FILE_METADATA_URI}/#{file_id}/metadata/#{scope}/#{template}"
8
10
  metadata, = post(uri, metadata, content_type: 'application/json')
9
11
  metadata
@@ -11,6 +13,8 @@ module Boxr
11
13
 
12
14
  def create_folder_metadata(folder, metadata, scope, template)
13
15
  folder_id = ensure_id(folder)
16
+ scope = ensure_path_segment(scope)
17
+ template = ensure_path_segment(template)
14
18
  uri = "#{FOLDER_METADATA_URI}/#{folder_id}/metadata/#{scope}/#{template}"
15
19
  metadata, = post(uri, metadata, content_type: 'application/json')
16
20
  metadata
@@ -18,6 +22,8 @@ module Boxr
18
22
 
19
23
  def metadata(file, scope: :global, template: :properties)
20
24
  file_id = ensure_id(file)
25
+ scope = ensure_path_segment(scope)
26
+ template = ensure_path_segment(template)
21
27
  uri = "#{FILE_METADATA_URI}/#{file_id}/metadata/#{scope}/#{template}"
22
28
  metadata, = get(uri)
23
29
  metadata
@@ -25,6 +31,8 @@ module Boxr
25
31
 
26
32
  def folder_metadata(folder, scope, template)
27
33
  folder_id = ensure_id(folder)
34
+ scope = ensure_path_segment(scope)
35
+ template = ensure_path_segment(template)
28
36
  uri = "#{FOLDER_METADATA_URI}/#{folder_id}/metadata/#{scope}/#{template}"
29
37
  metadata, = get(uri)
30
38
  metadata
@@ -46,6 +54,8 @@ module Boxr
46
54
 
47
55
  def update_metadata(file, updates, scope: :global, template: :properties)
48
56
  file_id = ensure_id(file)
57
+ scope = ensure_path_segment(scope)
58
+ template = ensure_path_segment(template)
49
59
  uri = "#{FILE_METADATA_URI}/#{file_id}/metadata/#{scope}/#{template}"
50
60
 
51
61
  # in the event just one update is specified ensure that it is packaged inside an array
@@ -57,6 +67,8 @@ module Boxr
57
67
 
58
68
  def update_folder_metadata(folder, updates, scope, template)
59
69
  folder_id = ensure_id(folder)
70
+ scope = ensure_path_segment(scope)
71
+ template = ensure_path_segment(template)
60
72
  uri = "#{FOLDER_METADATA_URI}/#{folder_id}/metadata/#{scope}/#{template}"
61
73
 
62
74
  # in the event just one update is specified ensure that it is packaged inside an array
@@ -68,6 +80,8 @@ module Boxr
68
80
 
69
81
  def delete_metadata(file, scope: :global, template: :properties)
70
82
  file_id = ensure_id(file)
83
+ scope = ensure_path_segment(scope)
84
+ template = ensure_path_segment(template)
71
85
  uri = "#{FILE_METADATA_URI}/#{file_id}/metadata/#{scope}/#{template}"
72
86
  result, = delete(uri)
73
87
  result
@@ -75,6 +89,8 @@ module Boxr
75
89
 
76
90
  def delete_folder_metadata(folder, scope, template)
77
91
  folder_id = ensure_id(folder)
92
+ scope = ensure_path_segment(scope)
93
+ template = ensure_path_segment(template)
78
94
  uri = "#{FOLDER_METADATA_URI}/#{folder_id}/metadata/#{scope}/#{template}"
79
95
  result, = delete(uri)
80
96
  result
@@ -88,6 +104,8 @@ module Boxr
88
104
  alias get_enterprise_templates enterprise_metadata
89
105
 
90
106
  def metadata_schema(scope, template_key)
107
+ scope = ensure_path_segment(scope)
108
+ template_key = ensure_path_segment(template_key)
91
109
  uri = "#{METADATA_TEMPLATES_URI}/#{scope}/#{template_key}/schema"
92
110
  schema, = get(uri)
93
111
  schema
@@ -116,6 +134,8 @@ module Boxr
116
134
  end
117
135
 
118
136
  def delete_metadata_template(scope, template_key)
137
+ scope = ensure_path_segment(scope)
138
+ template_key = ensure_path_segment(template_key)
119
139
  uri = "#{METADATA_TEMPLATES_URI}/#{scope}/#{template_key}/schema"
120
140
  result, = delete(uri)
121
141
  result
data/lib/boxr/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Boxr
4
- VERSION = '1.24.0'
4
+ VERSION = '1.24.1'
5
5
  end
@@ -31,7 +31,8 @@ module Boxr
31
31
  end
32
32
 
33
33
  def verify_signature
34
- generate_signature(primary_signature_key) == primary_signature || generate_signature(secondary_signature_key) == secondary_signature
34
+ signature_valid?(primary_signature_key, primary_signature) ||
35
+ signature_valid?(secondary_signature_key, secondary_signature)
35
36
  end
36
37
 
37
38
  def generate_signature(key)
@@ -54,5 +55,17 @@ module Boxr
54
55
  def message_age
55
56
  current_time - delivery_time
56
57
  end
58
+
59
+ def secure_compare(expected, actual)
60
+ return false unless expected.bytesize == actual.bytesize
61
+
62
+ expected.bytes.zip(actual.bytes).reduce(0) { |result, (x, y)| result | (x ^ y) }.zero?
63
+ end
64
+
65
+ def signature_valid?(key, signature)
66
+ return false if key.empty? || !signature.is_a?(String)
67
+
68
+ secure_compare(generate_signature(key), signature)
69
+ end
57
70
  end
58
71
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: boxr
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.24.0
4
+ version: 1.24.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Chad Burnette
@@ -332,7 +332,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
332
332
  - !ruby/object:Gem::Version
333
333
  version: '0'
334
334
  requirements: []
335
- rubygems_version: 4.0.3
335
+ rubygems_version: 3.6.9
336
336
  specification_version: 4
337
337
  summary: A Ruby client library for the Box V2 Content API.
338
338
  test_files: []