CVE-2023-43256 (published 2023-09-25T14:15Z)

A path traversal in Gladys Assistant v4.26.1 and below allows authenticated attackers to extract sensitive files in the host machine by exploiting a non-sanitized user input.

CVSS v3 base score: 6.5, MEDIUM (CVSS:3.1/AV:N/AC:L/PR:L/UI:N/S:U/C:H/I:N/A:N)

Weakness information:
cwe_id: CWE-22
cwe_name: Improper Limitation of a Pathname to a Restricted Directory ('Path Traversal')
description: The product uses external input to construct a pathname that is intended to identify a file or directory that is located underneath a restricted parent directory, but the product does not properly neutralize special elements within the pathname that can cause the pathname to resolve to a location that is outside of the restricted directory.
extended_description: 
            Many file operations are intended to take place within a restricted directory. By using special elements such as ".." and "/" separators, attackers can escape outside of the restricted location to access files or directories that are elsewhere on the system. One of the most common special elements is the "../" sequence, which in most modern operating systems is interpreted as the parent directory of the current location. This is referred to as relative path traversal. Path traversal also covers the use of absolute pathnames such as "/usr/local/bin" to access unexpected files. This is referred to as absolute path traversal.
         
         

Fix information:
repo_url: https://github.com/GladysAssistant/Gladys
commit_details: repo_url: https://github.com/GladysAssistant/Gladys
author: Pierre-Gilles Leymarie
author_date: 2023-08-25 10:53:12+02:00
msg: Camera Streaming: Fix validation of session_id and filename (#1847)
repository: repo_url: https://github.com/GladysAssistant/Gladys
repo_name: GladysAssistant/Gladys
description: A privacy-first, open-source home assistant
repo_language: JavaScript
owner: GladysAssistant
file_changes: filename: rtspCamera.controller.js
change_type: ModificationType.MODIFY
diff: @@ -2,7 +2,8 @@ const fs = require('fs');
 const path = require('path');
 
 const logger = require('../../../utils/logger');
-const { Error404 } = require('../../../utils/httpErrors');
+const { Error404, Error400 } = require('../../../utils/httpErrors');
+const { validateFilename, validateSessionId } = require('../utils/validateStreamParams');
 const asyncMiddleware = require('../../../api/middlewares/asyncMiddleware');
 
 module.exports = function RtspCameraController(gladys, rtspCameraHandler) {
@@ -57,6 +58,8 @@ module.exports = function RtspCameraController(gladys, rtspCameraHandler) {
    */
   async function getStreamingFile(req, res) {
     try {
+      validateSessionId(req.params.folder);
+      validateFilename(req.params.file);
       const filePath = path.join(gladys.config.tempFolder, req.params.folder, req.params.file);
       const filestream = fs.createReadStream(filePath);
       filestream.on('error', (err) => {
@@ -64,6 +67,9 @@ module.exports = function RtspCameraController(gladys, rtspCameraHandler) {
       });
       filestream.pipe(res);
     } catch (e) {
+      if (e instanceof Error400) {
+        throw e;
+      }
       logger.warn(e);
       throw new Error404('FILE_NOT_FOUND');
     }

programming_language: TypeScript
filename: validateStreamParams.js
change_type: ModificationType.ADD
diff: @@ -0,0 +1,28 @@
+const { Error400 } = require('../../../utils/httpErrors');
+
+const SESSION_ID_REGEX = /^camera-[a-zA-Z0-9-_]+$/;
+
+// Session_id usually looks like "camera-7835d25d-b8ce-4824-a235-23637f778f83-39-50-13"
+const validateSessionId = (sessionId) => {
+  if (!SESSION_ID_REGEX.test(sessionId)) {
+    throw new Error400('Invalid session id');
+  }
+};
+
+const AUTHORIZED_FILENAMES = ['index.m3u8', 'index.m3u8.key', 'key_info_file.txt'];
+const HLS_CHUNK_REGEX = /index[0-9]+.ts/;
+
+const validateFilename = (filename) => {
+  if (AUTHORIZED_FILENAMES.includes(filename)) {
+    return;
+  }
+
+  if (!HLS_CHUNK_REGEX.test(filename)) {
+    throw new Error400('Invalid filename');
+  }
+};
+
+module.exports = {
+  validateSessionId,
+  validateFilename,
+};

programming_language: JavaScript
filename: rtspCamera.controller.test.js
change_type: ModificationType.MODIFY
diff: @@ -99,7 +99,7 @@ describe('camera controller test', () => {
     );
     assert.calledWith(rtspCameraService.liveActivePing, 'my-camera');
   });
-  it('should get streaming file', async () => {
+  it('should get index.m3u8 file', async () => {
     const rtspCameraController = RtspCameraController(gladys, rtspCameraService);
     const req = {
       params: {
@@ -115,12 +115,28 @@ describe('camera controller test', () => {
       resWriteStream,
     );
   });
+  it('should get index1.ts file', async () => {
+    const rtspCameraController = RtspCameraController(gladys, rtspCameraService);
+    const req = {
+      params: {
+        folder: 'camera-1',
+        file: 'index1.ts',
+      },
+    };
+    await fse.ensureDir(path.join(gladys.config.tempFolder, 'camera-1'));
+    await fse.writeFile(path.join(gladys.config.tempFolder, 'camera-1', 'index1.ts'), 'test-toto-content');
+    const resWriteStream = fse.createWriteStream(path.join(gladys.config.tempFolder, 'camera-1', 'result.txt'));
+    await rtspCameraController['get /api/v1/service/rtsp-camera/camera/streaming/:folder/:file'].controller(
+      req,
+      resWriteStream,
+    );
+  });
   it('should return 404, file not found (res.status) ', async () => {
     const rtspCameraController = RtspCameraControllerWithFsMocked(gladys, rtspCameraService);
     const req = {
       params: {
         folder: 'camera-1',
-        file: 'FILE_NOT_FOUND',
+        file: 'index12212.ts',
       },
     };
     const resWriteStream = fse.createWriteStream(path.join(gladys.config.tempFolder, 'camera-1', 'result.txt'));
@@ -147,4 +163,34 @@ describe('camera controller test', () => {
     );
     await chaiAssert.isRejected(promise, 'FILE_NOT_FOUND');
   });
+  it('should return 400, bad request, invalid filename', async () => {
+    const rtspCameraController = RtspCameraController(gladys, rtspCameraService);
+    const req = {
+      params: {
+        folder: 'camera-1',
+        file: 'lalalalala',
+      },
+    };
+    const resWriteStream = {};
+    const promise = rtspCameraController['get /api/v1/service/rtsp-camera/camera/streaming/:folder/:file'].controller(
+      req,
+      resWriteStream,
+    );
+    await chaiAssert.isRejected(promise, 'Invalid filename');
+  });
+  it('should return 400, bad request, invalid session id', async () => {
+    const rtspCameraController = RtspCameraController(gladys, rtspCameraService);
+    const req = {
+      params: {
+        folder: '.....',
+        file: 'lalalalala',
+      },
+    };
+    const resWriteStream = {};
+    const promise = rtspCameraController['get /api/v1/service/rtsp-camera/camera/streaming/:folder/:file'].controller(
+      req,
+      resWriteStream,
+    );
+    await chaiAssert.isRejected(promise, 'Invalid session id');
+  });
 });

programming_language: TypeScript