Skip to content
Merged
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
18 changes: 15 additions & 3 deletions packages/supabase_storage/lib/src/storage_file_api.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ import 'file_stub.dart' if (dart.library.io) './file_io.dart';

/// API namespace for managing the objects within a bucket, obtained with
/// `SupabaseStorageClient.from`.
///
/// A method that puts an object path in the request URL throws an
/// [ArgumentError] for a path with a `.` or `..` segment, since the URL would
/// resolve it to a route outside the bucket.
class StorageFileApi {
@internal
StorageFileApi(
Expand Down Expand Up @@ -57,9 +61,17 @@ class StorageFileApi {
// (for example a `?` being parsed as the start of the query string). `/`
// separators and characters that are already valid in a path segment
// (such as `:` in ISO-8601 timestamps) are preserved, so URLs for existing
// valid keys are unchanged.
final cleanPath = _removeEmptyFolders(path);
final encodedPath = Uri(pathSegments: cleanPath.split('/')).path;
// valid keys are unchanged. A `.` or `..` segment is rejected, since URL
// parsing would resolve it to a route outside the bucket.
final segments = _removeEmptyFolders(path).split('/');
if (segments.any((segment) => segment == '.' || segment == '..')) {
throw ArgumentError.value(
path,
'path',
'Must not contain a "." or ".." segment',
);
}
final encodedPath = Uri(pathSegments: segments).path;
return '$_encodedBucketId/$encodedPath';
}

Expand Down
59 changes: 59 additions & 0 deletions packages/supabase_storage/test/path_encoding_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,63 @@ void main() {
expect(url.pathSegments.last, 'report?v=2 final.pdf');
expect(url.path, contains('/object/public/'));
});

group('object paths with dot segments', () {
late MockSupabaseHttpClient mockClient;
late StorageFileApi bucket;

setUp(() {
mockClient = MockSupabaseHttpClient();
bucket = SupabaseStorageClient(
storageUrl,
headers,
httpClient: mockClient,
).from('uploads');
});

for (final path in ['../../rest/v1/rpc/f', 'a/./b', 'a/..', '/..//x']) {
test('$path is rejected before a request is sent', () async {
await expectLater(
() => bucket.uploadBinary(path, Uint8List(1)),
throwsArgumentError,
);
await expectLater(() => bucket.download(path), throwsArgumentError);
await expectLater(
() => bucket.createSignedUrl(path, 60),
throwsArgumentError,
);
await expectLater(
() => bucket.createSignedUploadUrl(path),
throwsArgumentError,
);
await expectLater(() => bucket.getMetadata(path), throwsArgumentError);
await expectLater(() => bucket.exists(path), throwsArgumentError);
await expectLater(() => bucket.purgeCache(path), throwsArgumentError);
expect(() => bucket.getPublicUrl(path), throwsArgumentError);
expect(mockClient.requests, isEmpty);
});
}

test('keys that only start with a dot are kept', () async {
mockClient.stub(Uint8List.fromList([1]));

await bucket.download('.hidden/..config/...');

expect(
mockClient.requests.single.url.path,
endsWith('/object/uploads/.hidden/..config/...'),
);
});

test('encoded dot segments stay part of the key', () async {
mockClient.stub(Uint8List.fromList([1]));

await bucket.download('%2e%2e/x');

expect(
mockClient.requests.single.url.path,
endsWith('/object/uploads/%252e%252e/x'),
);
});
});
}
Loading