Setup Node refactor (the secret-bearing fields no longer exist on the
node at all):
• Removed app_key / app_secret / auth_code from INPUT_TYPES. The
Setup Node now exposes only storage_method, encryption_key_method,
and reconnect — none of which are credentials.
• Added a "Set credentials..." button on the node (LiteGraph button
widget installed via beforeRegisterNodeDef + onNodeCreated
prototype hook so it survives reload, drag-and-drop workflow
JSON, and PNG metadata restore).
• Click opens a browser-only modal with three <input type=password>
fields. Browser-native dot masking the whole time the user types
or pastes — no canvas-rendered plaintext, no shoulder-surf.
• Modal Save POSTs values to a new same-origin route
/dropsend/setup/stash and closes. setup() consumes the stash
entry on the next Queue (one-shot, keyed by client_id, 60-second
TTL, 32-entry cap with oldest-eviction).
• Architectural consequence: secrets never become widget values, so
they cannot enter saved workflow JSON, ComfyUI's localStorage
auto-save, PNG metadata, copy-pasted nodes, or PromptServer.history
(and therefore the unauthenticated /history HTTP endpoint).
Auto-curl auth flow (one-click second paste):
• After Run 1 prints the OAuth URL banner, setup() sends a
sid-targeted dropsend_credentials_needed WebSocket event.
• JS handler auto-opens the entry modal in the originating browser
so the user can paste auth_code as soon as Dropbox shows it,
without clicking the button between runs.
• Defense in depth: client_id is echoed in the WS payload and the
JS handler verifies it matches api.clientId before opening.
Security hardening on /dropsend/setup/stash:
• Same-origin enforcement: Origin header must match request.host.
Closes cross-origin CSRF from another browser tab even when an
attacker has sniffed a victim's client_id.
• Content-Type lockdown: rejects anything other than
application/json with HTTP 415. Closes the confused-deputy class
where form-encoded bodies could be parsed as JSON.
• Live-session check: client_id must correspond to an active
PromptServer.instance.sockets entry. Returns 403 otherwise.
Closes the stash-poisoning vector where an attacker who guesses
a victim's client_id could pre-populate credentials.
• Per-IP rate limit: 30 POSTs / 10-second sliding window, returns
429 over budget. Defense against stash-spam DoS attempting to
evict a legitimate user's entry.
• Body cap (32 KB), client_id regex validation, JSON parse errors
return 400, empty payloads dropped.
• Both rate-limit and live-session checks empirically verified via
curl smoke tests.
Reconnect now revokes the Dropbox refresh token at the source:
• reconnect=True populates DropboxAuthManager from os.environ and
calls reset(revoke_token=True).
• POST /2/auth/token/revoke with an 8-second timeout so a Dropbox
outage cannot hang the operator mid-reconnect. Local cleanup
proceeds regardless of revocation outcome.
• Revocation outcome is now logged at logger.error level (not just
print) and the failure message tells the operator to manually
disconnect at https://www.dropbox.com/account/connected_apps so
a silent revocation failure cannot leave a still-valid token at
Dropbox.
Logging:
• Single FileHandler attached to the root logger from __init__.py,
pointed at a plugin-relative dropsend.log. Replaces the four
duplicated logging.basicConfig() blocks in monitor_output.py /
encrypt_file.py / dropbox_upload.py / dropsend_uploader_node.py
that were dead code (basicConfig is a no-op once ComfyUI has
already configured root). Log records from every DropSend module
now actually persist to disk.
• Race-free creation: pre-create with os.open(O_CREAT | O_WRONLY |
O_APPEND, 0o600) before FileHandler attaches, follow-up chmod as
belt-and-suspenders. Closes the brief default-umask window
between FileHandler creating the file and post-hoc chmod.
• Rotation via RotatingFileHandler (5 MB per file, 3 backups, ~20
MB max). Bounds disk use on long-lived hosts.
• dropbox_auth_manager.py now has its own logger so revocation
successes/failures land in dropsend.log alongside stdout.
Tests (tests/ directory, unittest, no extra deps):
• test_safe_paths.py — 23 tests covering symlink rejection, path-
traversal rejection, allowlist behaviour, dropbox_dest validation
(NUL bytes, missing leading slash, parent-traversal segments),
and resolve_safe_watch_folder edge cases. safe_paths is the only
barrier between remote workflow inputs and arbitrary-file-read
on the host; this is regression coverage for that barrier.
• test_setup_node_security.py — static AST analysis of
dropsend_setup_node.py asserting that every send_sync call for a
credential event passes a non-None sid AND sits inside an
"if sid:" guard. Catches future regressions of the original
WebSocket-broadcast vulnerability deterministically. Also asserts
INPUT_TYPES never re-introduces app_key/app_secret/auth_code.
WebSocket-broadcast guard maintained on all three event sites
(dropsend_credentials_ready, dropbox_reconnect_complete,
dropsend_credentials_needed) — refuse to send when client_id is None
to prevent send_sync(..., None) from broadcasting credentials to
every connected browser tab.
README:
• New Changelog section documenting the v1.1.0 security advisory
(workflow-persistence + WS-broadcast leaks in earlier versions)
and the fixes shipped in this release.
• New "Bind ComfyUI to localhost or front it with auth" Best
Practice item (item 4) explicitly calling out that ComfyUI's
/prompt endpoint is unauthenticated.
• New "Encryption key rotation" subsection covering symmetric-key
implications, leaked-key recovery, and how to generate a new key.
• New "Uninstalling / disconnecting" subsection with step-by-step
instructions for revoking Dropbox app, resetting app secret,
removing platform secrets, and handling existing .enc files.
• Threat model reflects same-origin/Content-Type checks, log
rotation, plugin-directory metadata-leak note for multi-tenant
hosts, reconnect-revocation silent-failure note, and Dropbox
SDK error-message log review.
.gitignore:
• Expanded to cover *.bak, *.enc, *.key/.pem/.p12/.pfx, .idea/,
.pytest_cache/, build artifacts, Thumbs.db/Desktop.ini.
Bundled in this minor release:
• Single-Observer fix for the macOS _fsevents conflict that
previously prevented .enc files from being picked up by the
upload watcher.
• folder_format dropdown for AutoUploader (project_name default
plus five worldwide date formats), with two-base persistence so
switching between project-name and dated modes preserves both
patterns.
• RUNNING banner with per-setting plain-English explanations.
• Branded watching/encrypted/sent log emojis.
• Per-shell copy-paste commands and echo verification for the
COMFYUI_DROPSEND_ALLOW_SETUP gate.
129 lines
5.1 KiB
Python
129 lines
5.1 KiB
Python
# ComfyUI_DropSendNode/encrypt_file.py
|
|
import os
|
|
import time
|
|
import logging
|
|
from queue import Queue
|
|
from watchdog.observers import Observer
|
|
from watchdog.events import FileSystemEventHandler
|
|
from cryptography.fernet import Fernet
|
|
import threading
|
|
from .safe_paths import is_safe_event_path
|
|
|
|
# Logging configured by the package __init__ — see __init__.py.
|
|
logger = logging.getLogger(__name__)
|
|
|
|
ENCRYPT_EXTENSIONS = ('.png', '.jpg', '.jpeg', '.webp', '.gif', '.mp4', '.avi', '.mov')
|
|
_stop_queue_processor = False # Stop signal for queue processor
|
|
|
|
|
|
def get_encryption_key():
|
|
"""
|
|
Get the encryption key from environment variable.
|
|
Checks multiple possible key names for compatibility.
|
|
|
|
Key retrieval order:
|
|
- COMFYUI_ENCRYPTION_KEY (uppercase - preferred)
|
|
- comfyui_encryption_key (lowercase - legacy)
|
|
- DROPSEND_ENCRYPTION_KEY
|
|
- DRIVESEND_ENCRYPTION_KEY
|
|
- RUNPOD_SECRET_COMFYUI_ENCRYPTION_KEY (RunPod fallback)
|
|
|
|
Returns:
|
|
str: The encryption key, or None if not set
|
|
"""
|
|
return (
|
|
os.environ.get('COMFYUI_ENCRYPTION_KEY') or
|
|
os.environ.get('comfyui_encryption_key') or
|
|
os.environ.get('DROPSEND_ENCRYPTION_KEY') or
|
|
os.environ.get('DRIVESEND_ENCRYPTION_KEY') or
|
|
os.environ.get('RUNPOD_SECRET_COMFYUI_ENCRYPTION_KEY')
|
|
)
|
|
|
|
|
|
def stop_queue_processor():
|
|
"""Signal the queue processor to stop."""
|
|
global _stop_queue_processor
|
|
_stop_queue_processor = True
|
|
logger.info("Signaled encryption queue processor to stop")
|
|
|
|
|
|
class FileEncryptHandler(FileSystemEventHandler):
|
|
def __init__(self, watch_dir="/workspace/ComfyUI/output", delete_original=False, subfolder_monitor=True):
|
|
self.watch_dir = watch_dir
|
|
self.delete_original = delete_original
|
|
self.subfolder_monitor = subfolder_monitor
|
|
self.file_queue = Queue()
|
|
self.start_queue_processor()
|
|
|
|
def on_created(self, event):
|
|
if event.is_directory:
|
|
return
|
|
# Defense-in-depth: refuse symlinks or paths whose realpath escapes
|
|
# the allowed output roots, so a symlink in the watched directory
|
|
# cannot trick the encryptor into reading and uploading a file
|
|
# outside the tree.
|
|
if not is_safe_event_path(event.src_path):
|
|
logger.warning(f"Skipping unsafe path (symlink or outside output root): {event.src_path}")
|
|
return
|
|
if event.src_path.lower().endswith(ENCRYPT_EXTENSIONS):
|
|
logger.info(f"Detected new file to encrypt: {event.src_path}")
|
|
self.file_queue.put(event.src_path)
|
|
|
|
def start_queue_processor(self):
|
|
global _stop_queue_processor
|
|
# Reset the stop flag so that a new handler created after a prior
|
|
# stop_queue_processor() call still has a working queue worker.
|
|
# Mirrors what start_monitoring() does on the upload side.
|
|
_stop_queue_processor = False
|
|
def process_queue():
|
|
while not _stop_queue_processor:
|
|
if not self.file_queue.empty():
|
|
file_path = self.file_queue.get()
|
|
try:
|
|
time.sleep(1) # Ensure file is fully written
|
|
size1 = os.path.getsize(file_path)
|
|
time.sleep(0.1)
|
|
size2 = os.path.getsize(file_path)
|
|
if size1 != size2:
|
|
logger.warning(f"File {file_path} still writing, requeueing...")
|
|
self.file_queue.put(file_path)
|
|
continue
|
|
|
|
ENCRYPT_KEY = get_encryption_key()
|
|
if not ENCRYPT_KEY:
|
|
logger.error("Encryption key not found. Set COMFYUI_ENCRYPTION_KEY environment variable.")
|
|
continue
|
|
fernet = Fernet(ENCRYPT_KEY.encode())
|
|
|
|
with open(file_path, 'rb') as f:
|
|
file_data = f.read()
|
|
|
|
encrypted_data = fernet.encrypt(file_data)
|
|
enc_path = file_path + '.enc'
|
|
with open(enc_path, 'wb') as f:
|
|
f.write(encrypted_data)
|
|
logger.info(f"📦🔐 Encrypted: {enc_path}")
|
|
|
|
# Defer deletion until after upload verification (handled in monitor_output.py)
|
|
except Exception as e:
|
|
logger.error(f"Error encrypting {file_path}: {e}")
|
|
time.sleep(0.1) # Small delay to avoid CPU overload
|
|
logger.info("Encryption queue processor stopped")
|
|
|
|
threading.Thread(target=process_queue, daemon=True).start()
|
|
|
|
|
|
if __name__ == "__main__":
|
|
watch_dir = os.getenv("WATCH_DIR", "/workspace/ComfyUI/output")
|
|
os.makedirs(watch_dir, exist_ok=True)
|
|
observer = Observer()
|
|
observer.schedule(FileEncryptHandler(watch_dir), path=watch_dir, recursive=True)
|
|
observer.start()
|
|
logger.info(f"Monitoring {watch_dir} for new files to encrypt...")
|
|
try:
|
|
while True:
|
|
time.sleep(1)
|
|
except KeyboardInterrupt:
|
|
observer.stop()
|
|
observer.join()
|