Skip to content

Commit f914b8e

Browse files
authored
Fix/command shell injection vulnerabilities (#555)
Modify the default `make_load_authorization_clients_handler` to just log the URI at a severity the user will see. Applications that want a browser to open can supply their own handler. This is fully portable and leaves nothing to inject into.
1 parent c3cfea8 commit f914b8e

4 files changed

Lines changed: 33 additions & 24 deletions

File tree

‎Development/nmos-cpp-node/main.cpp‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,8 +137,8 @@ int main(int argc, char* argv[])
137137

138138
// only configure communication with Authorization server if IS-10/BCP-003-02 is required
139139
// Note:
140-
// the validate_authorization callback must be set up before executing the make_node_server where make_node_api, make_connection_api, make_events_api, and make_channelmapping_api are set up
141-
// the ws_validate_authorization callback must be set up before executing the make_node_server where make_events_ws_validate_handler is set up
140+
// the validate_authorization callback must be set up before executing make_node_server, where all the APIs which require IS-10 are configured
141+
// the ws_validate_authorization callback must be set up before executing the make_node_server where all the WS APIs which require IS-10 are configured
142142
// the get_authorization_bearer_token callback must be set up before executing the make_node_server where make_http_client_config is set up
143143
nmos::experimental::authorization_state authorization_state;
144144
if (nmos::experimental::fields::server_authorization(node_model.settings))

‎Development/nmos/authorization_handlers.cpp‎

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,6 @@
1010
#include "nmos/json_schema.h"
1111
#include "nmos/json_fields.h"
1212
#include "nmos/slog.h"
13-
#if defined(_WIN32) && !defined(__cplusplus_winrt)
14-
#include <Windows.h>
15-
#include <shellapi.h>
16-
#endif
1713

1814
namespace nmos
1915
{
@@ -181,29 +177,28 @@ namespace nmos
181177
};
182178
}
183179

184-
// construct callback to start the authorization code flow request on a browser
180+
// construct callback to start the authorization code grant flow request
185181
// this is required for OAuth clients which use Authorization Code Flow to obtain the access token
186-
// note: as it is not easy to specify the 'content-type' used in the browser programmatically, this can be easily
187-
// fixed by installing a browser header modifier
188-
// extensions such as ModHeader can be used to add the missing 'content-type' header:
189-
// for Windows https://chrome.google.com/webstore/detail/modheader-modify-http-hea/idgpnmonknjnojddfkpgkljpfnnfcklj
190-
// for Linux https://addons.mozilla.org/en-GB/firefox/addon/modheader-firefox/
191-
request_authorization_code_handler make_request_authorization_code_handler(slog::base_gate& gate)
182+
// this implementation does not launch a browser (or any other external process) itself - it simply logs
183+
// the uri the user needs to visit to grant authorization, at warning severity so that it's visible by default
184+
// applications that want to automatically open a browser should supply their own handler via
185+
// node_implementation::on_request_authorization_code, e.g. one which launches the platform's browser directly
186+
// (not via a command shell) with the uri as a single argument, to avoid the possibility of command/shell injection
187+
// note: the authorization_code_uri consists of the authorization endpoint, client_id, redirect_uri, scope and state parameters etc
188+
// see https://specs.amwa.tv/is-10/latest/Behaviour%20-%20Token%20Requests/#authorization-request-and-response
189+
// see https://tools.ietf.org/html/rfc6749#section-4.1
190+
// see https://github.com/sony/nmos-cpp/blob/master/Documents/Authorization.md#authorization-code-grant
191+
// note: as it is not easy to specify the 'content-type' used in the browser programmatically; this can be easily fixed
192+
// by installing a browser header modifier extension such as ModHeader, which can be used to add the missing 'content-type' header.
193+
nmos::experimental::request_authorization_code_handler make_request_authorization_code_handler(slog::base_gate& gate)
192194
{
193195
return[&gate](const web::uri& authorization_code_uri)
194196
{
195-
slog::log<slog::severities::more_info>(gate, SLOG_FLF) << "Open a browser to start the authorization code flow: " << authorization_code_uri.to_string();
196-
197-
std::string browser_cmd;
198-
#if defined(_WIN32) && !defined(__cplusplus_winrt)
199-
browser_cmd = "start \"\" \"" + utility::us2s(authorization_code_uri.to_string()) + "\"";
200-
#else
201-
browser_cmd = "xdg-open \"" + utility::us2s(authorization_code_uri.to_string()) + "\"";
202-
#endif
203-
std::ignore = system(browser_cmd.c_str());
197+
slog::log<slog::severities::warning>(gate, SLOG_FLF) << "Open a browser and visit the following URI to start the authorization code flow: " << authorization_code_uri.to_string();
204198

205-
// hmm, notify authorization_code_flow in the authorization_behaviour thread
206-
// in the event of user cancels the authorization code flow process
199+
// The authorization code flow process (obtaining the access token) is expected to be completed within the time specified in the settings;
200+
// otherwise, the authorization code flow process will be considered failed
201+
// See authorization_code_flow_max in settings.h
207202
};
208203
}
209204

‎Development/nmos/authorization_handlers.h‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,18 @@ namespace nmos
118118
// callback on requesting to start off the authorization code grant flow
119119
// callbacks from this function are called with the model locked, and may read or write directly to the model
120120
// this callback should not throw exceptions
121+
// note: this is invoked with the uri the user needs to visit (in a browser of their choosing) to grant
122+
// authorization; the default handler (see make_request_authorization_code_handler) simply logs the uri
123+
// at an appropriate severity, since the library itself has no business launching external processes such
124+
// as a web browser; an application wanting that behaviour should supply its own handler, e.g. one which
125+
// launches a browser without ever invoking a command shell (so as to avoid any possibility of command/
126+
// shell injection via the uri)
127+
// note: the authorization_code_uri consists of the authorization endpoint, client_id, redirect_uri, scope and state parameters etc
128+
// see https://specs.amwa.tv/is-10/latest/Behaviour%20-%20Token%20Requests/#authorization-request-and-response
129+
// see https://tools.ietf.org/html/rfc6749#section-4.1
130+
// see https://github.com/sony/nmos-cpp/blob/master/Documents/Authorization.md#authorization-code-grant
131+
// note: as it is not easy to specify the 'content-type' used in the browser programmatically; this can be easily fixed
132+
// by installing a browser header modifier extension such as ModHeader, which can be used to add the missing 'content-type' header.
121133
typedef std::function<void(const web::uri& authorization_code_uri)> request_authorization_code_handler;
122134

123135
// helper function to load from the authorization clients file

‎Documents/Authorization.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ A number of grant types are defined in _OAuth 2.0_, but the IS-10/BCP-003-02 spe
3838

3939
This is the recommended grant type and should be used if the Client runs within a web browser (for instance a Broadcast Controller). An authorization code is returned by the Authorization Server via the Client's redirect URI. The Client can then exchange this code for a time-limited access token, which can be renewed with the refresh token.
4040

41+
Note: It is not easy to specify the 'content-type' used in the browser programmatically; this can be easily fixed by installing a browser header modifier extension such as ModHeader, which can be used to add the missing 'content-type' header.
42+
4143
For public clients, there is a risk of an attacker hijacking the authorization code. To prevent this _Proof Key for Code Exchange_ (PKCE) is used to further secure the Authorization Code grant.
4244

4345
The PCKE steps are:

0 commit comments

Comments
 (0)