Skip to content

Latest commit

 

History

History
161 lines (125 loc) · 12.4 KB

File metadata and controls

161 lines (125 loc) · 12.4 KB

Mitigation Strategies Analysis for ffmpegwasm/ffmpeg.wasm

Mitigation Strategy: Resource Limits

Mitigation Strategy: Resource Limits (Memory, CPU, Filesystem)

Description:

  1. Memory Limits:
    • When initializing the ffmpeg.wasm WebAssembly module, specify a maximum memory limit using the memory option in the configuration passed to createFFmpeg. Start with a conservative value (e.g., 256MB) and increase it only if necessary based on testing with representative input files. This directly controls the memory allocated to the ffmpeg.wasm instance.
    • Implement a monitoring mechanism (e.g., using performance.memory in JavaScript, if available, or by periodically checking the WebAssembly instance's memory usage) to track the memory consumption of the ffmpeg.wasm module.
    • If the memory usage exceeds the predefined limit, terminate the WebAssembly instance using ffmpeg.exit() or by destroying the Web Worker (if ffmpeg.wasm is running in a worker). Provide a user-friendly error message.
  2. CPU Time Limits:
    • Wrap calls to ffmpeg.run() (or any other ffmpeg.wasm functions that perform processing) in a Promise with a timeout. Use Promise.race() to combine the ffmpeg.run() promise with a timeout promise (created using setTimeout).
    • If the timeout promise resolves first, it indicates that the ffmpeg.wasm operation has exceeded the allowed time. Terminate the WebAssembly instance using ffmpeg.exit() or worker termination.
    • Adjust the timeout value based on testing with representative input files. Consider different timeouts for different operations (e.g., shorter timeouts for thumbnail generation, longer timeouts for full video transcoding).
  3. Filesystem Access Limits (if using virtual filesystem):
    • Before running ffmpeg.wasm, calculate the maximum expected size of the input and output files that will be stored in the virtual filesystem.
    • Use the FS.mkdir() and FS.writeFile() functions (from the ffmpeg.wasm virtual filesystem API) to create the necessary directories and files within the virtual filesystem.
    • Monitor the size of the virtual filesystem during processing. If the size exceeds the calculated maximum, terminate the WebAssembly instance using ffmpeg.exit().
    • Consider using a separate, temporary directory within the virtual filesystem for each ffmpeg.wasm operation to further isolate files. Use FS.rmdir() to clean up.

Threats Mitigated:

  • Denial of Service (DoS) - High Severity: Prevents attackers from crafting malicious inputs that cause excessive memory allocation or CPU consumption within the ffmpeg.wasm instance, crashing the browser tab or making the application unresponsive.
  • Resource Exhaustion - High Severity: Protects against scenarios where legitimate, but large or complex, input files could lead to resource exhaustion within the ffmpeg.wasm environment.

Impact:

  • DoS: Significantly reduces the risk of successful DoS attacks targeting ffmpeg.wasm.
  • Resource Exhaustion: Reduces the risk of performance degradation or application crashes due to legitimate, but resource-intensive, input files processed by ffmpeg.wasm.

Currently Implemented:

  • Example: Memory limits are set in src/workers/ffmpegWorker.js. CPU timeouts are implemented in src/components/VideoProcessor.js. Filesystem limits are not currently implemented.

Missing Implementation:

  • Filesystem limits are missing. They should be implemented in src/workers/ffmpegWorker.js, similar to the memory limit implementation. Monitoring of the ffmpeg.wasm virtual filesystem size needs to be added.

Mitigation Strategy: Input Validation (Format Whitelisting, Parameter Restrictions, Duration Limits)

Description:

  1. Format Whitelisting:
    • Create a constant array or configuration file that explicitly lists the allowed input and output formats (e.g., ['mp4', 'webm'] for containers, ['h264', 'vp9', 'aac'] for codecs). This directly restricts the formats passed to ffmpeg.wasm.
    • Before passing any input to ffmpeg.wasm, validate the input file's format against the whitelist.
    • Reject any input that does not match an allowed format.
    • Similarly, validate the desired output format against the whitelist before constructing the arguments for ffmpeg.run().
  2. Parameter Restrictions:
    • Create a configuration object that defines the allowed FFmpeg options and their permissible values. This directly controls the command-line arguments passed to ffmpeg.wasm. For example:
      const allowedOptions = {
        '-vf': ['scale=w=1280:h=720:force_original_aspect_ratio=decrease'], // Example: Limit resolution
        '-b:v': ['2M'], // Example: Limit video bitrate
        '-b:a': ['128k'], //Example: Limit audio bitrate
        '-r' : ['30'] //Example: Limit framerate
      };
    • Before constructing the arguments for ffmpeg.run(), validate each option and its value against the allowedOptions object.
    • Reject any input that attempts to use disallowed options or values with ffmpeg.wasm.
  3. Duration Limits:
    • If possible, obtain the duration of the input file before passing it to ffmpeg.wasm.
    • Compare the duration to a predefined maximum duration.
    • Reject any input that exceeds the maximum duration, preventing it from being processed by ffmpeg.wasm.

Threats Mitigated:

  • Denial of Service (DoS) - Medium Severity: Reduces the attack surface by limiting the number of code paths within FFmpeg (inside ffmpeg.wasm) that can be reached by an attacker.
  • Code Execution (within WebAssembly sandbox) - Low Severity: Limiting options and formats reduces the likelihood of triggering a vulnerability within ffmpeg.wasm that could lead to code execution.
  • Resource Exhaustion - Medium Severity: Duration limits prevent processing of excessively long files by ffmpeg.wasm.

Impact:

  • DoS: Significantly reduces the risk of DoS attacks by limiting the attack surface exposed by ffmpeg.wasm.
  • Code Execution: Reduces the risk, but does not eliminate it.
  • Resource Exhaustion: Effectively prevents resource exhaustion caused by excessively long input files processed by ffmpeg.wasm.

Currently Implemented:

  • Format whitelisting is partially implemented in src/utils/validation.js, but only checks file extensions. Parameter restrictions and duration limits are not implemented.

Missing Implementation:

  • src/utils/validation.js needs to use a more robust format detection method.
  • Parameter restrictions need to be implemented, likely in src/workers/ffmpegWorker.js or a dedicated validation module, directly controlling the arguments passed to ffmpeg.run().
  • Duration limits need to be implemented before calling ffmpeg.wasm functions.

Mitigation Strategy: Output Sanitization

Mitigation Strategy: Output Sanitization (of ffmpeg.wasm results)

Description:

  1. Treat Output as Untrusted: Assume that the output returned by ffmpeg.wasm could contain malicious data, even if the processing itself was successful.
  2. Context-Specific Sanitization: The specific sanitization steps depend on how the output data from ffmpeg.wasm is used.
    • If the output is displayed as text: Use a robust HTML sanitization library (e.g., DOMPurify) to remove any potentially harmful HTML tags or attributes before displaying the output.
    • If the output is used as a URL: Validate the URL using a URL parsing library and ensure it conforms to expected patterns.
    • If the output is used as data in your application: Validate the data against a strict schema or whitelist of allowed values.
  3. Error Handling: Ensure that error messages returned by ffmpeg.wasm are sanitized before being displayed to the user. Remove any potentially sensitive information, such as internal file paths from the WebAssembly environment.

Threats Mitigated:

  • Information Disclosure - Medium Severity: Prevents attackers from embedding sensitive information (e.g., memory contents from the ffmpeg.wasm instance) in the output.
  • Cross-Site Scripting (XSS) - High Severity (if output is displayed): Prevents XSS attacks if the output of ffmpeg.wasm is displayed without sanitization.

Impact:

  • Information Disclosure: Significantly reduces the risk.
  • XSS: Effectively prevents XSS attacks originating from ffmpeg.wasm output.

Currently Implemented:

  • No output sanitization is currently implemented.

Missing Implementation:

  • Output sanitization needs to be implemented wherever the output of ffmpeg.wasm is used. This includes components that display the processed video, metadata, or error messages.
  • Error message sanitization needs to be implemented where ffmpeg.wasm errors are handled.

Mitigation Strategy: Regular Updates

Mitigation Strategy: Regular Updates (ffmpeg.wasm itself)

Description:

  1. ffmpeg.wasm Updates:
    • Subscribe to release notifications for the ffmpeg.wasm project on GitHub.
    • Regularly check for new releases (e.g., weekly).
    • When a new release is available, update the ffmpeg.wasm dependency in your project's package.json file.
    • Run npm install (or equivalent) to install the updated version.
    • Thoroughly test your application after updating ffmpeg.wasm.
  2. FFmpeg Updates (Indirect):
    • Monitor security advisories for FFmpeg. Be aware of reported vulnerabilities, as these will eventually be addressed in ffmpeg.wasm updates.

Threats Mitigated:

  • Code Execution (within WebAssembly sandbox) - High Severity: Addresses known vulnerabilities in the underlying FFmpeg codebase within ffmpeg.wasm.
  • Denial of Service (DoS) - High Severity: Addresses known DoS vulnerabilities within ffmpeg.wasm.
  • Information Disclosure - Medium Severity: Addresses known information disclosure vulnerabilities within ffmpeg.wasm.

Impact:

  • All Threats: Significantly reduces the risk by patching known vulnerabilities in ffmpeg.wasm.

Currently Implemented:

  • No automated update process is in place.

Missing Implementation:

  • Implement a process for regularly checking for and applying updates to ffmpeg.wasm.

Mitigation Strategy: WebAssembly Instance Isolation

Mitigation Strategy: WebAssembly Instance Isolation (Multiple ffmpeg.wasm instances)

Description:

  1. If your application processes multiple media files, create a new ffmpeg.wasm WebAssembly instance for each file. This isolates the processing of each file.
  2. This can be achieved by: a. Creating a new Web Worker for each file and initializing ffmpeg.wasm (using createFFmpeg) within that worker. This is the recommended approach. b. Or, if using ffmpeg.wasm on the main thread, calling ffmpeg.createFFmpeg() to create a new instance each time, and ensuring you call ffmpeg.exit() on the previous instance when finished.
  3. Ensure that no data is shared between instances (e.g., through shared memory or global variables).

Threats Mitigated:

  • Code Execution (within WebAssembly sandbox) - Medium Severity: Limits the impact of a successful exploit within a single ffmpeg.wasm instance.
  • Information Disclosure - Medium Severity: Prevents a vulnerability in one ffmpeg.wasm instance from leaking information about other files.
  • Denial of Service - Low Severity: If one ffmpeg.wasm instance crashes, others remain operational.

Impact:

  • Code Execution/Information Disclosure: Reduces the blast radius of a successful attack against a single ffmpeg.wasm instance.
  • DoS: Provides some resilience.

Currently Implemented:

  • A single ffmpeg.wasm instance is used for all processing.

Missing Implementation:

  • Modify the code (e.g., src/workers/ffmpegWorker.js) to create a new ffmpeg.wasm instance (via createFFmpeg) for each file processing request. Ensure proper cleanup of each instance after processing is complete using ffmpeg.exit().