Skip to content

armgit / ente

Public Imported from GitHub
0

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

[web] Reject line breaks in HLS segment URLs (#14306)

neeraj-pilot514c1d6ed13f
diff --git a/mobile/apps/photos/lib/utils/hls_playlist.dart b/mobile/apps/photos/lib/utils/hls_playlist.dart
index a8833254de..7ccaa04fb1 100644
--- a/mobile/apps/photos/lib/utils/hls_playlist.dart
+++ b/mobile/apps/photos/lib/utils/hls_playlist.dart
@@ -8,7 +8,8 @@ final _allowedTag = RegExp(
);
String reconstructHlsPlaylist(String template, String segmentUrl) {
- if (RegExp(r'\r(?!\n)').hasMatch(template)) {
+ if (RegExp(r'\r(?!\n)').hasMatch(template) ||
+ RegExp(r'[\r\n]').hasMatch(segmentUrl)) {
throw const FormatException('Invalid HLS playlist');
}
final result = <String>[];
diff --git a/mobile/apps/photos/test/utils/hls_playlist_test.dart b/mobile/apps/photos/test/utils/hls_playlist_test.dart
index 6bc72cc277..f7b51b65bc 100644
--- a/mobile/apps/photos/test/utils/hls_playlist_test.dart
+++ b/mobile/apps/photos/test/utils/hls_playlist_test.dart
@@ -36,6 +36,18 @@ void main() {
);
});
+ for (final entry in {'line feed': '\n', 'carriage return': '\r'}.entries) {
+ test('rejects a segment URL containing ${entry.key}', () {
+ final segmentUrl =
+ 'https://museum.example/video${entry.value}#EXT-X-KEY:METHOD=NONE';
+
+ expect(
+ () => reconstructHlsPlaylist(playlist, segmentUrl),
+ throwsFormatException,
+ );
+ });
+ }
+
final invalidPlaylists = {
'remote key': playlist.replaceFirst(
'data:text/plain;base64,XjvG7qeRrsOpPUbJPh2Ikg==',
diff --git a/web/apps/photos/tests/hls-playlist.test.ts b/web/apps/photos/tests/hls-playlist.test.ts
index efa67169fd..39a0570651 100644
--- a/web/apps/photos/tests/hls-playlist.test.ts
+++ b/web/apps/photos/tests/hls-playlist.test.ts
@@ -33,6 +33,15 @@ describe("reconstructHLSPlaylist", () => {
);
});
+ test.each([
+ ["line feed", "\n"],
+ ["carriage return", "\r"],
+ ])("rejects a segment URL containing %s", (_, lineBreak) => {
+ const segmentURL = `https://museum.example/video${lineBreak}#EXT-X-KEY:METHOD=NONE`;
+
+ expect(reconstructHLSPlaylist(playlist, segmentURL)).toBeUndefined();
+ });
+
test.each([
[
"remote key",
diff --git a/web/packages/gallery/utils/hls.ts b/web/packages/gallery/utils/hls.ts
index ed75c8afd6..dbde532bdb 100644
--- a/web/packages/gallery/utils/hls.ts
+++ b/web/packages/gallery/utils/hls.ts
@@ -8,6 +8,8 @@ export const reconstructHLSPlaylist = (
template: string,
segmentURL: string,
) => {
+ if (/[\r\n]/.test(segmentURL)) return undefined;
+
const result: string[] = [];
const segmentURLBytes = new TextEncoder().encode(segmentURL).byteLength;
let outputBytes = 0;