|
1 | | -import { generateDockerCompose, subnetsOverlap, writeConfigs, startContainers, stopContainers, cleanup, runAgentCommand } from './docker-manager'; |
| 1 | +import { generateDockerCompose, subnetsOverlap, writeConfigs, startContainers, stopContainers, cleanup, runAgentCommand, validateIdNotInSystemRange, getSafeHostUid, getSafeHostGid, MIN_REGULAR_UID } from './docker-manager'; |
2 | 2 | import { WrapperConfig } from './types'; |
3 | 3 | import * as fs from 'fs'; |
4 | 4 | import * as path from 'path'; |
@@ -47,6 +47,127 @@ describe('docker-manager', () => { |
47 | 47 | }); |
48 | 48 | }); |
49 | 49 |
|
| 50 | + describe('validateIdNotInSystemRange', () => { |
| 51 | + it('should return 1000 for system UIDs (0-999)', () => { |
| 52 | + expect(validateIdNotInSystemRange(0)).toBe('1000'); |
| 53 | + expect(validateIdNotInSystemRange(1)).toBe('1000'); |
| 54 | + expect(validateIdNotInSystemRange(13)).toBe('1000'); // proxy user |
| 55 | + expect(validateIdNotInSystemRange(999)).toBe('1000'); |
| 56 | + }); |
| 57 | + |
| 58 | + it('should return the UID as-is for regular users (>= 1000)', () => { |
| 59 | + expect(validateIdNotInSystemRange(1000)).toBe('1000'); |
| 60 | + expect(validateIdNotInSystemRange(1001)).toBe('1001'); |
| 61 | + expect(validateIdNotInSystemRange(65534)).toBe('65534'); // nobody user on some systems |
| 62 | + }); |
| 63 | + }); |
| 64 | + |
| 65 | + describe('getSafeHostUid', () => { |
| 66 | + const originalGetuid = process.getuid; |
| 67 | + const originalSudoUid = process.env.SUDO_UID; |
| 68 | + |
| 69 | + afterEach(() => { |
| 70 | + process.getuid = originalGetuid; |
| 71 | + if (originalSudoUid !== undefined) { |
| 72 | + process.env.SUDO_UID = originalSudoUid; |
| 73 | + } else { |
| 74 | + delete process.env.SUDO_UID; |
| 75 | + } |
| 76 | + }); |
| 77 | + |
| 78 | + it('should return 1000 when SUDO_UID is a system UID', () => { |
| 79 | + process.getuid = () => 0; // Running as root |
| 80 | + process.env.SUDO_UID = '13'; // proxy user |
| 81 | + expect(getSafeHostUid()).toBe('1000'); |
| 82 | + }); |
| 83 | + |
| 84 | + it('should return SUDO_UID when it is a regular user UID', () => { |
| 85 | + process.getuid = () => 0; // Running as root |
| 86 | + process.env.SUDO_UID = '1001'; |
| 87 | + expect(getSafeHostUid()).toBe('1001'); |
| 88 | + }); |
| 89 | + |
| 90 | + it('should return 1000 when SUDO_UID is 0', () => { |
| 91 | + process.getuid = () => 0; // Running as root |
| 92 | + process.env.SUDO_UID = '0'; |
| 93 | + expect(getSafeHostUid()).toBe('1000'); |
| 94 | + }); |
| 95 | + |
| 96 | + it('should return 1000 when running as root without SUDO_UID', () => { |
| 97 | + process.getuid = () => 0; |
| 98 | + delete process.env.SUDO_UID; |
| 99 | + expect(getSafeHostUid()).toBe('1000'); |
| 100 | + }); |
| 101 | + |
| 102 | + it('should return 1000 for non-root system UID', () => { |
| 103 | + process.getuid = () => 13; // proxy user |
| 104 | + delete process.env.SUDO_UID; |
| 105 | + expect(getSafeHostUid()).toBe('1000'); |
| 106 | + }); |
| 107 | + |
| 108 | + it('should return the UID when running as regular user', () => { |
| 109 | + process.getuid = () => 1001; |
| 110 | + delete process.env.SUDO_UID; |
| 111 | + expect(getSafeHostUid()).toBe('1001'); |
| 112 | + }); |
| 113 | + }); |
| 114 | + |
| 115 | + describe('getSafeHostGid', () => { |
| 116 | + const originalGetgid = process.getgid; |
| 117 | + const originalSudoGid = process.env.SUDO_GID; |
| 118 | + |
| 119 | + afterEach(() => { |
| 120 | + process.getgid = originalGetgid; |
| 121 | + if (originalSudoGid !== undefined) { |
| 122 | + process.env.SUDO_GID = originalSudoGid; |
| 123 | + } else { |
| 124 | + delete process.env.SUDO_GID; |
| 125 | + } |
| 126 | + }); |
| 127 | + |
| 128 | + it('should return 1000 when SUDO_GID is a system GID', () => { |
| 129 | + process.getgid = () => 0; // Running as root |
| 130 | + process.env.SUDO_GID = '13'; // proxy group |
| 131 | + expect(getSafeHostGid()).toBe('1000'); |
| 132 | + }); |
| 133 | + |
| 134 | + it('should return SUDO_GID when it is a regular user GID', () => { |
| 135 | + process.getgid = () => 0; // Running as root |
| 136 | + process.env.SUDO_GID = '1001'; |
| 137 | + expect(getSafeHostGid()).toBe('1001'); |
| 138 | + }); |
| 139 | + |
| 140 | + it('should return 1000 when SUDO_GID is 0', () => { |
| 141 | + process.getgid = () => 0; // Running as root |
| 142 | + process.env.SUDO_GID = '0'; |
| 143 | + expect(getSafeHostGid()).toBe('1000'); |
| 144 | + }); |
| 145 | + |
| 146 | + it('should return 1000 when running as root without SUDO_GID', () => { |
| 147 | + process.getgid = () => 0; |
| 148 | + delete process.env.SUDO_GID; |
| 149 | + expect(getSafeHostGid()).toBe('1000'); |
| 150 | + }); |
| 151 | + |
| 152 | + it('should return 1000 for non-root system GID', () => { |
| 153 | + process.getgid = () => 13; // proxy group |
| 154 | + delete process.env.SUDO_GID; |
| 155 | + expect(getSafeHostGid()).toBe('1000'); |
| 156 | + }); |
| 157 | + |
| 158 | + it('should return the GID when running as regular user', () => { |
| 159 | + process.getgid = () => 1001; |
| 160 | + delete process.env.SUDO_GID; |
| 161 | + expect(getSafeHostGid()).toBe('1001'); |
| 162 | + }); |
| 163 | + }); |
| 164 | + |
| 165 | + describe('MIN_REGULAR_UID constant', () => { |
| 166 | + it('should be 1000 (standard Linux regular user UID threshold)', () => { |
| 167 | + expect(MIN_REGULAR_UID).toBe(1000); |
| 168 | + }); |
| 169 | + }); |
| 170 | + |
50 | 171 | describe('generateDockerCompose', () => { |
51 | 172 | const mockConfig: WrapperConfig = { |
52 | 173 | allowedDomains: ['github.com', 'npmjs.org'], |
|
0 commit comments