|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright 2026 Google LLC |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | +import {describe, expect, it} from 'vitest'; |
| 7 | +import { |
| 8 | + assertInsideRoot, |
| 9 | + getSessionArtifactsDir, |
| 10 | + getUserRoot, |
| 11 | +} from '../src/artifacts/file_artifact_service.js'; |
| 12 | + |
| 13 | +const ROOT = '/tmp/adk-test-root'; |
| 14 | + |
| 15 | +describe('assertSafeSegment - valid inputs', () => { |
| 16 | + it('allows a plain alphanumeric userId', () => { |
| 17 | + expect(() => getUserRoot(ROOT, 'alice')).not.toThrow(); |
| 18 | + }); |
| 19 | + it('allows a UUID as userId', () => { |
| 20 | + expect(() => |
| 21 | + getUserRoot(ROOT, '550e8400-e29b-41d4-a716-446655440000'), |
| 22 | + ).not.toThrow(); |
| 23 | + }); |
| 24 | + it('allows an email-style userId', () => { |
| 25 | + expect(() => getUserRoot(ROOT, 'user.name@org')).not.toThrow(); |
| 26 | + }); |
| 27 | + it('allows a plain alphanumeric sessionId', () => { |
| 28 | + expect(() => |
| 29 | + getSessionArtifactsDir(`${ROOT}/users/alice`, 'session-abc123'), |
| 30 | + ).not.toThrow(); |
| 31 | + }); |
| 32 | +}); |
| 33 | + |
| 34 | +describe('assertSafeSegment - userId attacks', () => { |
| 35 | + it('blocks dot-dot-slash traversal in userId', () => { |
| 36 | + expect(() => getUserRoot(ROOT, '../../etc')).toThrow('Invalid userId'); |
| 37 | + }); |
| 38 | + it('blocks forward slash in userId', () => { |
| 39 | + expect(() => getUserRoot(ROOT, 'a/b')).toThrow('Invalid userId'); |
| 40 | + }); |
| 41 | + it('blocks percent-encoded slash in userId', () => { |
| 42 | + expect(() => getUserRoot(ROOT, '..%2F..%2Fetc')).toThrow('Invalid userId'); |
| 43 | + }); |
| 44 | + it('blocks null byte in userId', () => { |
| 45 | + expect(() => getUserRoot(ROOT, 'alice\x00')).toThrow('Invalid userId'); |
| 46 | + }); |
| 47 | + it('blocks empty string as userId', () => { |
| 48 | + expect(() => getUserRoot(ROOT, '')).toThrow('Invalid userId'); |
| 49 | + }); |
| 50 | +}); |
| 51 | + |
| 52 | +describe('assertSafeSegment - sessionId attacks', () => { |
| 53 | + const base = `${ROOT}/users/alice`; |
| 54 | + it('blocks dot-dot-slash traversal in sessionId', () => { |
| 55 | + expect(() => getSessionArtifactsDir(base, '../../../secret')).toThrow( |
| 56 | + 'Invalid sessionId', |
| 57 | + ); |
| 58 | + }); |
| 59 | + it('blocks forward slash in sessionId', () => { |
| 60 | + expect(() => getSessionArtifactsDir(base, 'sess/../../etc')).toThrow( |
| 61 | + 'Invalid sessionId', |
| 62 | + ); |
| 63 | + }); |
| 64 | + it('blocks percent-encoded slash in sessionId', () => { |
| 65 | + expect(() => getSessionArtifactsDir(base, '..%2F..%2F..%2Fsecret')).toThrow( |
| 66 | + 'Invalid sessionId', |
| 67 | + ); |
| 68 | + }); |
| 69 | + it('blocks empty string as sessionId', () => { |
| 70 | + expect(() => getSessionArtifactsDir(base, '')).toThrow('Invalid sessionId'); |
| 71 | + }); |
| 72 | +}); |
| 73 | + |
| 74 | +describe('assertInsideRoot - defence-in-depth', () => { |
| 75 | + it('throws when resolved path escapes root', () => { |
| 76 | + expect(() => |
| 77 | + assertInsideRoot('/tmp/root/../outside', '/tmp/root', 'test'), |
| 78 | + ).toThrow('escapes storage root'); |
| 79 | + }); |
| 80 | + it('allows a path equal to root', () => { |
| 81 | + expect(() => |
| 82 | + assertInsideRoot('/tmp/root', '/tmp/root', 'test'), |
| 83 | + ).not.toThrow(); |
| 84 | + }); |
| 85 | + it('allows a path nested inside root', () => { |
| 86 | + expect(() => |
| 87 | + assertInsideRoot('/tmp/root/users/alice', '/tmp/root', 'test'), |
| 88 | + ).not.toThrow(); |
| 89 | + }); |
| 90 | +}); |
0 commit comments