Repository: electron/electron (language: cpp)

Issue:
Support Chromium's command-line switch --unsafely-treat-insecure-origin-as-secure
**Is your feature request related to a problem? Please describe.**
Notifications, Geolocation, Device motion/orientation, EME, getUserMedia, AppCache require a secure origin to work in Chromium. HTTP URLs are not considered secure.
That's why Chromium provides the command-line switch
`--unsafely-treat-insecure-origin-as-secure="$url"` to circumvent this problem.
Unfortunately it's not working in Electron.

**Describe the solution you'd like**
Please make it work. In the renderer. I'd suggest simply like 
```
new BrowserWindow({
  webPreferences: {
    additionalArguments: {
    '--unsafely-treat-insecure-origin-as-secure', 'http://insecure-origin'
    }
}
```
Or a new option `considerSecure: String[]` to `BrowserWindow.webPreferences`.
I guess it should be not too hard to implement since Chromium already supports it and it seems to get lost only in the process of Electron integration.

**Describe alternatives you've considered**
- `app.commandLine.appendSwitch('unsafely-treat-insecure-origin-as-secure', $url)` in the main thread
- Invisible [BrowserWindow](https://github.com/electron/electron/blob/master/docs/api/browser-window.md) with secure origin + IPC
- `<iframe>` on a secure origin
- localhost-Proxy
- Using Data URLs in [loadURL](https://github.com/electron/electron/blob/master/docs/api/browser-window.md#winloadurlurl-options): `data:text/html,<script>new Notification("hi")</script>`
- webFrame.registerURLSchemeAsPrivileged

**Additional context**
'Deprecating Powerful Features on Insecure Origins' https://goo.gl/rStTGz
When the flag is set in Chromium a message appears under the address bar, saying that
'You are using an unsupported command-line flag'.
![image](https://user-images.githubusercontent.com/23075755/47640947-1c6ebf80-db65-11e8-9e6e-d7a5bd90c208.png)
Nonetheless the features are then working on the origin treated securely.


Discussion / hints:
Since the associated PR has been closed, I'd like to argue here once again for this feature:
With access to the mighty API via nodeIntegration
Chromium's classification of 'Insecure Origins' doesn't hold for Electron.

I can access any file I want and request across the whole internet,
but have to evoke intricatest trickery to display a humble notification.

@MarshallOfSound Since you already tried to implement this, would you try again?
In the comments of #15452 there are some hints how to do it.

For testing I found Chomium supports IP 0.0.0.0 as ubiquitous unsafe origin!
So you can `w.loadURL('http://0.0.0.0')` and then spawn a note
via `webContents.executeJavaScript('new Notification("test")')`
and resolve the test via its onshow handler.
Service workers are another use case for this; they require a secure context.

`localhost` and `127.0.0.1` are exceptions to the HTTPS rule and are considered secure, which serves many developers' needs.

Often however in corporate environments, due to authentication setups in development environments, it's only possible to (easily) access local applications under development using the machine's full host name. This is when `--unsafely-treat-insecure-origin-as-secure` is useful.

Note also regarding the flag that it has no effect in Chromium unless `--user-data-dir` is also supplied.
I would like this feature too, getUserMedia from http://{some where other than localhost} ,you know...
I would like this feature too, getUserMedia from http://{some where other than localhost} ,you know...
I would like this feature too, getUserMedia from http://{some where other than localhost} ,you know...
I would like this feature too, getUserMedia from http://{some where other than localhost} ,you know...
too
sit back and wait
When will this feature be supported?
daily ask:When will this feature be supported?
> daily ask:When will this feature be supported?
yan can use `protocol.registerSchemesAsPrivileged` to  resolve this 
> > daily ask:When will this feature be supported?
> > yan can use `protocol.registerSchemesAsPrivileged` to  resolve this

ok， thx, i will try
> > > daily ask:When will this feature be supported?
> > > yan can use `protocol.registerSchemesAsPrivileged` to  resolve this
> 
> ok， thx, i will try
used before the ready event of the app
```js
 protocol.registerSchemesAsPrivileged([
    {
      scheme: 'http', 
      privileges: {
        bypassCSP: true,
        secure: true,
        supportFetchAPI: true,
        corsEnabled: true
      }
    }
  ]);
```
idea from https://github.com/electron/electron/issues/10381#issuecomment-406206397
```js
// used before the ready event of the app
 protocol.registerSchemesAsPrivileged([
    {
      scheme: 'http', 
      privileges: {
        bypassCSP: true,
        secure: true,
        supportFetchAPI: true,
        corsEnabled: true
      }
    }
  ]);
```
> > > > daily ask:When will this feature be supported?
> > > > yan can use `protocol.registerSchemesAsPrivileged` to  resolve this
> > 
> > 
> > ok， thx, i will try
> > used before the ready event of the app
> 
> ```js
>  protocol.registerSchemesAsPrivileged([
>     {
>       scheme: 'http', 
>       privileges: {
>         bypassCSP: true,
>         secure: true,
>         supportFetchAPI: true,
>         corsEnabled: true
>       }
>     }
>   ]);
> ```

谢了兄弟！

Fix patch:
diff --git a/shell/browser/electron_browser_client.cc b/shell/browser/electron_browser_client.cc
index 096dcabb56c4f..04e31a63b754d 100644
--- a/shell/browser/electron_browser_client.cc
+++ b/shell/browser/electron_browser_client.cc
@@ -21,6 +21,7 @@
 #include "base/no_destructor.h"
 #include "base/path_service.h"
 #include "base/stl_util.h"
+#include "base/strings/strcat.h"
 #include "base/strings/string_number_conversions.h"
 #include "base/strings/string_util.h"
 #include "base/strings/utf_string_conversions.h"
@@ -63,6 +64,8 @@
 #include "printing/buildflags/buildflags.h"
 #include "services/device/public/cpp/geolocation/location_provider.h"
 #include "services/network/public/cpp/features.h"
+#include "services/network/public/cpp/is_potentially_trustworthy.h"
+#include "services/network/public/cpp/network_switches.h"
 #include "services/network/public/cpp/resource_request_body.h"
 #include "services/network/public/cpp/self_deleting_url_loader_factory.h"
 #include "shell/app/electron_crash_reporter_client.h"
@@ -361,6 +364,21 @@ int GetCrashSignalFD(const base::CommandLine& command_line) {
 }
 #endif  // BUILDFLAG(IS_LINUX)
 
+void MaybeAppendSecureOriginsAllowlistSwitch(base::CommandLine* cmdline) {
+  // |allowlist| combines pref/policy + cmdline switch in the browser process.
+  // For renderer and utility (e.g. NetworkService) processes the switch is the
+  // only available source, so below the combined (pref/policy + cmdline)
+  // allowlist of secure origins is injected into |cmdline| for these other
+  // processes.
+  std::vector<std::string> allowlist =
+      network::SecureOriginAllowlist::GetInstance().GetCurrentAllowlist();
+  if (!allowlist.empty()) {
+    cmdline->AppendSwitchASCII(
+        network::switches::kUnsafelyTreatInsecureOriginAsSecure,
+        base::JoinString(allowlist, ","));
+  }
+}
+
 }  // namespace
 
 // static
@@ -602,6 +620,10 @@ void ElectronBrowserClient::AppendExtraCommandLineSwitches(
     command_line->CopySwitchesFrom(*base::CommandLine::ForCurrentProcess(),
                                    kCommonSwitchNames,
                                    base::size(kCommonSwitchNames));
+    if (process_type == ::switches::kUtilityProcess ||
+        content::RenderProcessHost::FromID(process_id)) {
+      MaybeAppendSecureOriginsAllowlistSwitch(command_line);
+    }
   }
 
   if (process_type == ::switches::kRendererProcess) {


Test patch: