GitHub Security Advisory: GHSA-55v6-g8pm-pw4c
Package: rembg (PIP)
Severity: MODERATE (CVSS 4.3)
CWE: CWE-918: Server-Side Request Forgery (SSRF)
Vulnerable versions: < 2.0.75
Patched in: 2.0.75

Summary: rembg server is vulnerable to Server-Side Request Forgery (SSRF) and a weak default CORS configuration

Details:
# GitHub Security Lab (GHSL) Vulnerability Report, rembg: `GHSL-2024-161`, `GHSL-2024-162`

The [GitHub Security Lab](https://securitylab.github.com) team has identified potential security vulnerabilities in [rembg](https://github.com/danielgatis/rembg).

We are committed to working with you to help resolve these issues. In this report you will find everything you need to effectively coordinate a resolution of these issues with the GHSL team.

If at any point you have concerns or questions about this process, please do not hesitate to reach out to us at `securitylab@github.com` (please include `GHSL-2024-161` or `GHSL-2024-162` as a reference). See also [this blog post](https://github.blog/2022-04-22-removing-the-stigma-of-a-cve/) written by GitHub's Advisory Curation team which explains what CVEs and advisories are, why they are important to track vulnerabilities and keep downstream users informed, the CVE assigning process, and how they are used to keep open source software secure.

If you are _NOT_ the correct point of contact for this report, please let us know!

## Summary

rembg server is vulnerable to Server-Side Request Forgery (SSRF) and a weak default CORS configuration, which may allow an attacker website to send requests to servers on the internal network and view image responses.

## Project

rembg

## Tested Version

[v2.0.57](https://github.com/danielgatis/rembg/releases/tag/v2.0.57)

## Details

### Issue 1: SSRF via `/api/remove` (`GHSL-2024-161`)

The [`/api/remove`](https://github.com/danielgatis/rembg/blob/d1e00734f8a996abf512a3a5c251c7a9a392c90a/rembg/commands/s_command.py#L237) endpoint takes a URL query parameter that allows an image to be fetched, processed and returned. An attacker may  be able to query this endpoint to view pictures hosted on the internal network of the rembg server.

```python
 async def get_index(
        url: str = Query(
            default=..., description="URL of the image that has to be processed."
        ),
        commons: CommonQueryParams = Depends(),
    ):
        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)
```

#### Impact

This issue may lead to `Information Disclosure`.

#### Remediation

Ensure that the IP address specified is not a local address. If resolving a domain name, ensure that the resolved IP address is not local.

#### Proof of Concept

`curl -s "http://localhost:7000/api/remove?url=http://0.0.0.0/secret.png" -o output.png`


### Issue 2: CORS misconfiguration (`GHSL-2024-162`)

The following [CORS middleware](https://github.com/danielgatis/rembg/blob/d1e00734f8a996abf512a3a5c251c7a9a392c90a/rembg/commands/s_command.py#L93) is setup incorrectly. All origins are reflected, which allows any website to send cross site requests to the rembg server and thus query any API. Even if authentication were to be enabled,