Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

[mobile] Bound HLS and memory-share metadata processing (#13212)

neeraj-pilot92d9355a07d4
diff --git a/mobile/apps/photos/lib/services/memory_share_service.dart b/mobile/apps/photos/lib/services/memory_share_service.dart
index 38f5c1c239..e42c1332f2 100644
--- a/mobile/apps/photos/lib/services/memory_share_service.dart
+++ b/mobile/apps/photos/lib/services/memory_share_service.dart
@@ -24,6 +24,7 @@ import 'package:photos/service_locator.dart' show entityService;
import 'package:photos/services/collections_service.dart';
import 'package:photos/utils/face_crop_util.dart';
import 'package:photos/utils/file_key.dart';
+import 'package:photos/utils/gzip.dart';
class MemoryShareService {
MemoryShareService._();
@@ -896,8 +897,12 @@ class MemoryShareService {
Uint8List decryptedMetadata,
MemoryShareType shareType,
) {
+ _validateMetadataSize(decryptedMetadata.length);
if (shareType == MemoryShareType.lane) {
- final decompressed = GZipCodec().decode(decryptedMetadata);
+ final decompressed = gunzipBytes(
+ decryptedMetadata,
+ maxOutputBytes: _maxMemoryShareMetadataBytes,
+ );
return jsonDecode(utf8.decode(decompressed));
}
return jsonDecode(utf8.decode(decryptedMetadata));
diff --git a/mobile/apps/photos/lib/services/video_preview_service.dart b/mobile/apps/photos/lib/services/video_preview_service.dart
index 1e1377ecdd..369e0abdb7 100644
--- a/mobile/apps/photos/lib/services/video_preview_service.dart
+++ b/mobile/apps/photos/lib/services/video_preview_service.dart
@@ -957,6 +957,12 @@ class VideoPreviewService {
final detailsCache = await cacheManager.getFileFromCache(
_getDetailsCacheKey(objectID),
);
+ for (final cachedFile in [playlistCache, detailsCache]) {
+ if (cachedFile != null &&
+ cachedFile.file.lengthSync() > maxHlsMetadataBytes) {
+ throw const FormatException("HLS metadata exceeds the allowed size");
+ }
+ }
late final String playlistTemplate;
if (playlistCache != null) {
playlistTemplate = playlistCache.file.readAsStringSync();
@@ -1089,14 +1095,14 @@ class VideoPreviewService {
encryptionKey,
encryptedData: fetchResult.encryptedData,
header: fetchResult.decryptionHeader,
+ maxOutputBytes: maxHlsMetadataBytes,
);
return playlistData;
}
int? parseDurationFromHLS(String playlist) {
- final lines = playlist.split("\n");
double totalDuration = 0.0;
- for (final line in lines) {
+ for (final line in LineSplitter.split(playlist)) {
if (line.startsWith("#EXTINF:")) {
final durationStr = line.substring(8, line.length - 1);
final duration = double.tryParse(durationStr);
diff --git a/mobile/apps/photos/lib/utils/gzip.dart b/mobile/apps/photos/lib/utils/gzip.dart
index 44187d836b..5737906249 100644
--- a/mobile/apps/photos/lib/utils/gzip.dart
+++ b/mobile/apps/photos/lib/utils/gzip.dart
@@ -1,9 +1,9 @@
import "dart:convert";
import "dart:io";
+import "dart:typed_data";
import "package:computer/computer.dart";
import "package:ente_crypto/ente_crypto.dart";
-import "package:flutter/foundation.dart";
class ChaChaEncryptionResult {
final String encData;
@@ -12,10 +12,31 @@ class ChaChaEncryptionResult {
ChaChaEncryptionResult({required this.encData, required this.header});
}
-Uint8List _unGzipUInt8List(Uint8List compressedData) {
- final codec = GZipCodec();
- final List<int> decompressedList = codec.decode(compressedData);
- return Uint8List.fromList(decompressedList);
+Uint8List gunzipBytes(Uint8List compressedData, {int? maxOutputBytes}) {
+ final output = _GzipOutputSink(maxOutputBytes);
+ final decoder = gzip.decoder.startChunkedConversion(output);
+ decoder.add(compressedData);
+ decoder.close();
+ return output.bytes.takeBytes();
+}
+
+class _GzipOutputSink implements Sink<List<int>> {
+ final int? maxOutputBytes;
+ final bytes = BytesBuilder(copy: false);
+
+ _GzipOutputSink(this.maxOutputBytes);
+
+ @override
+ void add(List<int> data) {
+ if (maxOutputBytes != null &&
+ bytes.length + data.length > maxOutputBytes!) {
+ throw const FormatException("Decompressed data exceeds the allowed size");
+ }
+ bytes.add(data);
+ }
+
+ @override
+ void close() {}
}
Uint8List _gzipUInt8List(Uint8List data) {
@@ -28,12 +49,18 @@ Future<Map<String, dynamic>> decryptAndUnzipJson(
Uint8List key, {
required String encryptedData,
required String header,
+ int? maxOutputBytes,
}) async {
final Computer computer = Computer.shared();
final response = await computer
.compute<Map<String, dynamic>, Map<String, dynamic>>(
_decryptAndUnzipJsonSync,
- param: {"key": key, "encryptedData": encryptedData, "header": header},
+ param: {
+ "key": key,
+ "encryptedData": encryptedData,
+ "header": header,
+ "maxOutputBytes": maxOutputBytes,
+ },
taskName: "decryptAndUnzipJson",
);
return response;
@@ -43,13 +70,17 @@ Map<String, dynamic> decryptAndUnzipJsonSync(
Uint8List key, {
required String encryptedData,
required String header,
+ int? maxOutputBytes,
}) {
final decryptedData = chachaDecryptData({
"source": CryptoUtil.base642bin(encryptedData),
"key": key,
"header": CryptoUtil.base642bin(header),
});
- final decompressedData = _unGzipUInt8List(decryptedData);
+ final decompressedData = gunzipBytes(
+ decryptedData,
+ maxOutputBytes: maxOutputBytes,
+ );
final json = utf8.decode(decompressedData);
return jsonDecode(json);
}
@@ -93,5 +124,6 @@ Map<String, dynamic> _decryptAndUnzipJsonSync(Map<String, dynamic> args) {
args["key"],
encryptedData: args["encryptedData"],
header: args["header"],
+ maxOutputBytes: args["maxOutputBytes"],
);
}
diff --git a/mobile/apps/photos/lib/utils/hls_playlist.dart b/mobile/apps/photos/lib/utils/hls_playlist.dart
index bfcc82b7e2..a8833254de 100644
--- a/mobile/apps/photos/lib/utils/hls_playlist.dart
+++ b/mobile/apps/photos/lib/utils/hls_playlist.dart
@@ -1,24 +1,40 @@
+import "dart:convert";
+
+const maxHlsMetadataBytes = 8 * 1024 * 1024;
+const maxHlsPlaylistBytes = 32 * 1024 * 1024;
+
final _allowedTag = RegExp(
r'^#(?:EXTM3U|EXT-X-(?:VERSION:\d+|TARGETDURATION:\d+|MEDIA-SEQUENCE:\d+|KEY:METHOD=AES-128,URI="data:text/plain;base64,[A-Za-z0-9+/]{22}==",IV=0x[0-9a-fA-F]{32}|BYTERANGE:\d+(?:@\d+)?|ENDLIST)|EXTINF:\d+(?:\.\d+)?,$)$',
);
String reconstructHlsPlaylist(String template, String segmentUrl) {
+ if (RegExp(r'\r(?!\n)').hasMatch(template)) {
+ throw const FormatException('Invalid HLS playlist');
+ }
final result = <String>[];
+ final segmentUrlBytes = utf8.encode(segmentUrl).length;
+ var outputBytes = 0;
var hasKey = false;
var hasSegment = false;
- for (final line in template.split(RegExp(r'\r?\n'))) {
+ for (var line in LineSplitter.split(template)) {
if (line.isEmpty || (line.startsWith('#') && !line.startsWith('#EXT'))) {
continue;
}
if (!line.startsWith('#')) {
- result.add(segmentUrl);
+ line = segmentUrl;
+ outputBytes += segmentUrlBytes;
hasSegment = true;
} else if (_allowedTag.hasMatch(line)) {
- result.add(line);
+ outputBytes += line.length;
hasKey |= line.startsWith('#EXT-X-KEY:');
} else {
throw const FormatException('Invalid HLS playlist');
}
+ outputBytes += 1;
+ if (outputBytes > maxHlsPlaylistBytes) {
+ throw const FormatException("HLS playlist exceeds the allowed size");
+ }
+ result.add(line);
}
if (!hasKey ||
@@ -27,5 +43,5 @@ String reconstructHlsPlaylist(String template, String segmentUrl) {
result.last != '#EXT-X-ENDLIST') {
throw const FormatException('Invalid HLS playlist');
}
- return '${result.join('\n')}\n';
+ return (result..add('')).join('\n');
}