π End-to-end encrypted cloud for everything.
Dart 2faandroidauthye2eeencryptionend-to-end-encryptionfluttergolanggoogle-photosgoogle-photos-alternativeiosopensourcephotosprivacyprivacy-toolstypescriptzero-knowledge
[web] Bound HLS playlist processing (#13211)
neeraj-pilot0e8a410a6440
diff --git a/web/apps/albums/src/public-album/media/video/preview.ts b/web/apps/albums/src/public-album/media/video/preview.tsindex 9eed408285..8b9a209034 100644--- a/web/apps/albums/src/public-album/media/video/preview.ts+++ b/web/apps/albums/src/public-album/media/video/preview.ts@@ -6,9 +6,13 @@ import {fetchFileData,fetchFilePreviewData,} from "ente-gallery/services/file-data";-import { reconstructHLSPlaylist } from "ente-gallery/utils/hls";+import {+ maxHLSMetadataBytes,+ reconstructHLSPlaylist,+} from "ente-gallery/utils/hls";import { fileLogID, type EnteFile } from "ente-media/file";import { FileType } from "ente-media/file-type";+import { gunzipWithLimit } from "ente-new/photos/utils/gzip";import { ensurePrecondition } from "ente-utils/ensure";import { z } from "zod";@@ -82,15 +86,13 @@ const decryptPlaylistJSON = async (file: EnteFile,) => {const decryptedBytes = await decryptBlobBytes(encryptedPlaylist, file.key);- const jsonString = await gunzip(decryptedBytes);+ const jsonString = await gunzipWithLimit(+ decryptedBytes,+ maxHLSMetadataBytes,+ );return PlaylistJSON.parse(JSON.parse(jsonString));};-const gunzip = async (data: Uint8Array<ArrayBuffer>) =>- await new Response(- new Blob([data]).stream().pipeThrough(new DecompressionStream("gzip")),- ).text();-const blobToDataURL = (blob: Blob) =>new Promise<string>((resolve) => {const reader = new FileReader();diff --git a/web/packages/gallery/services/video.ts b/web/packages/gallery/services/video.tsindex 1738dbf39d..a868c5b27a 100644--- a/web/packages/gallery/services/video.ts+++ b/web/packages/gallery/services/video.ts@@ -11,13 +11,16 @@ 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 {+ maxHLSMetadataBytes,+ 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";import { savedCollectionFiles } from "ente-new/photos/services/photos-fdb";import { savedTrashItemFileIDs } from "ente-new/photos/services/trash";-import { gunzip, gzip } from "ente-new/photos/utils/gzip";+import { gunzipWithLimit, gzip } from "ente-new/photos/utils/gzip";import { randomSample } from "ente-utils/array";import { ensurePrecondition } from "ente-utils/ensure";import { wait } from "ente-utils/promise";@@ -197,13 +200,15 @@ export const hlsPlaylistDataForFile = async ();if (!playlistFileData) return undefined;- const {- type,- playlist: playlistTemplate,- width,- height,- } = await decryptPlaylistJSON(playlistFileData, file);+ let playlistJSON: PlaylistJSON;+ try {+ playlistJSON = await decryptPlaylistJSON(playlistFileData, file);+ } catch (e) {+ log.error(`Failed to read HLS playlist for ${fileLogID(file)}`, e);+ return undefined;+ }+ const { type, playlist: playlistTemplate, width, height } = playlistJSON;if (type != "hls_video") return undefined;const videoURL = await fetchFilePreviewData(@@ -243,7 +248,10 @@ const decryptPlaylistJSON = async (file: EnteFile,) => {const decryptedBytes = await decryptBlobBytes(encryptedPlaylist, file.key);- const jsonString = await gunzip(decryptedBytes);+ const jsonString = await gunzipWithLimit(+ decryptedBytes,+ maxHLSMetadataBytes,+ );return PlaylistJSON.parse(JSON.parse(jsonString));};diff --git a/web/packages/gallery/utils/hls.ts b/web/packages/gallery/utils/hls.tsindex 8409dc3561..ed75c8afd6 100644--- a/web/packages/gallery/utils/hls.ts+++ b/web/packages/gallery/utils/hls.ts@@ -1,3 +1,6 @@+export const maxHLSMetadataBytes = 8 * 1024 * 1024;+const maxHLSPlaylistBytes = 32 * 1024 * 1024;+const allowedTag =/^#(?: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+)?,$)$/;@@ -6,21 +9,28 @@ export const reconstructHLSPlaylist = (segmentURL: string,) => {const result: string[] = [];+ const segmentURLBytes = new TextEncoder().encode(segmentURL).byteLength;+ let outputBytes = 0;let hasKey = false;let hasSegment = false;- for (const line of template.split(/\r?\n/)) {+ for (const match of template.matchAll(/([^\n]*?)(?:\r?\n|$)/g)) {+ let line = match[1]!;if (!line || (line.startsWith("#") && !line.startsWith("#EXT"))) {continue;}if (!line.startsWith("#")) {- result.push(segmentURL);+ line = segmentURL;+ outputBytes += segmentURLBytes;hasSegment = true;} else if (allowedTag.test(line)) {- result.push(line);+ outputBytes += line.length;hasKey ||= line.startsWith("#EXT-X-KEY:");} else {return undefined;}+ outputBytes += 1;+ if (outputBytes > maxHLSPlaylistBytes) return undefined;+ result.push(line);}if (@@ -31,5 +41,6 @@ export const reconstructHLSPlaylist = () {return undefined;}- return [...result, ""].join("\n");+ result.push("");+ return result.join("\n");};