π End-to-end encrypted cloud for everything.
Dart 2faandroidauthye2eeencryptionend-to-end-encryptionfluttergolanggoogle-photosgoogle-photos-alternativeiosopensourcephotosprivacyprivacy-toolstypescriptzero-knowledge
Validate generated HLS playlists
Neeraj Gupta1cc3d7aea1bb
diff --git a/mobile/apps/photos/lib/services/video_preview_service.dart b/mobile/apps/photos/lib/services/video_preview_service.dartindex 8e17c50a17..048cc38038 100644--- a/mobile/apps/photos/lib/services/video_preview_service.dart+++ b/mobile/apps/photos/lib/services/video_preview_service.dart@@ -44,6 +44,7 @@ import "package:photos/services/machine_learning/compute_controller.dart";import "package:photos/ui/notification/toast.dart";import "package:photos/utils/file_key.dart";import "package:photos/utils/gzip.dart";+import "package:photos/utils/hls_playlist.dart";import "package:photos/utils/network_util.dart";const _maxRetryCount = 3;@@ -956,9 +957,9 @@ class VideoPreviewService {final detailsCache = await cacheManager.getFileFromCache(_getDetailsCacheKey(objectID),);- String finalPlaylist;+ late final String playlistTemplate;if (playlistCache != null) {- finalPlaylist = playlistCache.file.readAsStringSync();+ playlistTemplate = playlistCache.file.readAsStringSync();if (detailsCache != null) {final details = json.decode(detailsCache.file.readAsStringSync());width = details["width"];@@ -967,28 +968,10 @@ class VideoPreviewService {}} else {final Map<String, dynamic> playlistData = await _getPlaylistData(file);- finalPlaylist = playlistData["playlist"];+ playlistTemplate = playlistData["playlist"];width = playlistData["width"];height = playlistData["height"];size = playlistData["size"];- unawaited(- cacheManager.putFile(- _getCacheKey(objectID),- Uint8List.fromList((playlistData["playlist"] as String).codeUnits),- ),- );- unawaited(- cacheManager.putFile(- _getDetailsCacheKey(objectID),- Uint8List.fromList(- json.encode({- "width": width,- "height": height,- "size": size,- }).codeUnits,- ),- ),- );}final videoFile = (await videoCacheManager.getFileFromCache(_getVideoPreviewKey(objectID),@@ -996,6 +979,7 @@ class VideoPreviewService {final effectiveSize = size ?? previewInfo?.objectSize;final maxPreviewCacheSize =maxPreviewSizeBytes ?? _maxPreviewSizeLimitForCache;+ late final String segmentUrl;if (videoFile == null) {previewURLResult = previewURLResult ?? await _getPreviewUrl(file);final canCachePreview =@@ -1008,10 +992,7 @@ class VideoPreviewService {previewURLResult.$1,key: _getVideoPreviewKey(objectID),);- finalPlaylist = finalPlaylist.replaceAll(- '\noutput.ts',- '\n${previewFile.file.path}',- );+ segmentUrl = previewFile.file.path;} else {unawaited(_downloadVideoPreviewForCache(@@ -1020,21 +1001,36 @@ class VideoPreviewService {file,),);- finalPlaylist = finalPlaylist.replaceAll(- '\noutput.ts',- '\n${previewURLResult.$1}',- );+ segmentUrl = previewURLResult.$1;}} else {- finalPlaylist = finalPlaylist.replaceAll(- '\noutput.ts',- '\n${previewURLResult.$1}',- );+ segmentUrl = previewURLResult.$1;}} else {- finalPlaylist = finalPlaylist.replaceAll(- '\noutput.ts',- '\n${videoFile.path}',+ segmentUrl = videoFile.path;+ }+ final finalPlaylist = reconstructHlsPlaylist(+ playlistTemplate,+ segmentUrl,+ );+ if (playlistCache == null) {+ unawaited(+ cacheManager.putFile(+ _getCacheKey(objectID),+ Uint8List.fromList(playlistTemplate.codeUnits),+ ),+ );+ unawaited(+ cacheManager.putFile(+ _getDetailsCacheKey(objectID),+ Uint8List.fromList(+ json.encode({+ "width": width,+ "height": height,+ "size": size,+ }).codeUnits,+ ),+ ),);}final tempDir = await getTemporaryDirectory();diff --git a/mobile/apps/photos/lib/utils/hls_playlist.dart b/mobile/apps/photos/lib/utils/hls_playlist.dartnew file mode 100644index 0000000000..da801ea747--- /dev/null+++ b/mobile/apps/photos/lib/utils/hls_playlist.dart@@ -0,0 +1,40 @@+final _keyTag = RegExp(+ r'^#EXT-X-KEY:METHOD=AES-128,URI="data:text/plain;base64,[A-Za-z0-9+/]{22}==",IV=0x[0-9a-fA-F]{32}$',+);+final _durationTag = RegExp(r'^#EXTINF:\d+(?:\.\d+)?,$');+final _byteRangeTag = RegExp(r'^#EXT-X-BYTERANGE:\d+@\d+$');++String reconstructHlsPlaylist(String template, String segmentUrl) {+ if (template.contains('\r')) {+ throw const FormatException('Invalid HLS playlist');+ }++ final lines = template+ .split('\n')+ .where((line) => !line.startsWith('#') || line.startsWith('#EXT'))+ .toList();+ if (lines.isNotEmpty && lines.last.isEmpty) lines.removeLast();++ if (lines.length < 9 ||+ lines[0] != '#EXTM3U' ||+ lines[1] != '#EXT-X-VERSION:4' ||+ !RegExp(r'^#EXT-X-TARGETDURATION:\d+$').hasMatch(lines[2]) ||+ lines[3] != '#EXT-X-MEDIA-SEQUENCE:0' ||+ !_keyTag.hasMatch(lines[4]) ||+ lines.last != '#EXT-X-ENDLIST' ||+ (lines.length - 6) % 3 != 0) {+ throw const FormatException('Invalid HLS playlist');+ }++ final result = lines.sublist(0, 5);+ for (var i = 5; i < lines.length - 1; i += 3) {+ if (!_durationTag.hasMatch(lines[i]) ||+ !_byteRangeTag.hasMatch(lines[i + 1]) ||+ lines[i + 2] != 'output.ts') {+ throw const FormatException('Invalid HLS playlist');+ }+ result.addAll([lines[i], lines[i + 1], segmentUrl]);+ }++ return '${[...result, '#EXT-X-ENDLIST'].join('\n')}\n';+}diff --git a/mobile/apps/photos/test/utils/hls_playlist_test.dart b/mobile/apps/photos/test/utils/hls_playlist_test.dartnew file mode 100644index 0000000000..9c9032c100--- /dev/null+++ b/mobile/apps/photos/test/utils/hls_playlist_test.dart@@ -0,0 +1,56 @@+import 'package:flutter_test/flutter_test.dart';+import 'package:photos/utils/hls_playlist.dart';++const playlist = '''#EXTM3U+#EXT-X-VERSION:4+#EXT-X-TARGETDURATION:8+#EXT-X-MEDIA-SEQUENCE:0+#EXT-X-KEY:METHOD=AES-128,URI="data:text/plain;base64,XjvG7qeRrsOpPUbJPh2Ikg==",IV=0x00000000000000000000000000000000+#EXTINF:8.333333,+#EXT-X-BYTERANGE:3046928@0+output.ts+#EXTINF:2.200000,+#EXT-X-BYTERANGE:834736@3046928+output.ts+#EXT-X-ENDLIST+''';++void main() {+ group('reconstructHlsPlaylist', () {+ test('reconstructs a generated single-file playlist', () {+ const segmentUrl = 'https://museum.example/video?token=secret';++ expect(+ reconstructHlsPlaylist(playlist, segmentUrl),+ playlist.replaceAll('output.ts', segmentUrl),+ );+ });++ final invalidPlaylists = {+ 'remote segment': playlist.replaceFirst('output.ts', 'https://attacker'),+ 'segment suffix': playlist.replaceFirst('output.ts', 'output.ts?x'),+ 'remote key': playlist.replaceFirst(+ 'data:text/plain;base64,XjvG7qeRrsOpPUbJPh2Ikg==',+ 'https://attacker/key',+ ),+ 'map': playlist.replaceFirst(+ '#EXTINF:8.333333,',+ '#EXT-X-MAP:URI="https://attacker/map"\n#EXTINF:8.333333,',+ ),+ 'master playlist': playlist.replaceFirst(+ '#EXTINF:8.333333,',+ '#EXT-X-STREAM-INF:BANDWIDTH=1\n#EXTINF:8.333333,',+ ),+ 'CRLF': playlist.replaceAll('\n', '\r\n'),+ };++ for (final entry in invalidPlaylists.entries) {+ test('rejects a playlist with an unexpected ${entry.key}', () {+ expect(+ () => reconstructHlsPlaylist(entry.value, 'https://museum/video'),+ throwsFormatException,+ );+ });+ }+ });+}diff --git a/web/apps/albums/src/public-album/media/video/preview.ts b/web/apps/albums/src/public-album/media/video/preview.tsindex 6de6333996..35d6781cca 100644--- a/web/apps/albums/src/public-album/media/video/preview.ts+++ b/web/apps/albums/src/public-album/media/video/preview.ts@@ -6,6 +6,7 @@ import {fetchFileData,fetchFilePreviewData,} from "ente-gallery/services/file-data";+import { reconstructHLSPlaylist } from "ente-gallery/utils/hls";import { fileLogID, type EnteFile } from "ente-media/file";import { FileType } from "ente-media/file-type";import { ensurePrecondition } from "ente-utils/ensure";@@ -54,10 +55,7 @@ export const hlsPlaylistDataForFile = async ();if (!videoURL) return undefined;- const playlist = playlistTemplate.replaceAll(- "\noutput.ts",- `\n${videoURL}`,- );+ const playlist = reconstructHLSPlaylist(playlistTemplate, videoURL);const playlistURL = await blobToDataURL(new Blob([playlist], { type: "application/vnd.apple.mpegurl" }),diff --git a/web/apps/photos/tests/hls-playlist.test.ts b/web/apps/photos/tests/hls-playlist.test.tsnew file mode 100644index 0000000000..650d22e067--- /dev/null+++ b/web/apps/photos/tests/hls-playlist.test.ts@@ -0,0 +1,57 @@+import { reconstructHLSPlaylist } from "ente-gallery/utils/hls";+import { describe, expect, test } from "vitest";++const playlist = `#EXTM3U+#EXT-X-VERSION:4+#EXT-X-TARGETDURATION:8+#EXT-X-MEDIA-SEQUENCE:0+#EXT-X-KEY:METHOD=AES-128,URI="data:text/plain;base64,XjvG7qeRrsOpPUbJPh2Ikg==",IV=0x00000000000000000000000000000000+#EXTINF:8.333333,+#EXT-X-BYTERANGE:3046928@0+output.ts+#EXTINF:2.200000,+#EXT-X-BYTERANGE:834736@3046928+output.ts+#EXT-X-ENDLIST+`;++describe("reconstructHLSPlaylist", () => {+ test("reconstructs a generated single-file playlist", () => {+ const segmentURL = "https://museum.example/video?token=secret";++ expect(reconstructHLSPlaylist(playlist, segmentURL)).toBe(+ playlist.replaceAll("output.ts", segmentURL),+ );+ });++ test.each([+ ["remote segment", playlist.replace("output.ts", "https://attacker")],+ ["segment suffix", playlist.replace("output.ts", "output.ts?x")],+ [+ "remote key",+ playlist.replace(+ "data:text/plain;base64,XjvG7qeRrsOpPUbJPh2Ikg==",+ "https://attacker/key",+ ),+ ],+ [+ "map",+ playlist.replace(+ "#EXTINF:8.333333,",+ '#EXT-X-MAP:URI="https://attacker/map"\n#EXTINF:8.333333,',+ ),+ ],+ [+ "master playlist",+ playlist.replace(+ "#EXTINF:8.333333,",+ "#EXT-X-STREAM-INF:BANDWIDTH=1\n#EXTINF:8.333333,",+ ),+ ],+ ["CRLF", playlist.replaceAll("\n", "\r\n")],+ ])("rejects a playlist with an unexpected %s", (_, input) => {+ expect(() =>+ reconstructHLSPlaylist(input, "https://museum/video"),+ ).toThrow("Invalid HLS playlist");+ });+});diff --git a/web/packages/gallery/services/video.ts b/web/packages/gallery/services/video.tsindex 7c75cc25b2..4fd307ece1 100644--- a/web/packages/gallery/services/video.ts+++ b/web/packages/gallery/services/video.ts@@ -11,6 +11,7 @@ import { apiURL } from "ente-base/origins";import type { PublicMemoryCredentials } from "ente-base/public-memory";import { ensureAuthToken } from "ente-base/token";import { uniqueFilesByID } from "ente-gallery/utils/file";+import { reconstructHLSPlaylist } from "ente-gallery/utils/hls";import { fileLogID, type EnteFile } from "ente-media/file";import { FileType } from "ente-media/file-type";import { updateFilePublicMagicMetadata } from "ente-new/photos/services/file";@@ -181,11 +182,7 @@ export const hlsPlaylistDataForFile = async ();if (!videoURL) return undefined;- // Native playlists use this placeholder for the signed range-request URL.- const playlist = playlistTemplate.replaceAll(- "\noutput.ts",- `\n${videoURL}`,- );+ const playlist = reconstructHLSPlaylist(playlistTemplate, videoURL);// HLS clients require a recognizable playlist MIME type or filename.const playlistURL = await blobToDataURL(diff --git a/web/packages/gallery/utils/hls.ts b/web/packages/gallery/utils/hls.tsnew file mode 100644index 0000000000..070fa98d3d--- /dev/null+++ b/web/packages/gallery/utils/hls.ts@@ -0,0 +1,43 @@+const keyTag =+ /^#EXT-X-KEY:METHOD=AES-128,URI="data:text\/plain;base64,[A-Za-z0-9+/]{22}==",IV=0x[0-9a-fA-F]{32}$/;+const durationTag = /^#EXTINF:\d+(?:\.\d+)?,$/;+const byteRangeTag = /^#EXT-X-BYTERANGE:\d+@\d+$/;++export const reconstructHLSPlaylist = (+ template: string,+ segmentURL: string,+) => {+ if (template.includes("\r")) throw new Error("Invalid HLS playlist");++ const lines = template+ .split("\n")+ .filter((line) => !line.startsWith("#") || line.startsWith("#EXT"));+ if (lines.at(-1) == "") lines.pop();++ if (+ lines.length < 9 ||+ lines[0] != "#EXTM3U" ||+ lines[1] != "#EXT-X-VERSION:4" ||+ !/^#EXT-X-TARGETDURATION:\d+$/.test(lines[2]!) ||+ lines[3] != "#EXT-X-MEDIA-SEQUENCE:0" ||+ !keyTag.test(lines[4]!) ||+ lines.at(-1) != "#EXT-X-ENDLIST" ||+ (lines.length - 6) % 3+ ) {+ throw new Error("Invalid HLS playlist");+ }++ const result = lines.slice(0, 5);+ for (let i = 5; i < lines.length - 1; i += 3) {+ if (+ !durationTag.test(lines[i]!) ||+ !byteRangeTag.test(lines[i + 1]!) ||+ lines[i + 2] != "output.ts"+ ) {+ throw new Error("Invalid HLS playlist");+ }+ result.push(lines[i]!, lines[i + 1]!, segmentURL);+ }++ return [...result, "#EXT-X-ENDLIST", ""].join("\n");+};