Skip to content

armgit / ente

Public Imported from GitHub
0

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

Preserve fallback for invalid HLS previews

Neeraj Gupta6d69764e7803
diff --git a/mobile/apps/photos/lib/utils/hls_playlist.dart b/mobile/apps/photos/lib/utils/hls_playlist.dart
index da801ea747..1f09e8c1af 100644
--- a/mobile/apps/photos/lib/utils/hls_playlist.dart
+++ b/mobile/apps/photos/lib/utils/hls_playlist.dart
@@ -5,10 +5,6 @@ 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'))
diff --git a/mobile/apps/photos/test/utils/hls_playlist_test.dart b/mobile/apps/photos/test/utils/hls_playlist_test.dart
index 9c9032c100..ac52dca0ab 100644
--- a/mobile/apps/photos/test/utils/hls_playlist_test.dart
+++ b/mobile/apps/photos/test/utils/hls_playlist_test.dart
@@ -28,7 +28,6 @@ void main() {
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',
@@ -37,11 +36,6 @@ void main() {
'#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) {
diff --git a/web/apps/albums/src/public-album/media/video/preview.ts b/web/apps/albums/src/public-album/media/video/preview.ts
index 35d6781cca..01601fe854 100644
--- a/web/apps/albums/src/public-album/media/video/preview.ts
+++ b/web/apps/albums/src/public-album/media/video/preview.ts
@@ -56,6 +56,10 @@ export const hlsPlaylistDataForFile = async (
if (!videoURL) return undefined;
const playlist = reconstructHLSPlaylist(playlistTemplate, videoURL);
+ if (!playlist) {
+ log.warn(`Ignoring invalid HLS playlist for ${fileLogID(file)}`);
+ return undefined;
+ }
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.ts
index 650d22e067..1e23cdf451 100644
--- a/web/apps/photos/tests/hls-playlist.test.ts
+++ b/web/apps/photos/tests/hls-playlist.test.ts
@@ -26,7 +26,6 @@ describe("reconstructHLSPlaylist", () => {
test.each([
["remote segment", playlist.replace("output.ts", "https://attacker")],
- ["segment suffix", playlist.replace("output.ts", "output.ts?x")],
[
"remote key",
playlist.replace(
@@ -41,17 +40,9 @@ describe("reconstructHLSPlaylist", () => {
'#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(() =>
+ expect(
reconstructHLSPlaylist(input, "https://museum/video"),
- ).toThrow("Invalid HLS playlist");
+ ).toBeUndefined();
});
});
diff --git a/web/packages/gallery/services/video.ts b/web/packages/gallery/services/video.ts
index 4fd307ece1..7248f1747e 100644
--- a/web/packages/gallery/services/video.ts
+++ b/web/packages/gallery/services/video.ts
@@ -183,6 +183,10 @@ export const hlsPlaylistDataForFile = async (
if (!videoURL) return undefined;
const playlist = reconstructHLSPlaylist(playlistTemplate, videoURL);
+ if (!playlist) {
+ log.warn(`Ignoring invalid HLS playlist for ${fileLogID(file)}`);
+ return undefined;
+ }
// 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.ts
index 070fa98d3d..4380350371 100644
--- a/web/packages/gallery/utils/hls.ts
+++ b/web/packages/gallery/utils/hls.ts
@@ -7,8 +7,6 @@ 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"));
@@ -24,7 +22,7 @@ export const reconstructHLSPlaylist = (
lines.at(-1) != "#EXT-X-ENDLIST" ||
(lines.length - 6) % 3
) {
- throw new Error("Invalid HLS playlist");
+ return undefined;
}
const result = lines.slice(0, 5);
@@ -34,7 +32,7 @@ export const reconstructHLSPlaylist = (
!byteRangeTag.test(lines[i + 1]!) ||
lines[i + 2] != "output.ts"
) {
- throw new Error("Invalid HLS playlist");
+ return undefined;
}
result.push(lines[i]!, lines[i + 1]!, segmentURL);
}