Skip to content

Conversation

@its-iris
Copy link

No description provided.

@JellyBrick
Copy link
Collaborator

BG does not run on node:vm.

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR refactors the downloader plugin to use Node.js's vm module instead of the Function constructor for executing untrusted scripts from YouTube's APIs. The goal is to improve security by sandboxing script execution.

Changes:

  • Replaced Function constructor with vm.runInNewContext for executing YouTube cipher scripts in Platform.shim.eval
  • Changed Platform.shim.eval from async to synchronous function
  • Applied vm.runInNewContext for executing interpreter JavaScript from background challenges

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +76 to +79
return vm.runInNewContext(
`(() => { ${code} })()`,
Object.create(null) as vm.Context,
) as Types.EvalResult;
Copy link

Copilot AI Jan 21, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The vm.runInNewContext is being called with an empty context (Object.create(null)), but the code being executed references 'exportedVars' which is defined in 'data.output'. Since the context is empty, this will throw a ReferenceError when the code tries to access 'exportedVars.nFunction' or 'exportedVars.sigFunction'. The context should either include the necessary variables from data.output or the implementation needs to be restructured to properly evaluate the code with its dependencies.

Copilot uses AI. Check for mistakes.
// Maybe there is a better way to do this (e.g. https://github.com/Siubaak/sval ?)
// eslint-disable-next-line @typescript-eslint/no-implied-eval,@typescript-eslint/no-unsafe-call
new Function(interpreterJavascript)();
vm.runInNewContext(interpreterJavascript, globalThis);
Copy link

Copilot AI Jan 21, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using globalThis as the context for vm.runInNewContext defeats the purpose of sandboxing. This gives the untrusted interpreterJavascript code full access to the Node.js global scope, including process, require, and other sensitive APIs. This is a critical security vulnerability. Instead, use an isolated context object that only includes the specific properties needed (window and document), similar to how Object.create(null) is used elsewhere. Consider using vm.createContext with a restricted sandbox object.

Copilot uses AI. Check for mistakes.
Comment on lines +68 to +72
if (env.n) {
properties.push(`n: exportedVars.nFunction("${env.n}")`);
}

if (env.sig) {
properties.push(`sig: exportedVars.sigFunction("${env.sig}")`)
properties.push(`sig: exportedVars.sigFunction("${env.sig}")`);
Copy link

Copilot AI Jan 21, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The env.n and env.sig values are directly interpolated into the code string without sanitization or escaping. If these values contain special characters like quotes or backslashes, they could break out of the string literals and inject arbitrary code. Consider using proper escaping or a different approach that doesn't rely on string interpolation to construct the code.

Copilot uses AI. Check for mistakes.
const ffmpegMutex = new Mutex();

Platform.shim.eval = async (data: Types.BuildScriptResult, env: Record<string, Types.VMPrimative>) => {
Platform.shim.eval = (
Copy link

Copilot AI Jan 21, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The function signature was changed from async to synchronous, removing the async keyword. If the youtubei.js library expects this Platform.shim.eval function to return a Promise (as indicated by the original async signature), this change will break the API contract and cause type errors or runtime issues. Verify whether the library can handle a synchronous return value.

Suggested change
Platform.shim.eval = (
Platform.shim.eval = async (

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants