π End-to-end encrypted cloud for everything.
Dart 2faandroidauthye2eeencryptionend-to-end-encryptionfluttergolanggoogle-photosgoogle-photos-alternativeiosopensourcephotosprivacyprivacy-toolstypescriptzero-knowledge
Simplify HLS playlist validation
Neeraj Gupta532326601dce
diff --git a/mobile/apps/photos/lib/utils/hls_playlist.dart b/mobile/apps/photos/lib/utils/hls_playlist.dartindex 1f09e8c1af..bfcc82b7e2 100644--- a/mobile/apps/photos/lib/utils/hls_playlist.dart+++ b/mobile/apps/photos/lib/utils/hls_playlist.dart@@ -1,36 +1,31 @@-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 _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+)?,$)$',);-final _durationTag = RegExp(r'^#EXTINF:\d+(?:\.\d+)?,$');-final _byteRangeTag = RegExp(r'^#EXT-X-BYTERANGE:\d+@\d+$');String reconstructHlsPlaylist(String template, String segmentUrl) {- 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') {+ final result = <String>[];+ var hasKey = false;+ var hasSegment = false;+ for (final line in template.split(RegExp(r'\r?\n'))) {+ if (line.isEmpty || (line.startsWith('#') && !line.startsWith('#EXT'))) {+ continue;+ }+ if (!line.startsWith('#')) {+ result.add(segmentUrl);+ hasSegment = true;+ } else if (_allowedTag.hasMatch(line)) {+ result.add(line);+ hasKey |= line.startsWith('#EXT-X-KEY:');+ } else {throw const FormatException('Invalid HLS playlist');}- result.addAll([lines[i], lines[i + 1], segmentUrl]);}- return '${[...result, '#EXT-X-ENDLIST'].join('\n')}\n';+ if (!hasKey ||+ !hasSegment ||+ result.first != '#EXTM3U' ||+ result.last != '#EXT-X-ENDLIST') {+ throw const FormatException('Invalid HLS playlist');+ }+ return '${result.join('\n')}\n';}diff --git a/mobile/apps/photos/test/utils/hls_playlist_test.dart b/mobile/apps/photos/test/utils/hls_playlist_test.dartindex ac52dca0ab..6bc72cc277 100644--- a/mobile/apps/photos/test/utils/hls_playlist_test.dart+++ b/mobile/apps/photos/test/utils/hls_playlist_test.dart@@ -2,32 +2,41 @@ import 'package:flutter_test/flutter_test.dart';import 'package:photos/utils/hls_playlist.dart';const playlist = '''#EXTM3U-#EXT-X-VERSION:4+#EXT-X-MEDIA-SEQUENCE:42+#EXT-X-VERSION:7#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+video.ts#EXTINF:2.200000,-#EXT-X-BYTERANGE:834736@3046928-output.ts+#EXT-X-BYTERANGE:834736+video.ts#EXT-X-ENDLIST''';void main() {group('reconstructHlsPlaylist', () {- test('reconstructs a generated single-file playlist', () {+ test('reconstructs an allowed single-file playlist', () {const segmentUrl = 'https://museum.example/video?token=secret';expect(reconstructHlsPlaylist(playlist, segmentUrl),- playlist.replaceAll('output.ts', segmentUrl),+ playlist.replaceAll('video.ts', segmentUrl),+ );+ });++ test('replaces every untrusted URI line', () {+ const segmentUrl = 'https://museum.example/video';+ final input = playlist.replaceFirst('video.ts', 'https://attacker');++ expect(+ reconstructHlsPlaylist(input, segmentUrl),+ playlist.replaceAll('video.ts', segmentUrl),);});final invalidPlaylists = {- 'remote segment': playlist.replaceFirst('output.ts', 'https://attacker'),'remote key': playlist.replaceFirst('data:text/plain;base64,XjvG7qeRrsOpPUbJPh2Ikg==','https://attacker/key',diff --git a/web/apps/photos/tests/hls-playlist.test.ts b/web/apps/photos/tests/hls-playlist.test.tsindex 1e23cdf451..efa67169fd 100644--- a/web/apps/photos/tests/hls-playlist.test.ts+++ b/web/apps/photos/tests/hls-playlist.test.ts@@ -2,30 +2,38 @@ import { reconstructHLSPlaylist } from "ente-gallery/utils/hls";import { describe, expect, test } from "vitest";const playlist = `#EXTM3U-#EXT-X-VERSION:4+#EXT-X-MEDIA-SEQUENCE:42+#EXT-X-VERSION:7#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+video.ts#EXTINF:2.200000,-#EXT-X-BYTERANGE:834736@3046928-output.ts+#EXT-X-BYTERANGE:834736+video.ts#EXT-X-ENDLIST`;describe("reconstructHLSPlaylist", () => {- test("reconstructs a generated single-file playlist", () => {+ test("reconstructs an allowed single-file playlist", () => {const segmentURL = "https://museum.example/video?token=secret";expect(reconstructHLSPlaylist(playlist, segmentURL)).toBe(- playlist.replaceAll("output.ts", segmentURL),+ playlist.replaceAll("video.ts", segmentURL),+ );+ });++ test("replaces every untrusted URI line", () => {+ const segmentURL = "https://museum.example/video";+ const input = playlist.replace("video.ts", "https://attacker");++ expect(reconstructHLSPlaylist(input, segmentURL)).toBe(+ playlist.replaceAll("video.ts", segmentURL),);});test.each([- ["remote segment", playlist.replace("output.ts", "https://attacker")],["remote key",playlist.replace(diff --git a/web/packages/gallery/utils/hls.ts b/web/packages/gallery/utils/hls.tsindex 4380350371..8409dc3561 100644--- a/web/packages/gallery/utils/hls.ts+++ b/web/packages/gallery/utils/hls.ts@@ -1,41 +1,35 @@-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+$/;+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+)?,$)$/;export const reconstructHLSPlaylist = (template: string,segmentURL: string,) => {- const lines = template- .split("\n")- .filter((line) => !line.startsWith("#") || line.startsWith("#EXT"));- if (lines.at(-1) == "") lines.pop();+ const result: string[] = [];+ let hasKey = false;+ let hasSegment = false;+ for (const line of template.split(/\r?\n/)) {+ if (!line || (line.startsWith("#") && !line.startsWith("#EXT"))) {+ continue;+ }+ if (!line.startsWith("#")) {+ result.push(segmentURL);+ hasSegment = true;+ } else if (allowedTag.test(line)) {+ result.push(line);+ hasKey ||= line.startsWith("#EXT-X-KEY:");+ } else {+ return undefined;+ }+ }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+ !hasKey ||+ !hasSegment ||+ result[0] != "#EXTM3U" ||+ result.at(-1) != "#EXT-X-ENDLIST") {return undefined;}-- 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"- ) {- return undefined;- }- result.push(lines[i]!, lines[i + 1]!, segmentURL);- }-- return [...result, "#EXT-X-ENDLIST", ""].join("\n");+ return [...result, ""].join("\n");};