Skip to content

Commit 0c08d7a

Browse files
authored
change port bind and add a unittest (#10208)
1 parent eb71ad9 commit 0c08d7a

File tree

4 files changed

+30
-1
lines changed

4 files changed

+30
-1
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
kind: Security
2+
body: Explicitly bind to localhost in docs serve
3+
time: 2024-05-22T09:45:40.748185-04:00
4+
custom:
5+
Author: ChenyuLInx michelleark
6+
Issue: "10209"

core/dbt/task/docs/serve.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ def run(self):
2020
if self.args.browser:
2121
webbrowser.open_new_tab(f"http://localhost:{port}")
2222

23-
with socketserver.TCPServer(("", port), SimpleHTTPRequestHandler) as httpd:
23+
with socketserver.TCPServer(("127.0.0.1", port), SimpleHTTPRequestHandler) as httpd:
2424
click.echo(f"Serving docs at {port}")
2525
click.echo(f"To access from your browser, navigate to: http://localhost:{port}")
2626
click.echo("\n\n")

tests/unit/task/docs/__init__.py

Whitespace-only changes.

tests/unit/task/docs/test_serve.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
from http.server import SimpleHTTPRequestHandler
2+
from unittest.mock import MagicMock, patch
3+
4+
import pytest
5+
6+
from dbt.task.docs.serve import ServeTask
7+
8+
9+
@pytest.fixture
10+
def serve_task():
11+
# Set up
12+
task = ServeTask(config=MagicMock(), args=MagicMock())
13+
task.config.project_target_path = "."
14+
task.args.port = 8000
15+
return task
16+
17+
18+
def test_serve_bind_to_127(serve_task):
19+
serve_task.args.browser = False
20+
with patch("dbt.task.docs.serve.socketserver.TCPServer") as patched_TCPServer:
21+
patched_TCPServer.return_value = MagicMock()
22+
serve_task.run()
23+
patched_TCPServer.assert_called_once_with(("127.0.0.1", 8000), SimpleHTTPRequestHandler)

0 commit comments

Comments
 (0)