X Tutup
Skip to content

Commit 6d84658

Browse files
committed
add checks to verify key is set and not empty string for getting and deleting files
1 parent 8db6e49 commit 6d84658

File tree

3 files changed

+59
-4
lines changed

3 files changed

+59
-4
lines changed

cloud_storage/google.lua

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -287,15 +287,19 @@ do
287287
return self:_get("/" .. tostring(bucket))
288288
end,
289289
get_file = function(self, bucket, key, opts)
290+
assert(type(key) == "string" and key ~= "", "Invalid key (missing or empty string)")
290291
return self:_get("/" .. tostring(bucket) .. "/" .. tostring(url.escape(key)), nil, opts and opts.headers)
291292
end,
292293
delete_file = function(self, bucket, key)
294+
assert(type(key) == "string" and key ~= "", "Invalid key for deletion (missing or empty string)")
293295
return self:_delete("/" .. tostring(bucket) .. "/" .. tostring(url.escape(key)))
294296
end,
295297
head_file = function(self, bucket, key)
298+
assert(type(key) == "string" and key ~= "", "Invalid key (missing or empty string)")
296299
return self:_head("/" .. tostring(bucket) .. "/" .. tostring(url.escape(key)))
297300
end,
298301
put_file_acl = function(self, bucket, key, acl)
302+
assert(type(key) == "string" and key ~= "", "Invalid key (missing or empty string)")
299303
return self:_put("/" .. tostring(bucket) .. "/" .. tostring(url.escape(key)) .. "?acl", "", {
300304
["Content-length"] = 0,
301305
["x-goog-acl"] = acl
@@ -309,7 +313,7 @@ do
309313
if type(data) == "table" then
310314
error("put_file_string interface has changed: key is now the second argument")
311315
end
312-
assert(key, "missing key")
316+
assert(type(key) == "string" and key ~= "", "Invalid key (missing or empty string)")
313317
assert(type(data) == "string", "expected string for data")
314318
return self:_put("/" .. tostring(bucket) .. "/" .. tostring(key), data, extend({
315319
["Content-length"] = #data,

cloud_storage/google.moon

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -182,13 +182,20 @@ class CloudStorage
182182
get_bucket: (bucket) => @_get "/#{bucket}"
183183

184184
get_file: (bucket, key, opts) =>
185+
assert type(key) == "string" and key != "", "Invalid key (missing or empty string)"
185186
@_get "/#{bucket}/#{url.escape key}", nil, opts and opts.headers
186187

187-
delete_file: (bucket, key) => @_delete "/#{bucket}/#{url.escape key}"
188-
head_file: (bucket, key) => @_head "/#{bucket}/#{url.escape key}"
188+
delete_file: (bucket, key) =>
189+
assert type(key) == "string" and key != "", "Invalid key for deletion (missing or empty string)"
190+
@_delete "/#{bucket}/#{url.escape key}"
191+
192+
head_file: (bucket, key) =>
193+
assert type(key) == "string" and key != "", "Invalid key (missing or empty string)"
194+
@_head "/#{bucket}/#{url.escape key}"
189195

190196
-- sets predefined acl
191197
put_file_acl: (bucket, key, acl) =>
198+
assert type(key) == "string" and key != "", "Invalid key (missing or empty string)"
192199
@_put "/#{bucket}/#{url.escape key}?acl", "", {
193200
"Content-length": 0
194201
"x-goog-acl": acl
@@ -199,7 +206,7 @@ class CloudStorage
199206
if type(data) == "table"
200207
error "put_file_string interface has changed: key is now the second argument"
201208

202-
assert key, "missing key"
209+
assert type(key) == "string" and key != "", "Invalid key (missing or empty string)"
203210
assert type(data) == "string", "expected string for data"
204211

205212
@_put "/#{bucket}/#{key}", data, extend {

spec/cloud_storage_spec.moon

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,20 @@ describe "cloud_storage", ->
169169
}
170170
}, http_requests
171171

172+
it "get_file fails with empty key", ->
173+
assert.has_error(
174+
-> storage\get_file "mybucket", ""
175+
"Invalid key (missing or empty string)"
176+
)
177+
assert.same { }, http_requests
178+
179+
it "get_file fails with missing key", ->
180+
assert.has_error(
181+
-> storage\get_file "mybucket"
182+
"Invalid key (missing or empty string)"
183+
)
184+
assert.same { }, http_requests
185+
172186
it "copy_file", ->
173187
storage\copy_file "from_bucket", "input/a.txt", "to_bucket", "output/b.txt", {
174188
acl: "private"
@@ -189,6 +203,36 @@ describe "cloud_storage", ->
189203
}
190204
}, http_requests
191205

206+
it "delete_file", ->
207+
storage\delete_file "mybucket", "source/my-pic..png"
208+
assert.same {
209+
{
210+
url: "https://storage.googleapis.com/mybucket/source%2fmy%2dpic%2e%2epng"
211+
method: "DELETE"
212+
headers: {
213+
"x-goog-api-version": 2
214+
"x-goog-project-id": "111111111111"
215+
Authorization: "OAuth my-fake-access-token"
216+
}
217+
}
218+
}, http_requests
219+
220+
it "delete_file fails with empty string key", ->
221+
assert.has_error(
222+
-> storage\delete_file "mybucket", ""
223+
"Invalid key for deletion (missing or empty string)"
224+
)
225+
226+
assert.same {}, http_requests
227+
228+
it "delete_file fails with missing string key", ->
229+
assert.has_error(
230+
-> storage\delete_file "mybucket"
231+
"Invalid key for deletion (missing or empty string)"
232+
)
233+
234+
assert.same {}, http_requests
235+
192236
describe "with storage from json key", ->
193237
local storage
194238

0 commit comments

Comments
 (0)
X Tutup