Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NAS-132495 / 25.04 / Only allow py_exceptions to be used for unix domain sockets #14

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 15 additions & 7 deletions truenas_api_client/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@
logger = logging.getLogger(__name__)


UNIX_SOCKET_PREFIX = "ws+unix://"
DUMMY_HOSTNAME = "ws://localhost/api/current" # Advised by official docs to use dummy hostname


class Client:
"""Implicit wrapper of either a `JSONRPCClient` or a `LegacyClient`."""

Expand Down Expand Up @@ -133,11 +137,10 @@ def connect(self):
Exception: The `socket` failed to connect.

"""
unix_socket_prefix = "ws+unix://"
if self.url.startswith(unix_socket_prefix):
if self.url.startswith(UNIX_SOCKET_PREFIX):
self.socket = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
self.socket.connect(self.url.removeprefix(unix_socket_prefix))
app_url = "ws://localhost/api/current" # Adviced by official docs to use dummy hostname
self.socket.connect(self.url.removeprefix(UNIX_SOCKET_PREFIX))
app_url = DUMMY_HOSTNAME
elif self.reserved_ports:
self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.socket.settimeout(10)
Expand All @@ -148,7 +151,7 @@ def connect(self):
except Exception:
self.socket.close()
raise
app_url = "ws://localhost/api/current" # Adviced by official docs to use dummy hostname
app_url = DUMMY_HOSTNAME
else:
sockopt = sock_opt(None, None if self.verify_ssl else {"cert_reqs": ssl.CERT_NONE})
sockopt.timeout = 10
Expand Down Expand Up @@ -216,7 +219,7 @@ def _on_open(self, app):

"""
# TCP keepalive settings don't apply to local unix sockets
if 'ws+unix' not in self.url:
if UNIX_SOCKET_PREFIX not in self.url:
# enable keepalives on the socket
self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1)

Expand Down Expand Up @@ -416,11 +419,16 @@ def __init__(self, uri: str | None=None, reserved_ports=False, py_exceptions=Fal

"""
if uri is None:
uri = f'ws+unix://{MIDDLEWARE_RUN_DIR}/middlewared.sock'
uri = f'{UNIX_SOCKET_PREFIX}{MIDDLEWARE_RUN_DIR}/middlewared.sock'

if call_timeout is undefined:
call_timeout = CALL_TIMEOUT

# We pickle_load when handling py_exceptions, reduce risk of MITM on client causing a pickle.load
# of malicious information by only allowing this over unix domain socket.
if py_exceptions and not uri.startswith(UNIX_SOCKET_PREFIX):
raise ClientException('py_exceptions are only allowed for connections to unix domain socket')

self._calls: dict[str, Call] = {}
self._jobs: defaultdict[str, _JobDict] = defaultdict(dict) # type: ignore
self._jobs_lock = Lock()
Expand Down
Loading