Skip to content

Commit 738105a

Browse files
committed
Ensure resource look ups exist under their respective directory. Fixes #725
1 parent 683836c commit 738105a

3 files changed

Lines changed: 28 additions & 10 deletions

File tree

py/server/utils_server.py

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import os
22
from aiohttp import web
33

4+
from ..utils import sub_abspath
5+
46
THIS_DIR = os.path.dirname(os.path.abspath(__file__))
57
DIR_WEB = os.path.abspath(f'{THIS_DIR}/../../web/')
68

@@ -23,18 +25,25 @@ def is_param_truthy(request, param):
2325

2426

2527
def set_default_page_resources(path, routes):
26-
""" Sets up routes for handling static files under a path."""
28+
"""Sets up routes for handling static files under a path."""
2729

2830
@routes.get(f'/rgthree/{path}/{{file}}')
2931
async def get_resource(request):
30-
""" Returns a resource file. """
31-
return web.FileResponse(os.path.join(DIR_WEB, path, request.match_info['file']))
32+
"""Returns a resource file."""
33+
filepath = request.match_info['file']
34+
abspath = sub_abspath(os.path.join(DIR_WEB, path), filepath)
35+
if abspath is None:
36+
return web.HTTPNotFound()
37+
return web.FileResponse(abspath)
3238

3339
@routes.get(f'/rgthree/{path}/{{subdir}}/{{file}}')
3440
async def get_resource_subdir(request):
35-
""" Returns a resource file. """
36-
return web.FileResponse(
37-
os.path.join(DIR_WEB, path, request.match_info['subdir'], request.match_info['file']))
41+
"""Returns a resource file."""
42+
filepath = os.path.join(request.match_info['subdir'], request.match_info['file'])
43+
abspath = sub_abspath(os.path.join(DIR_WEB, path), filepath)
44+
if abspath is None:
45+
return web.HTTPNotFound()
46+
return web.FileResponse(abspath)
3847

3948

4049
def set_default_page_routes(path, routes):

py/utils.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -152,10 +152,19 @@ def remove_path(path):
152152

153153
def abspath(file_path: str):
154154
"""Resolves the abspath of a file, resolving symlinks and user dirs."""
155-
if file_path and not path_exists(file_path):
155+
abs_path = os.path.abspath(file_path) if file_path else file_path
156+
if abs_path and not path_exists(abs_path):
156157
maybe_path = os.path.abspath(os.path.realpath(os.path.expanduser(file_path)))
157-
file_path = maybe_path if path_exists(maybe_path) else file_path
158-
return file_path
158+
abs_path = maybe_path if path_exists(maybe_path) else abs_path
159+
return abs_path
160+
161+
def sub_abspath(parent_dir: str, rel_path: str):
162+
"""Resolves the abspath under a parent directory ensuring it exists and is contained within."""
163+
rel_path = os.path.join(parent_dir, rel_path)
164+
abs_path = abspath(rel_path)
165+
if not path_exists(abs_path) or not abs_path.startswith(parent_dir):
166+
return None
167+
return abs_path
159168

160169
class ByPassTypeTuple(tuple):
161170
"""A special class that will return additional "AnyType" strings beyond defined values.

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[project]
22
name = "rgthree-comfy"
33
description = "Making ComfyUI more comfortable."
4-
version = "1.0.2604070017"
4+
version = "1.0.2605082257"
55
license = { file = "LICENSE" }
66
dependencies = []
77

0 commit comments

Comments
 (0)