-
Notifications
You must be signed in to change notification settings - Fork 2.8k
AmazonS3Client doesBucketExist() should throw an exception if bucket access is forbidden #1256
Description
I have an application that uploads data to S3, before uploading I check whether bucket exists in S3 or not, if it doesn't exist I throw an exception to notify the user to create one. To check if bucket exists I use doesBucketExist() of AmazonS3Client class. This check happens in the initialisation phase.
Today when I ran my application with incorrect AWSCredentials, to my surprise my application didn't fail in the initialisation phase rather it failed when my app tried to upload data to S3. Then I was wondering why it didn't fail in the initialisation phase where I check if bucket exists or not, then looking at the code of AmazonS3Client.doesBucketExist() I realised it was returning true when bucket access is forbidden or for a status code of 403 of AmazonServiceException
The issue here is that code is making an incorrect assumption that bucket always exists when a client is unable to access a bucket or if the access is forbidden. In my test I tried to check if a bucket named "nirabafasfanjan-str" exists with incorrect AWS credentials.
These are the buckets available in my region
As you can clearly see this is a false positive and in this case correct behaviour should be to throw exception rather than returning true when the client cannot ascertain bucket exists or not.

