nwjs / nw.js

Call all Node.js modules directly from DOM/WebWorker and enable a new way of writing applications with all Web technologies.
https://nwjs.io
MIT License
40.39k stars 3.88k forks source link

Are 3rd-party modules sandboxed? knex, bookshelf, pg, pg.js all cannot access Postgresql server on same network #6866

Closed bzkdjc closed 5 years ago

bzkdjc commented 6 years ago

NWJS Version : 0.34.3 SDK (whitelisted on firewall, so has full access to entire network) Operating System : Windows 10 x64 1809

Expected behavior

Actual behavior

All these modules stay on infinite attempt to connect until TimeoutException. None of them passes the connecting step.

How to reproduce

On a fresh install of NW.js v0.34.3 SDK on Windows 10 x64 1809:

  1. Install PostgreSQL v10 (to simplify, put trust to pg_hba.conf for all users on all DBs on host 127.0.0.1/32 and give it full access to local net)
  2. Launch nw.exe, but before: show the Dev Tools window in index.html on a script tag inside the body tag, like this:
    <script>
    let win = nw.Window.get();
    win.showDevTools();
    </script>
  3. On the Console tab of the Dev Tools window, type the following either line by line or in one 'paste':
    
    const { Pool, Client } = require('pg')

const client = new Client({ user: 'postgres', host: '127.0.0.1', database: 'postgres', password: '', port: 5432, })

client.connect()

client.query('SELECT NOW()', (err, res) => { console.log('err', err) console.log('res', res) client.end() })


3. The `console.log`s are never reached, as `connect()` never finishes.

### How I know something's wrong: 
I took some minutes to add Electron to the same folder on the same machine. I repeated the same steps above and everything went fine without any problem: the logs are printed (`err null` and `res ResultSet{...}`). 
I removed Electron and, back to NW.js, the same problem were still there.
rogerwang commented 6 years ago

They are not sandboxed. Will see what's wrong soon. Thanks.

rogerwang commented 6 years ago

I just tried your sample here and it works for me well.

bzkdjc commented 6 years ago

I just tried your sample here and it works for me well.

Wow! What kind of problem could I have then?

Any idea? Any solution for me to try?

bzkdjc commented 6 years ago

Just tried on my side => same problem; same result.

Still looking for a solution. Will update as soon as I find something relevant.

rogerwang commented 6 years ago

A wild guess is that the console log is not printed to the console? Just check the devtools, or the devtools console of the background page. In fact, I tried your code snippet by including them in a html file and point package.json to it.

bzkdjc commented 6 years ago

I had to activate debugmode on "knex.js" in order to see Timeout Error. The most bizarre part of this issue is that elsewhere (e.g. electron) it works!

1-line to fetch one record from Postgres using knex ORM:

new Utilisateur({login: 'dan'}).fetch().then(d=>{console.log(d)}).catch(e=>{console.error(e)})

On NW.js (DevTools)

2018-11-14 23_29_54-devtools - chrome-extension___mjbggnfoccjkbhannaafcabegdiapdja_index html

On Electron (Developer Tools)

2018-11-14 23_27_46-developer tools - file____d__local_src_gappi-for-electron_index html

Do you see? There's a timeout while attempting to acquire connection when using NWjs, while the connection is immediately acquired when using Electron where, after some debug output, we get what was console.log'ed at last.

=> I thought it was a 'knex' bug until I tried the same folder of code under Electron.

The same happens when using bookshelf or pg npm modules.

rogerwang commented 6 years ago

Could you upload a full sample app? (Not copy/paste in devtools). In this way we can rule out some factors.

bzkdjc commented 6 years ago

It's a large project started 4 years ago under "node-webkit". Everything has always been fine 'til that issue. I'm going to look for which part to extract for upload.

rogerwang commented 6 years ago

Here is my test which passes here:

package.json:

{
  "name": "nw-demo",
  "main": "index.html"
}

index.html:

<script>
const { Pool, Client } = require('pg')

const client = new Client({
  user: 'postgres',
  host: '127.0.0.1',
  database: 'postgres',
  password: '',
  port: 5432,
})

client.connect()

client.query('SELECT NOW()', (err, res) => {
  console.log('err'+ err)
  console.log('res'+ res)
  client.end()
})
</script>
rogerwang commented 5 years ago

close until more information is available.