From 5358ebb0c3deec87aa6f8db4002a7c8f399d274b Mon Sep 17 00:00:00 2001 From: "feliks.pobiedzinski@swmansion.com" Date: Wed, 30 Oct 2024 11:22:45 +0100 Subject: [PATCH] Validate signalling --- lib/membrane_webrtc/simple_websocket_server.ex | 5 ++++- lib/membrane_webrtc/sink.ex | 2 ++ lib/membrane_webrtc/source.ex | 2 ++ lib/membrane_webrtc/utils.ex | 18 ++++++++++++++++++ 4 files changed, 26 insertions(+), 1 deletion(-) create mode 100644 lib/membrane_webrtc/utils.ex diff --git a/lib/membrane_webrtc/simple_websocket_server.ex b/lib/membrane_webrtc/simple_websocket_server.ex index 93bf093..544cf93 100644 --- a/lib/membrane_webrtc/simple_websocket_server.ex +++ b/lib/membrane_webrtc/simple_websocket_server.ex @@ -26,7 +26,7 @@ defmodule Membrane.WebRTC.SimpleWebSocketServer do @doc false @spec child_spec({options, SignalingChannel.t()}) :: Supervisor.child_spec() def child_spec({opts, signaling}) do - opts = opts |> Keyword.validate!([:port, ip: {127, 0, 0, 1}]) |> Map.new() + opts = opts |> validate_options!() |> Map.new() Supervisor.child_spec( {Bandit, @@ -37,6 +37,9 @@ defmodule Membrane.WebRTC.SimpleWebSocketServer do ) end + @spec validate_options!(options()) :: options() | no_return() + def validate_options!(options), do: Keyword.validate!(options, [:port, ip: {127, 0, 0, 1}]) + @doc false @spec start_link_supervised(pid, options) :: SignalingChannel.t() def start_link_supervised(utility_supervisor, opts) do diff --git a/lib/membrane_webrtc/sink.ex b/lib/membrane_webrtc/sink.ex index 83e902d..002e180 100644 --- a/lib/membrane_webrtc/sink.ex +++ b/lib/membrane_webrtc/sink.ex @@ -102,6 +102,8 @@ defmodule Membrane.WebRTC.Sink do @impl true def handle_init(_ctx, opts) do + :ok = Membrane.WebRTC.Utils.validate_signaling!(opts.signaling) + spec = child(:webrtc, %ExWebRTCSink{ signaling: opts.signaling, diff --git a/lib/membrane_webrtc/source.ex b/lib/membrane_webrtc/source.ex index f9951c8..354ba7c 100644 --- a/lib/membrane_webrtc/source.ex +++ b/lib/membrane_webrtc/source.ex @@ -72,6 +72,8 @@ defmodule Membrane.WebRTC.Source do def handle_init(_ctx, opts) do {signaling, opts} = opts |> Map.from_struct() |> Map.pop!(:signaling) + :ok = Membrane.WebRTC.Utils.validate_signaling!(signaling) + spec = child(:webrtc, %ExWebRTCSource{ signaling: signaling, diff --git a/lib/membrane_webrtc/utils.ex b/lib/membrane_webrtc/utils.ex new file mode 100644 index 0000000..e211471 --- /dev/null +++ b/lib/membrane_webrtc/utils.ex @@ -0,0 +1,18 @@ +defmodule Membrane.WebRTC.Utils do + @moduledoc false + + alias Membrane.WebRTC.{SignalingChannel, SimpleWebSocketServer} + + @spec validate_signaling!(SignalingChannel.t() | {:websocket, SimpleWebSocketServer.options()}) :: + :ok | no_return() + def validate_signaling!(%SignalingChannel{}), do: :ok + + def validate_signaling!({:websocket, options}) do + _options = SimpleWebSocketServer.validate_options!(options) + :ok + end + + def validate_signaling!(signaling) do + raise "Invalid signaling: #{inspect(signaling, pretty: true)}" + end +end