Skip to content

Commit 52885aa

Browse files
author
Max
committed
Add option forwarded_allow_ips
1 parent 2173760 commit 52885aa

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed

src/fastapi_cli/cli.py

+16
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ def _run(
6060
command: str,
6161
app: Union[str, None] = None,
6262
proxy_headers: bool = False,
63+
forwarded_allow_ips: Union[str, None] = None,
6364
) -> None:
6465
try:
6566
use_uvicorn_app = get_import_string(path=path, app_name=app)
@@ -97,6 +98,7 @@ def _run(
9798
workers=workers,
9899
root_path=root_path,
99100
proxy_headers=proxy_headers,
101+
forwarded_allow_ips=forwarded_allow_ips,
100102
)
101103

102104

@@ -145,6 +147,12 @@ def dev(
145147
help="Enable/Disable X-Forwarded-Proto, X-Forwarded-For, X-Forwarded-Port to populate remote address info."
146148
),
147149
] = True,
150+
forwarded_allow_ips: Annotated[
151+
Union[str, None],
152+
typer.Option(
153+
help="Comma separated list of IP Addresses to trust with proxy headers. The literal '*' means trust everything."
154+
),
155+
] = None,
148156
) -> Any:
149157
"""
150158
Run a [bold]FastAPI[/bold] app in [yellow]development[/yellow] mode. 🧪
@@ -180,6 +188,7 @@ def dev(
180188
app=app,
181189
command="dev",
182190
proxy_headers=proxy_headers,
191+
forwarded_allow_ips=forwarded_allow_ips,
183192
)
184193

185194

@@ -234,6 +243,12 @@ def run(
234243
help="Enable/Disable X-Forwarded-Proto, X-Forwarded-For, X-Forwarded-Port to populate remote address info."
235244
),
236245
] = True,
246+
forwarded_allow_ips: Annotated[
247+
Union[str, None],
248+
typer.Option(
249+
help="Comma separated list of IP Addresses to trust with proxy headers. The literal '*' means trust everything."
250+
),
251+
] = None,
237252
) -> Any:
238253
"""
239254
Run a [bold]FastAPI[/bold] app in [green]production[/green] mode. 🚀
@@ -270,6 +285,7 @@ def run(
270285
app=app,
271286
command="run",
272287
proxy_headers=proxy_headers,
288+
forwarded_allow_ips=forwarded_allow_ips,
273289
)
274290

275291

tests/test_cli.py

+33
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ def test_dev() -> None:
2929
"workers": None,
3030
"root_path": "",
3131
"proxy_headers": True,
32+
"forwarded_allow_ips": None,
3233
}
3334
assert "Using import string single_file_app:app" in result.output
3435
assert (
@@ -71,6 +72,7 @@ def test_dev_args() -> None:
7172
"workers": None,
7273
"root_path": "/api",
7374
"proxy_headers": False,
75+
"forwarded_allow_ips": None,
7476
}
7577
assert "Using import string single_file_app:api" in result.output
7678
assert (
@@ -97,6 +99,36 @@ def test_run() -> None:
9799
"workers": None,
98100
"root_path": "",
99101
"proxy_headers": True,
102+
"forwarded_allow_ips": None,
103+
}
104+
assert "Using import string single_file_app:app" in result.output
105+
assert (
106+
"╭─────────── FastAPI CLI - Production mode ───────────╮" in result.output
107+
)
108+
assert "│ Serving at: http://0.0.0.0:8000" in result.output
109+
assert "│ API docs: http://0.0.0.0:8000/docs" in result.output
110+
assert "│ Running in production mode, for development use:" in result.output
111+
assert "│ fastapi dev" in result.output
112+
113+
114+
def test_run_trust_proxy() -> None:
115+
with changing_dir(assets_path):
116+
with patch.object(uvicorn, "run") as mock_run:
117+
result = runner.invoke(
118+
app, ["run", "single_file_app.py", "--forwarded-allow-ips", "*"]
119+
)
120+
assert result.exit_code == 0, result.output
121+
assert mock_run.called
122+
assert mock_run.call_args
123+
assert mock_run.call_args.kwargs == {
124+
"app": "single_file_app:app",
125+
"host": "0.0.0.0",
126+
"port": 8000,
127+
"reload": False,
128+
"workers": None,
129+
"root_path": "",
130+
"proxy_headers": True,
131+
"forwarded_allow_ips": "*",
100132
}
101133
assert "Using import string single_file_app:app" in result.output
102134
assert (
@@ -141,6 +173,7 @@ def test_run_args() -> None:
141173
"workers": 2,
142174
"root_path": "/api",
143175
"proxy_headers": False,
176+
"forwarded_allow_ips": None,
144177
}
145178
assert "Using import string single_file_app:api" in result.output
146179
assert (

0 commit comments

Comments
 (0)