From 1c2a78a896ba662d401ae636e7eeba653e56e0a1 Mon Sep 17 00:00:00 2001 From: John Kleinschmidt Date: Wed, 8 Mar 2017 09:55:59 -0500 Subject: [PATCH] Add WebRTCIPPolicy setting to webContents and webview Resolves #8777 Code to set and get the policy come from the muon framework code: https://github.com/brave/muon/blob/master/atom/browser/api/atom_api_web_ contents.cc#L1324...L1343 --- atom/browser/api/atom_api_web_contents.cc | 21 ++++++++++++++++++++ atom/browser/api/atom_api_web_contents.h | 2 ++ docs/api/web-contents.md | 14 +++++++++++++ docs/api/webview-tag.md | 9 ++++++++- lib/browser/guest-view-manager.js | 3 +++ lib/renderer/web-view/web-view-attributes.js | 8 ++++++++ lib/renderer/web-view/web-view-constants.js | 1 + 7 files changed, 57 insertions(+), 1 deletion(-) diff --git a/atom/browser/api/atom_api_web_contents.cc b/atom/browser/api/atom_api_web_contents.cc index 83fe76e6bb7b..bfe8a7861232 100644 --- a/atom/browser/api/atom_api_web_contents.cc +++ b/atom/browser/api/atom_api_web_contents.cc @@ -1067,6 +1067,23 @@ void WebContents::GoToOffset(int offset) { web_contents()->GetController().GoToOffset(offset); } +const std::string& WebContents::GetWebRTCIPHandlingPolicy() const { + return web_contents()-> + GetMutableRendererPrefs()->webrtc_ip_handling_policy; +} + +void WebContents::SetWebRTCIPHandlingPolicy( + const std::string webrtc_ip_handling_policy) { + if (GetWebRTCIPHandlingPolicy() == webrtc_ip_handling_policy) + return; + web_contents()->GetMutableRendererPrefs()->webrtc_ip_handling_policy = + webrtc_ip_handling_policy; + + content::RenderViewHost* host = web_contents()->GetRenderViewHost(); + if (host) + host->SyncRendererPrefs(); +} + bool WebContents::IsCrashed() const { return web_contents()->IsCrashed(); } @@ -1765,6 +1782,10 @@ void WebContents::BuildPrototype(v8::Isolate* isolate, .SetMethod("copyImageAt", &WebContents::CopyImageAt) .SetMethod("capturePage", &WebContents::CapturePage) .SetMethod("setEmbedder", &WebContents::SetEmbedder) + .SetMethod("setWebRTCIPHandlingPolicy", + &WebContents::SetWebRTCIPHandlingPolicy) + .SetMethod("getWebRTCIPHandlingPolicy", + &WebContents::GetWebRTCIPHandlingPolicy) .SetProperty("id", &WebContents::ID) .SetProperty("session", &WebContents::Session) .SetProperty("hostWebContents", &WebContents::HostWebContents) diff --git a/atom/browser/api/atom_api_web_contents.h b/atom/browser/api/atom_api_web_contents.h index 41edcc264e0c..5aecab046222 100644 --- a/atom/browser/api/atom_api_web_contents.h +++ b/atom/browser/api/atom_api_web_contents.h @@ -92,6 +92,8 @@ class WebContents : public mate::TrackableObject, void GoBack(); void GoForward(); void GoToOffset(int offset); + const std::string& GetWebRTCIPHandlingPolicy() const; + void SetWebRTCIPHandlingPolicy(const std::string webrtc_ip_handling_policy); bool IsCrashed() const; void SetUserAgent(const std::string& user_agent, mate::Arguments* args); std::string GetUserAgent(); diff --git a/docs/api/web-contents.md b/docs/api/web-contents.md index a665fae963a5..6ef3b73d63d3 100644 --- a/docs/api/web-contents.md +++ b/docs/api/web-contents.md @@ -1245,6 +1245,20 @@ Schedules a full repaint of the window this web contents is in. If *offscreen rendering* is enabled invalidates the frame and generates a new one through the `'paint'` event. +#### `contents.getWebRTCIPHandlingPolicy()` + +* Returns `String` - Returns the WebRTC IP Handling Policy + +#### `contents.setWebRTCIPHandlingPolicy(policy)` + +* `policy` String - Specify the WebRTC IP Handling Policy + * `default` - Exposes user's public and local IPs. This is the default behavior if not specified. + * `default_public_interface_only` - Exposes user's public IP, but does not expose user's local IP. + * `default_public_and_private_interfaces` - Exposes user's public and local IPs. + * `disable_non_proxied_udp` - Does not expose public or local IPs. + +Setting the WebRTC IP handling policy allows you to control which IPs are exposed via WebRTC. See [BrowserLeaks](https://browserleaks.com/webrtc) for more details. + ### Instance Properties #### `contents.id` diff --git a/docs/api/webview-tag.md b/docs/api/webview-tag.md index 5efc225a1d65..105dfa1b662a 100644 --- a/docs/api/webview-tag.md +++ b/docs/api/webview-tag.md @@ -289,6 +289,13 @@ win.on('resize', () => { }) ``` +### `webrtcippolicy` + +```html + +``` +This attribute allows you to set the WebRTC IP handling policy which controls what IPs are exposed via WebRTC. See [webContents](web-contents.md#contentssetwebrtciphandlingpolicypolicy) for available policies. + ## Methods The `webview` tag has the following methods: @@ -312,7 +319,7 @@ webview.addEventListener('dom-ready', () => { * `userAgent` String (optional) - A user agent originating the request. * `extraHeaders` String (optional) - Extra headers separated by "\n" * `postData` ([UploadRawData](structures/upload-raw-data.md) | [UploadFile](structures/upload-file.md) | [UploadFileSystem](structures/upload-file-system.md) | [UploadBlob](structures/upload-blob.md))[] - (optional) - * `baseURLForDataURL` String (optional) - Base url (with trailing path separator) for files to be loaded by the data url. This is needed only if the specified `url` is a data url and needs to load other files. + * `baseURLForDataURL` String (optional) - Base url (with trailing path separator) for files to be loaded by the data url. This is needed only if the specified `url` is a data url and needs to load other files. Loads the `url` in the webview, the `url` must contain the protocol prefix, e.g. the `http://` or `file://`. diff --git a/lib/browser/guest-view-manager.js b/lib/browser/guest-view-manager.js index 0bfe77d1e5e4..00b309baa665 100644 --- a/lib/browser/guest-view-manager.js +++ b/lib/browser/guest-view-manager.js @@ -103,6 +103,9 @@ const createGuest = function (embedder, params) { height: params.maxheight } }) + if (params.webrtcippolicy) { + guest.setWebRTCIPHandlingPolicy(params.webrtcippolicy) + } if (params.src) { const opts = {} if (params.httpreferrer) { diff --git a/lib/renderer/web-view/web-view-attributes.js b/lib/renderer/web-view/web-view-attributes.js index 204046bd6054..c8911f5065f1 100644 --- a/lib/renderer/web-view/web-view-attributes.js +++ b/lib/renderer/web-view/web-view-attributes.js @@ -304,6 +304,13 @@ class DisableBlinkFeaturesAttribute extends WebViewAttribute { } } +// Attribute specifies WebRTC IP handling policy for handling IP leaking. +class WebRTCIPHandlingPolicyAttribute extends WebViewAttribute { + constructor (webViewImpl) { + super(webViewConstants.ATTRIBUTE_WEBRTCIPPOLICY, webViewImpl) + } +} + // Attribute that specifies the web preferences to be enabled. class WebPreferencesAttribute extends WebViewAttribute { constructor (webViewImpl) { @@ -329,6 +336,7 @@ WebViewImpl.prototype.setupWebViewAttributes = function () { this.attributes[webViewConstants.ATTRIBUTE_GUESTINSTANCE] = new GuestInstanceAttribute(this) this.attributes[webViewConstants.ATTRIBUTE_DISABLEGUESTRESIZE] = new BooleanAttribute(webViewConstants.ATTRIBUTE_DISABLEGUESTRESIZE, this) this.attributes[webViewConstants.ATTRIBUTE_WEBPREFERENCES] = new WebPreferencesAttribute(this) + this.attributes[webViewConstants.ATTRIBUTE_WEBRTCIPPOLICY] = new WebRTCIPHandlingPolicyAttribute(this) const autosizeAttributes = [webViewConstants.ATTRIBUTE_MAXHEIGHT, webViewConstants.ATTRIBUTE_MAXWIDTH, webViewConstants.ATTRIBUTE_MINHEIGHT, webViewConstants.ATTRIBUTE_MINWIDTH] autosizeAttributes.forEach((attribute) => { diff --git a/lib/renderer/web-view/web-view-constants.js b/lib/renderer/web-view/web-view-constants.js index bf2601822d33..42f813456cae 100644 --- a/lib/renderer/web-view/web-view-constants.js +++ b/lib/renderer/web-view/web-view-constants.js @@ -20,6 +20,7 @@ module.exports = { ATTRIBUTE_GUESTINSTANCE: 'guestinstance', ATTRIBUTE_DISABLEGUESTRESIZE: 'disableguestresize', ATTRIBUTE_WEBPREFERENCES: 'webpreferences', + ATTRIBUTE_WEBRTCIPPOLICY: 'webrtcippolicy', // Internal attribute. ATTRIBUTE_INTERNALINSTANCEID: 'internalinstanceid',