Skip to content

Conversation

@deepanjanroy
Copy link
Contributor

Design

  • You can turn on the sandbox using --enable-sandbox, only on windows.
  • It checks for correct directory permissions at startup
  • If using the default directory structure, it offers to set up the permissions automatically. Users get an elevation request (see pics below). The elevated privileges only apply to the setup_sandbox_permissions.bat file. This should be a one time thing.
  • The directories user, custom_nodes, and output are lowered in integrity level, along with a new directory write-permitted.
    • Making custom_nodes low integrity means any custom nodes that want to write to its own directory continues functioning. Tested with comfyui-manager. It also means if there is a binary inside a custom_node it will always run with low integrity.
    • The temp directory is moved inside the new write-permitted folder because we have code that deletes and recreated the entire directory.
    • If we ever want to create new places we want to write to, we can just put it inside write-permitted to keep things simple.

Manual tests performed:

  • The default comfy workflow can generate outputs
  • ComfyUI manager seems to work fine although I haven't tested it too hard
  • User settings persist across update
  • Works even if ComfyUI is in D: drive
  • If --enable-sandbox is not passed, it does not require pywin32 (so it also should not break non-windows platforms)

Work to do in follow up PRs:

  • Support overriding directories (e.g. --output-directory)
  • Enable it by default / add the --enable-sandbox to bat file for portable
image

image

(^ this is a photo because it's hard to take a screenshot of the elevation screen)

@yoland68 yoland68 added Core Core team dependency Run-CI-Test This is an administrative label to tell the CI to run full automatic testing on this PR now. labels Jun 11, 2025
@github-actions
Copy link

(Automated Bot Message) CI Tests are running, you can view the results at https://ci.comfy.org/?branch=8351%2Fmerge

pass


print("Copying windows base files")
Copy link
Owner

Choose a reason for hiding this comment

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

This block should go in a separate PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done. Also note that I pulled the subprocess import to the top of the file - hope that's ok.

Copy link

@maludwig maludwig left a comment

Choose a reason for hiding this comment

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

I am unclear about the purpose of sandbox mode, but currently, it seems that if a custom node contained malicious code, it could overwrite the code of every other custom node by writing over the .py files. The permissions script will not work if there are spaces in the comfyui install path.

LOW_INTEGRITY_SID_STRING = "S-1-16-4096"

# Use absolute path to prevent command injection
ICACLS_PATH = r"C:\Windows\System32\icacls.exe"

Choose a reason for hiding this comment

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

This assumes that Windows is installed there, recommend using:

ICACLS_PATH = os.path.join(win32api.GetSystemDirectory(), "icacls.exe")

import folder_paths


LOW_INTEGRITY_SID_STRING = "S-1-16-4096"

Choose a reason for hiding this comment

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

Consider adding a comment here explaining what "S-1-16-4096" means.

@@ -0,0 +1,172 @@
import logging

Choose a reason for hiding this comment

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

Consider adding a docstring to the top of the file to explain the file, since it's pretty far off in the weeds of win32. Perhaps something like:

"""
Sandboxing Utilities for Low-Integrity Process Execution on Windows
===================================================================

This module enables a sandboxed execution environment by lowering the
integrity level of the current process to "Low", which restricts file system
and registry access for enhanced security. It ensures that specific
directories are writable even under this constrained execution mode.
"""

ICACLS_PATH = r"C:\Windows\System32\icacls.exe"


def set_process_integrity_level_to_low():

Choose a reason for hiding this comment

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

Consider docstrings for each of these, since they're modestly advances win32, like:

    """
    Set the current process to run at a low integrity level.

    This restricts the process's permissions, creating a sandboxed environment
    to limit potential damage if compromised.
    """

Checks if an icacls output indicates that the path is writable by low
integrity processes.
Note that currently it is a bit of a crude check - it is possible for

Choose a reason for hiding this comment

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

I would elaborate on when this fails, if you know offhand.

openapi.yaml
filtered-openapi.yaml
uv.lock
/write-permitted/

Choose a reason for hiding this comment

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

Presumably also remove /temp/, if you're moving it. I wouldn't personally remove temp, see my later comment in folder_paths.py

"--enable-sandbox",
default=False,
action="store_true",
help="Enable sandbox mode.",

Choose a reason for hiding this comment

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

I would elaborate in this help entry about what use case sandbox mode is intended for. Some people might assume that it would be a security feature that allows them to safely execute malicious code. Consider a more specific name, like: "--limit-write-access" or something, rather than "sandbox".

# Note: If we ever support custom directories, we should warn users if
# the directories are in a senstive location (e.g. a high level
# directory like C:\ or the user's home directory).
raise Exception("Sandbox mode is not supported when using --output-directory, "

Choose a reason for hiding this comment

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

This is a really good note. Setting the user's entire home directory to Low would be horrendous.

from sandbox import windows_sandbox
try_enable_sandbox()
else:
logging.warning("Sandbox mode is not supported on non-windows platforms."

Choose a reason for hiding this comment

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

Shouldn't this be an error?

proc_info = shell.ShellExecuteEx(**execute_info)
hProcess = proc_info["hProcess"]

# Setup script should less than a second. Time out at 10 seconds.

Choose a reason for hiding this comment

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

If the user hums and has at the UAC prompt for a moment, or the ACLs take a while to apply, then do you really want to quit halfway through? I would consider a much longer timeout.

@Baughn
Copy link
Contributor

Baughn commented Dec 7, 2025

Related: #11013 kinda does this on Linux. I just spotted your PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Core Core team dependency Run-CI-Test This is an administrative label to tell the CI to run full automatic testing on this PR now.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants