11cb0ef41Sopenharmony_ci// Copyright Joyent, Inc. and other Node contributors. 21cb0ef41Sopenharmony_ci// 31cb0ef41Sopenharmony_ci// Permission is hereby granted, free of charge, to any person obtaining a 41cb0ef41Sopenharmony_ci// copy of this software and associated documentation files (the 51cb0ef41Sopenharmony_ci// "Software"), to deal in the Software without restriction, including 61cb0ef41Sopenharmony_ci// without limitation the rights to use, copy, modify, merge, publish, 71cb0ef41Sopenharmony_ci// distribute, sublicense, and/or sell copies of the Software, and to permit 81cb0ef41Sopenharmony_ci// persons to whom the Software is furnished to do so, subject to the 91cb0ef41Sopenharmony_ci// following conditions: 101cb0ef41Sopenharmony_ci// 111cb0ef41Sopenharmony_ci// The above copyright notice and this permission notice shall be included 121cb0ef41Sopenharmony_ci// in all copies or substantial portions of the Software. 131cb0ef41Sopenharmony_ci// 141cb0ef41Sopenharmony_ci// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 151cb0ef41Sopenharmony_ci// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 161cb0ef41Sopenharmony_ci// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN 171cb0ef41Sopenharmony_ci// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 181cb0ef41Sopenharmony_ci// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 191cb0ef41Sopenharmony_ci// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE 201cb0ef41Sopenharmony_ci// USE OR OTHER DEALINGS IN THE SOFTWARE. 211cb0ef41Sopenharmony_ci 221cb0ef41Sopenharmony_ci'use strict'; 231cb0ef41Sopenharmony_ciconst common = require('../common'); 241cb0ef41Sopenharmony_ci 251cb0ef41Sopenharmony_ciconst assert = require('assert'); 261cb0ef41Sopenharmony_ci 271cb0ef41Sopenharmony_ciif (common.isWindows) { 281cb0ef41Sopenharmony_ci // uid/gid functions are POSIX only. 291cb0ef41Sopenharmony_ci assert.strictEqual(process.getuid, undefined); 301cb0ef41Sopenharmony_ci assert.strictEqual(process.getgid, undefined); 311cb0ef41Sopenharmony_ci assert.strictEqual(process.setuid, undefined); 321cb0ef41Sopenharmony_ci assert.strictEqual(process.setgid, undefined); 331cb0ef41Sopenharmony_ci return; 341cb0ef41Sopenharmony_ci} 351cb0ef41Sopenharmony_ci 361cb0ef41Sopenharmony_ciif (!common.isMainThread) 371cb0ef41Sopenharmony_ci return; 381cb0ef41Sopenharmony_ci 391cb0ef41Sopenharmony_ciassert.throws(() => { 401cb0ef41Sopenharmony_ci process.setuid({}); 411cb0ef41Sopenharmony_ci}, { 421cb0ef41Sopenharmony_ci code: 'ERR_INVALID_ARG_TYPE', 431cb0ef41Sopenharmony_ci message: 'The "id" argument must be one of type ' + 441cb0ef41Sopenharmony_ci 'number or string. Received an instance of Object' 451cb0ef41Sopenharmony_ci}); 461cb0ef41Sopenharmony_ci 471cb0ef41Sopenharmony_ciassert.throws(() => { 481cb0ef41Sopenharmony_ci process.setuid('fhqwhgadshgnsdhjsdbkhsdabkfabkveyb'); 491cb0ef41Sopenharmony_ci}, { 501cb0ef41Sopenharmony_ci code: 'ERR_UNKNOWN_CREDENTIAL', 511cb0ef41Sopenharmony_ci message: 'User identifier does not exist: fhqwhgadshgnsdhjsdbkhsdabkfabkveyb' 521cb0ef41Sopenharmony_ci}); 531cb0ef41Sopenharmony_ci 541cb0ef41Sopenharmony_ci// Passing -0 shouldn't crash the process 551cb0ef41Sopenharmony_ci// Refs: https://github.com/nodejs/node/issues/32750 561cb0ef41Sopenharmony_ci// And neither should values exceeding 2 ** 31 - 1. 571cb0ef41Sopenharmony_cifor (const id of [-0, 2 ** 31, 2 ** 32 - 1]) { 581cb0ef41Sopenharmony_ci for (const fn of [process.setuid, process.setuid, process.setgid, process.setegid]) { 591cb0ef41Sopenharmony_ci try { fn(id); } catch { 601cb0ef41Sopenharmony_ci // Continue regardless of error. 611cb0ef41Sopenharmony_ci } 621cb0ef41Sopenharmony_ci } 631cb0ef41Sopenharmony_ci} 641cb0ef41Sopenharmony_ci 651cb0ef41Sopenharmony_ci// If we're not running as super user... 661cb0ef41Sopenharmony_ciif (process.getuid() !== 0) { 671cb0ef41Sopenharmony_ci // Should not throw. 681cb0ef41Sopenharmony_ci process.getgid(); 691cb0ef41Sopenharmony_ci process.getuid(); 701cb0ef41Sopenharmony_ci 711cb0ef41Sopenharmony_ci assert.throws( 721cb0ef41Sopenharmony_ci () => { process.setgid('nobody'); }, 731cb0ef41Sopenharmony_ci /(?:EPERM, .+|Group identifier does not exist: nobody)$/ 741cb0ef41Sopenharmony_ci ); 751cb0ef41Sopenharmony_ci 761cb0ef41Sopenharmony_ci assert.throws( 771cb0ef41Sopenharmony_ci () => { process.setuid('nobody'); }, 781cb0ef41Sopenharmony_ci /(?:EPERM, .+|User identifier does not exist: nobody)$/ 791cb0ef41Sopenharmony_ci ); 801cb0ef41Sopenharmony_ci return; 811cb0ef41Sopenharmony_ci} 821cb0ef41Sopenharmony_ci 831cb0ef41Sopenharmony_ci// If we are running as super user... 841cb0ef41Sopenharmony_ciconst oldgid = process.getgid(); 851cb0ef41Sopenharmony_citry { 861cb0ef41Sopenharmony_ci process.setgid('nobody'); 871cb0ef41Sopenharmony_ci} catch (err) { 881cb0ef41Sopenharmony_ci if (err.code !== 'ERR_UNKNOWN_CREDENTIAL') { 891cb0ef41Sopenharmony_ci throw err; 901cb0ef41Sopenharmony_ci } 911cb0ef41Sopenharmony_ci process.setgid('nogroup'); 921cb0ef41Sopenharmony_ci} 931cb0ef41Sopenharmony_ci 941cb0ef41Sopenharmony_ciconst newgid = process.getgid(); 951cb0ef41Sopenharmony_ciassert.notStrictEqual(newgid, oldgid); 961cb0ef41Sopenharmony_ci 971cb0ef41Sopenharmony_ciconst olduid = process.getuid(); 981cb0ef41Sopenharmony_ciprocess.setuid('nobody'); 991cb0ef41Sopenharmony_ciconst newuid = process.getuid(); 1001cb0ef41Sopenharmony_ciassert.notStrictEqual(newuid, olduid); 101