Rembg is a tool to remove images background
Fix code scanning alerts: workflow permissions, SSRF redirects, Gradio path handling
Daniel Gatis0be7f2d20c75
diff --git a/.github/workflows/lint_python.yml b/.github/workflows/lint_python.ymlindex de666cc..52accac 100644--- a/.github/workflows/lint_python.yml+++ b/.github/workflows/lint_python.yml@@ -6,6 +6,9 @@ on:- "**"pull_request:+permissions:+ contents: read+jobs:lint_python:runs-on: ubuntu-latestdiff --git a/.github/workflows/publish_docker.yml b/.github/workflows/publish_docker.ymlindex e372fd3..f79f295 100644--- a/.github/workflows/publish_docker.yml+++ b/.github/workflows/publish_docker.yml@@ -5,6 +5,9 @@ on:tags:- "v*.*.*"+permissions:+ contents: read+jobs:publish_docker:name: Push Docker image to Docker Hubdiff --git a/.github/workflows/publish_pypi.yml b/.github/workflows/publish_pypi.ymlindex 3da1162..1332267 100644--- a/.github/workflows/publish_pypi.yml+++ b/.github/workflows/publish_pypi.yml@@ -5,6 +5,9 @@ on:tags:- "v*.*.*"+permissions:+ contents: read+jobs:publish_pypi:runs-on: ubuntu-latestdiff --git a/.github/workflows/windows_installer.yml b/.github/workflows/windows_installer.ymlindex 9b821d7..3629224 100644--- a/.github/workflows/windows_installer.yml+++ b/.github/workflows/windows_installer.yml@@ -4,6 +4,10 @@ on:push:tags:- "v*.*.*"++permissions:+ contents: write+jobs:windows_installer:name: Build the Inno Setup Installerdiff --git a/rembg/commands/s_command.py b/rembg/commands/s_command.pyindex 4e96f14..78d6b96 100644--- a/rembg/commands/s_command.py+++ b/rembg/commands/s_command.py@@ -1,10 +1,9 @@import ipaddressimport json-import osimport socketimport webbrowserfrom typing import Optional, Tuple, cast-from urllib.parse import urlparse+from urllib.parse import urljoin, urlparseimport aiohttpimport click@@ -285,9 +284,27 @@ def s_command(port: int, host: str, log_level: str, threads: int, no_ui: bool) -raise HTTPException(status_code=400, detail=str(e))async with aiohttp.ClientSession() as session:- async with session.get(url) as response:- file = await response.read()- return await asyncify(im_without_bg)(file, commons)+ current_url = url+ for _ in range(5):+ async with session.get(current_url, allow_redirects=False) as response:+ if response.status in (301, 302, 303, 307, 308):+ location = response.headers.get("Location")+ if not location:+ raise HTTPException(+ status_code=400,+ detail="Redirect without Location header.",+ )+ current_url = urljoin(current_url, location)+ try:+ _validate_url(current_url)+ except ValueError as e:+ raise HTTPException(status_code=400, detail=str(e))+ continue++ file = await response.read()+ return await asyncify(im_without_bg)(file, commons)++ raise HTTPException(status_code=400, detail="Too many redirects.")@app.post(path="/api/remove",@@ -305,8 +322,7 @@ def s_command(port: int, host: str, log_level: str, threads: int, no_ui: bool) -return await asyncify(im_without_bg)(file, commons) # type: ignoredef gr_app(app):- def inference(input_path, model, *args):- output_path = "output.png"+ def inference(input_image, model, *args):a, af, ab, ae, om, ppm, cmd_args = argskwargs = {@@ -327,17 +343,12 @@ def s_command(port: int, host: str, log_level: str, threads: int, no_ui: bool) -sessions[model] = sessionkwargs["session"] = session- with open(input_path, "rb") as i:- with open(output_path, "wb") as o:- input = i.read()- output = remove(input, **kwargs)- o.write(output)- return os.path.join(output_path)+ return remove(input_image, **kwargs)interface = gr.Interface(inference,[- gr.components.Image(type="filepath", label="Input"),+ gr.components.Image(type="pil", label="Input"),gr.components.Dropdown(sessions_names, value="u2net", label="Models"),gr.components.Checkbox(value=True, label="Alpha matting"),gr.components.Slider(@@ -353,7 +364,7 @@ def s_command(port: int, host: str, log_level: str, threads: int, no_ui: bool) -gr.components.Checkbox(value=True, label="Post process mask"),gr.components.Textbox(label="Arguments"),],- gr.components.Image(type="filepath", label="Output"),+ gr.components.Image(type="pil", label="Output"),concurrency_limit=3,analytics_enabled=False,)