Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 23 additions & 2 deletions packages/google-cloud-storage/google/cloud/storage/blob.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,10 @@ class Blob(_PropertyMixin):
the object in the bucket. If bytes, will be converted to a
unicode object. Blob / object names can contain any sequence
of valid unicode characters, of length 1-1024 bytes when
UTF-8 encoded.
UTF-8 encoded, and cannot be ``"."`` or ``".."``.

:raises: :class:`ValueError` if ``name`` is ``"."`` or ``".."`` (or their
``bytes`` equivalents).

:type bucket: :class:`google.cloud.storage.bucket.Bucket`
:param bucket: The bucket to which this blob belongs.
Expand Down Expand Up @@ -222,6 +225,7 @@ def __init__(
Get the blob's name.
"""
name = _bytes_to_unicode(name)
_validate_blob_name(name)
super(Blob, self).__init__(name=name)

self.chunk_size = chunk_size # Check that setter accepts value.
Expand Down Expand Up @@ -305,12 +309,16 @@ def path_helper(bucket_path, blob_name):
:type bucket_path: str
:param bucket_path: The URL path for a bucket.

:type blob_name: str
:type blob_name: str or bytes
:param blob_name: The name of the blob.

:rtype: str
:returns: The relative URL path for ``blob_name``.

:raises: :class:`ValueError` if ``blob_name`` is ``"."`` or ``".."``
(or their ``bytes`` equivalents).
"""
_validate_blob_name(blob_name)
return bucket_path + "/o/" + _quote(blob_name)

@property
Expand Down Expand Up @@ -5182,6 +5190,19 @@ def _get_encryption_headers(key, source=False):
}


def _validate_blob_name(name):
"""Validate a blob name.

:type name: str or bytes
:param name: The name of the blob.

:raises: :class:`ValueError` if ``name`` is ``"."`` or ``".."`` (or their
``bytes`` equivalents).
"""
if name in (".", "..", b".", b".."):
raise ValueError("Blob name cannot be '.' or '..'.")


def _quote(value, safe=b"~"):
"""URL-quote a string.

Expand Down
33 changes: 33 additions & 0 deletions packages/google-cloud-storage/tests/unit/test_blob.py
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,39 @@ def test_path_with_non_ascii(self):
blob = self._make_one(blob_name, bucket=bucket)
self.assertEqual(blob.path, "/b/name/o/Caf%C3%A9")

def test_ctor_rejects_dot_names(self):
bucket = _Bucket()
for name in (".", "..", b".", b".."):
with self.subTest(name=name):
with self.assertRaisesRegex(
ValueError, r"Blob name cannot be '\.' or '\.\.'\."
):
self._make_one(name, bucket=bucket)

def test_path_helper_rejects_dot_names(self):
from google.cloud.storage.blob import Blob

for name in (".", "..", b".", b".."):
with self.subTest(name=name):
with self.assertRaisesRegex(
ValueError, r"Blob name cannot be '\.' or '\.\.'\."
):
Blob.path_helper("/b/name", name)

def test_path_helper_valid_dot_names(self):
from google.cloud.storage.blob import Blob

cases = (
(".hidden", "/b/name/o/.hidden"),
("..foo", "/b/name/o/..foo"),
("foo.txt", "/b/name/o/foo.txt"),
("a/./b", "/b/name/o/a%2F.%2Fb"),
("a/../b", "/b/name/o/a%2F..%2Fb"),
)
for name, expected in cases:
with self.subTest(name=name):
self.assertEqual(Blob.path_helper("/b/name", name), expected)

def test_bucket_readonly_property(self):
blob_name = "BLOB"
bucket = _Bucket()
Expand Down
Loading