|
1 | 1 | import * as core from '@actions/core'; |
| 2 | +import { fs, vol } from 'memfs'; |
2 | 3 | import { beforeEach, describe, expect, it, vi } from 'vitest'; |
3 | 4 | import * as helpers from '../src/helpers'; |
4 | 5 |
|
| 6 | +vi.mock('node:fs'); |
| 7 | +vi.mock('@actions/core'); |
| 8 | + |
5 | 9 | describe('Configure AWS Credentials helpers', {}, () => { |
6 | 10 | beforeEach(() => { |
7 | 11 | vi.restoreAllMocks(); |
8 | | - vi.spyOn(core, 'debug').mockImplementation(() => {}); |
| 12 | + vi.clearAllMocks(); |
| 13 | + vol.reset(); |
9 | 14 | }); |
10 | 15 | it('removes brackets from GitHub Actor', {}, () => { |
11 | 16 | const actor = 'actor[bot]'; |
@@ -97,4 +102,97 @@ describe('Configure AWS Credentials helpers', {}, () => { |
97 | 102 | helpers.exportCredentials({ AccessKeyId: 'test', SecretAccessKey: 'test' }, false, true); |
98 | 103 | expect(core.exportVariable).toHaveBeenCalledWith('AWS_SESSION_TOKEN', ''); |
99 | 104 | }); |
| 105 | + |
| 106 | + describe('filesystem helpers', {}, () => { |
| 107 | + describe('isSymlink', {}, () => { |
| 108 | + it('returns true for a symlink', {}, () => { |
| 109 | + fs.mkdirSync('/dir', { recursive: true }); |
| 110 | + fs.writeFileSync('/dir/target', 'data'); |
| 111 | + fs.symlinkSync('/dir/target', '/dir/link'); |
| 112 | + expect(helpers.isSymlink('/dir/link')).toBe(true); |
| 113 | + }); |
| 114 | + |
| 115 | + it('returns false for a regular file', {}, () => { |
| 116 | + fs.mkdirSync('/dir', { recursive: true }); |
| 117 | + fs.writeFileSync('/dir/file', 'data'); |
| 118 | + expect(helpers.isSymlink('/dir/file')).toBe(false); |
| 119 | + }); |
| 120 | + |
| 121 | + it('returns false for a missing path', {}, () => { |
| 122 | + expect(helpers.isSymlink('/nonexistent')).toBe(false); |
| 123 | + }); |
| 124 | + }); |
| 125 | + |
| 126 | + describe('readFileUtf8', {}, () => { |
| 127 | + it('returns content for a regular file', {}, () => { |
| 128 | + fs.mkdirSync('/dir', { recursive: true }); |
| 129 | + fs.writeFileSync('/dir/file', 'hello'); |
| 130 | + expect(helpers.readFileUtf8('/dir/file')).toBe('hello'); |
| 131 | + }); |
| 132 | + |
| 133 | + it('returns null when the file does not exist', {}, () => { |
| 134 | + fs.mkdirSync('/dir', { recursive: true }); |
| 135 | + expect(helpers.readFileUtf8('/dir/missing')).toBe(null); |
| 136 | + }); |
| 137 | + |
| 138 | + it('refuses to read through a symlink at the target', {}, () => { |
| 139 | + fs.mkdirSync('/dir', { recursive: true }); |
| 140 | + fs.writeFileSync('/dir/secret', 'sensitive'); |
| 141 | + fs.symlinkSync('/dir/secret', '/dir/link'); |
| 142 | + expect(() => helpers.readFileUtf8('/dir/link')).toThrow(/Refusing .* \(.* symbolic link\)/); |
| 143 | + }); |
| 144 | + |
| 145 | + it('refuses to read when the parent directory is a symlink', {}, () => { |
| 146 | + fs.mkdirSync('/real/.aws', { recursive: true }); |
| 147 | + fs.writeFileSync('/real/.aws/credentials', 'data'); |
| 148 | + fs.mkdirSync('/home', { recursive: true }); |
| 149 | + fs.symlinkSync('/real/.aws', '/home/.aws'); |
| 150 | + expect(() => helpers.readFileUtf8('/home/.aws/credentials')).toThrow(/Refusing .* \(.* symbolic link\)/); |
| 151 | + }); |
| 152 | + |
| 153 | + it('refuses to read when the path is a directory', {}, () => { |
| 154 | + fs.mkdirSync('/dir/subdir', { recursive: true }); |
| 155 | + expect(() => helpers.readFileUtf8('/dir/subdir')).toThrow(/not a regular file/); |
| 156 | + }); |
| 157 | + }); |
| 158 | + |
| 159 | + describe('writeFileUtf8', {}, () => { |
| 160 | + it('writes content with the specified mode', {}, () => { |
| 161 | + fs.mkdirSync('/dir', { recursive: true }); |
| 162 | + helpers.writeFileUtf8('/dir/file', 'payload', 0o600); |
| 163 | + expect(fs.readFileSync('/dir/file', 'utf-8')).toBe('payload'); |
| 164 | + expect(fs.statSync('/dir/file').mode & 0o777).toBe(0o600); |
| 165 | + }); |
| 166 | + |
| 167 | + it('refuses to follow a symlink at the target and leaves the target file untouched', {}, () => { |
| 168 | + fs.mkdirSync('/dir', { recursive: true }); |
| 169 | + fs.writeFileSync('/dir/target', 'original'); |
| 170 | + fs.symlinkSync('/dir/target', '/dir/link'); |
| 171 | + expect(() => helpers.writeFileUtf8('/dir/link', 'attacker', 0o600)).toThrow(/Refusing .* \(.* symbolic link\)/); |
| 172 | + expect(fs.readFileSync('/dir/target', 'utf-8')).toBe('original'); |
| 173 | + }); |
| 174 | + |
| 175 | + it.skipIf(process.platform === 'win32')('tightens mode on existing files', () => { |
| 176 | + fs.mkdirSync('/dir', { recursive: true }); |
| 177 | + fs.writeFileSync('/dir/file', 'old', { mode: 0o644 }); |
| 178 | + helpers.writeFileUtf8('/dir/file', 'new', 0o600); |
| 179 | + expect(fs.statSync('/dir/file').mode & 0o777).toBe(0o600); |
| 180 | + }); |
| 181 | + }); |
| 182 | + |
| 183 | + describe('mkdir', {}, () => { |
| 184 | + it('is idempotent on a regular directory', {}, () => { |
| 185 | + helpers.mkdir('/some/nested/dir', 0o700); |
| 186 | + helpers.mkdir('/some/nested/dir', 0o700); |
| 187 | + expect(fs.statSync('/some/nested/dir').isDirectory()).toBe(true); |
| 188 | + }); |
| 189 | + |
| 190 | + it('refuses when the target directory is a symlink', {}, () => { |
| 191 | + fs.mkdirSync('/real', { recursive: true }); |
| 192 | + fs.mkdirSync('/home', { recursive: true }); |
| 193 | + fs.symlinkSync('/real', '/home/.aws'); |
| 194 | + expect(() => helpers.mkdir('/home/.aws', 0o700)).toThrow(/Refusing .* \(.* symbolic link\)/); |
| 195 | + }); |
| 196 | + }); |
| 197 | + }); |
100 | 198 | }); |
0 commit comments