|
| 1 | +'use strict'; |
| 2 | +const common = require('../common'); |
| 3 | +if (process.platform !== 'darwin') |
| 4 | + common.skip('App Sandbox is only avaliable on Darwin'); |
| 5 | + |
| 6 | +const fixtures = require('../common/fixtures'); |
| 7 | +const tmpdir = require('../common/tmpdir'); |
| 8 | +const assert = require('assert'); |
| 9 | +const child_process = require('child_process'); |
| 10 | +const path = require('path'); |
| 11 | +const fs = require('fs'); |
| 12 | +const os = require('os'); |
| 13 | + |
| 14 | +const nodeBinary = process.execPath; |
| 15 | + |
| 16 | +tmpdir.refresh(); |
| 17 | + |
| 18 | +const appBundlePath = path.join(tmpdir.path, 'node_sandboxed.app'); |
| 19 | +const appBundleContentPath = path.join(appBundlePath, 'Contents'); |
| 20 | +const appExecutablePath = path.join( |
| 21 | + appBundleContentPath, 'MacOS', 'node'); |
| 22 | + |
| 23 | +// Construct the app bundle and put the node executable in it: |
| 24 | +// node_sandboxed.app/ |
| 25 | +// └── Contents |
| 26 | +// ├── Info.plist |
| 27 | +// ├── MacOS |
| 28 | +// │ └── node |
| 29 | +fs.mkdirSync(appBundlePath); |
| 30 | +fs.mkdirSync(appBundleContentPath); |
| 31 | +fs.mkdirSync(path.join(appBundleContentPath, 'MacOS')); |
| 32 | +fs.copyFileSync( |
| 33 | + fixtures.path('macos-app-sandbox', 'Info.plist'), |
| 34 | + path.join(appBundleContentPath, 'Info.plist')); |
| 35 | +fs.copyFileSync( |
| 36 | + nodeBinary, |
| 37 | + appExecutablePath); |
| 38 | + |
| 39 | + |
| 40 | +// Sign the app bundle with sandbox entitlements: |
| 41 | +assert.strictEqual( |
| 42 | + child_process.spawnSync('/usr/bin/codesign', [ |
| 43 | + '--entitlements', fixtures.path( |
| 44 | + 'macos-app-sandbox', 'node_sandboxed.entitlements'), |
| 45 | + '-s', '-', |
| 46 | + appBundlePath |
| 47 | + ]).status, |
| 48 | + 0); |
| 49 | + |
| 50 | +// Sandboxed app shouldn't be able to read the home dir |
| 51 | +assert.notStrictEqual( |
| 52 | + child_process.spawnSync(appExecutablePath, [ |
| 53 | + '-e', 'fs.readdirSync(process.argv[1])', os.homedir() |
| 54 | + ]).status, |
| 55 | + 0); |
| 56 | + |
| 57 | +if (process.stdin.isTTY) { |
| 58 | + // Run the sandboxed node instance with inherited tty stdin |
| 59 | + const spawnResult = child_process.spawnSync( |
| 60 | + appExecutablePath, ['-e', ''], |
| 61 | + { stdio: 'inherit' } |
| 62 | + ); |
| 63 | + |
| 64 | + assert.strictEqual(spawnResult.signal, null); |
| 65 | +} |
0 commit comments