|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const common = require('../common'); |
| 4 | + |
| 5 | +if (!common.isWindows) { |
| 6 | + // TODO: Similar checks on *nix-like systems (e.g using chmod or the like) |
| 7 | + common.skip('test only runs on Windows'); |
| 8 | +} |
| 9 | + |
| 10 | +const assert = require('assert'); |
| 11 | +const fs = require('fs'); |
| 12 | +const path = require('path'); |
| 13 | +const cp = require('child_process'); |
| 14 | + |
| 15 | +const tmpdir = require('../common/tmpdir'); |
| 16 | +tmpdir.refresh(); |
| 17 | + |
| 18 | +// Create readOnlyMod.js and set to read only |
| 19 | +const readOnlyMod = path.join(tmpdir.path, 'readOnlyMod'); |
| 20 | +const readOnlyModRelative = path.relative(__dirname, readOnlyMod); |
| 21 | +const readOnlyModFullPath = `${readOnlyMod}.js`; |
| 22 | + |
| 23 | +fs.writeFileSync(readOnlyModFullPath, 'module.exports = 42;'); |
| 24 | + |
| 25 | +// Removed any inherited ACEs, and any explicitly granted ACEs for the |
| 26 | +// current user |
| 27 | +cp.execSync( |
| 28 | + `icacls.exe "${readOnlyModFullPath}" /inheritance:r /remove "%USERNAME%"`); |
| 29 | + |
| 30 | +// Grant the current user read & execute only |
| 31 | +cp.execSync(`icacls.exe "${readOnlyModFullPath}" /grant "%USERNAME%":RX`); |
| 32 | + |
| 33 | +let except = null; |
| 34 | +try { |
| 35 | + // Attempt to load the module. Will fail if write access is required |
| 36 | + require(readOnlyModRelative); |
| 37 | +} catch (err) { |
| 38 | + except = err; |
| 39 | +} |
| 40 | + |
| 41 | +// Remove the expliclty granted rights, and reenable inheritance |
| 42 | +cp.execSync( |
| 43 | + `icacls.exe "${readOnlyModFullPath}" /remove "%USERNAME%" /inheritance:e`); |
| 44 | + |
| 45 | +// Delete the test module (note: tmpdir should get cleaned anyway) |
| 46 | +fs.unlinkSync(readOnlyModFullPath); |
| 47 | + |
| 48 | +assert.ifError(except); |
0 commit comments