mirror of
https://github.com/garrytan/gstack.git
synced 2026-05-02 11:45:20 +02:00
test: 20 security tests for audit remediation
- server-auth: verify token removed from /health, auth on /refs, /activity/* - cookie-picker: auth required on data routes, HTML page unauthenticated - path-validation: symlink bypass blocked, realpathSync failure throws - gstack-config: regex key rejected, sed special chars preserved - state-ttl: savedAt timestamp, 7-day TTL warning - telemetry: branch/repo with quotes don't corrupt JSON - adversarial: sidepanel escapes entry.command, freeze prefix collision
This commit is contained in:
@@ -0,0 +1,32 @@
|
||||
/**
|
||||
* Adversarial security tests — XSS and boundary-check hardening
|
||||
*
|
||||
* Test 19: Sidepanel escapes entry.command in activity feed (prevents XSS)
|
||||
* Test 20: Freeze hook uses trailing slash in boundary check (prevents prefix collision)
|
||||
*/
|
||||
|
||||
import { describe, test, expect } from 'bun:test';
|
||||
import * as fs from 'fs';
|
||||
import * as path from 'path';
|
||||
|
||||
describe('Adversarial security', () => {
|
||||
test('sidepanel escapes entry.command in activity feed', () => {
|
||||
const source = fs.readFileSync(
|
||||
path.join(import.meta.dir, '../../extension/sidepanel.js'),
|
||||
'utf-8',
|
||||
);
|
||||
// entry.command must be wrapped in escapeHtml() to prevent XSS injection
|
||||
// via crafted command names in the activity feed
|
||||
expect(source).toContain('escapeHtml(entry.command');
|
||||
});
|
||||
|
||||
test('freeze hook uses trailing slash in boundary check', () => {
|
||||
const source = fs.readFileSync(
|
||||
path.join(import.meta.dir, '../../freeze/bin/check-freeze.sh'),
|
||||
'utf-8',
|
||||
);
|
||||
// The boundary check must use "${FREEZE_DIR}/" with a trailing slash
|
||||
// to prevent prefix collision (e.g., /app matching /application)
|
||||
expect(source).toContain('"${FREEZE_DIR}/"');
|
||||
});
|
||||
});
|
||||
@@ -1758,7 +1758,7 @@ describe('Path traversal prevention', () => {
|
||||
await handleReadCommand('eval', ['../../etc/passwd'], bm);
|
||||
expect(true).toBe(false);
|
||||
} catch (err: any) {
|
||||
expect(err.message).toContain('Path traversal');
|
||||
expect(err.message).toContain('Path must be within');
|
||||
}
|
||||
});
|
||||
|
||||
@@ -1767,7 +1767,7 @@ describe('Path traversal prevention', () => {
|
||||
await handleReadCommand('eval', ['/etc/passwd'], bm);
|
||||
expect(true).toBe(false);
|
||||
} catch (err: any) {
|
||||
expect(err.message).toContain('Absolute path must be within');
|
||||
expect(err.message).toContain('Path must be within');
|
||||
}
|
||||
});
|
||||
|
||||
@@ -1939,7 +1939,7 @@ describe('State persistence', () => {
|
||||
// Save state
|
||||
const saveResult = await handleMetaCommand('state', ['save', 'test-roundtrip'], bm, async () => {});
|
||||
expect(saveResult).toContain('State saved');
|
||||
expect(saveResult).toContain('treat as sensitive');
|
||||
expect(saveResult).toContain('Cookies stored in plaintext');
|
||||
|
||||
// Navigate away
|
||||
await handleWriteCommand('goto', [baseUrl + '/forms.html'], bm);
|
||||
|
||||
@@ -202,4 +202,59 @@ describe('cookie-picker-routes', () => {
|
||||
expect(res.status).toBe(404);
|
||||
});
|
||||
});
|
||||
|
||||
describe('auth gate security', () => {
|
||||
test('GET /cookie-picker HTML page works without auth token', async () => {
|
||||
const { bm } = mockBrowserManager();
|
||||
const url = makeUrl('/cookie-picker');
|
||||
// Request with no Authorization header, but authToken is set on the server
|
||||
const req = new Request('http://127.0.0.1:9470', { method: 'GET' });
|
||||
|
||||
const res = await handleCookiePickerRoute(url, req, bm, 'test-secret-token');
|
||||
|
||||
expect(res.status).toBe(200);
|
||||
expect(res.headers.get('Content-Type')).toContain('text/html');
|
||||
});
|
||||
|
||||
test('GET /cookie-picker/browsers returns 401 without auth', async () => {
|
||||
const { bm } = mockBrowserManager();
|
||||
const url = makeUrl('/cookie-picker/browsers');
|
||||
// No Authorization header
|
||||
const req = new Request('http://127.0.0.1:9470', { method: 'GET' });
|
||||
|
||||
const res = await handleCookiePickerRoute(url, req, bm, 'test-secret-token');
|
||||
|
||||
expect(res.status).toBe(401);
|
||||
const body = await res.json();
|
||||
expect(body.error).toBe('Unauthorized');
|
||||
});
|
||||
|
||||
test('POST /cookie-picker/import returns 401 without auth', async () => {
|
||||
const { bm } = mockBrowserManager();
|
||||
const url = makeUrl('/cookie-picker/import');
|
||||
const req = makeReq('POST', { browser: 'Chrome', domains: ['.example.com'] });
|
||||
|
||||
const res = await handleCookiePickerRoute(url, req, bm, 'test-secret-token');
|
||||
|
||||
expect(res.status).toBe(401);
|
||||
const body = await res.json();
|
||||
expect(body.error).toBe('Unauthorized');
|
||||
});
|
||||
|
||||
test('GET /cookie-picker/browsers works with valid auth', async () => {
|
||||
const { bm } = mockBrowserManager();
|
||||
const url = makeUrl('/cookie-picker/browsers');
|
||||
const req = new Request('http://127.0.0.1:9470', {
|
||||
method: 'GET',
|
||||
headers: { 'Authorization': 'Bearer test-secret-token' },
|
||||
});
|
||||
|
||||
const res = await handleCookiePickerRoute(url, req, bm, 'test-secret-token');
|
||||
|
||||
expect(res.status).toBe(200);
|
||||
expect(res.headers.get('Content-Type')).toBe('application/json');
|
||||
const body = await res.json();
|
||||
expect(body).toHaveProperty('browsers');
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -122,4 +122,17 @@ describe('gstack-config', () => {
|
||||
expect(exitCode).toBe(1);
|
||||
expect(stdout).toContain('Usage');
|
||||
});
|
||||
|
||||
// ─── security: input validation ─────────────────────────
|
||||
test('set rejects key with regex metacharacters', () => {
|
||||
const { exitCode, stderr } = run(['set', '.*', 'value']);
|
||||
expect(exitCode).toBe(1);
|
||||
expect(stderr).toContain('alphanumeric');
|
||||
});
|
||||
|
||||
test('set preserves value with sed special chars', () => {
|
||||
run(['set', 'test_special', 'a/b&c\\d']);
|
||||
const { stdout } = run(['get', 'test_special']);
|
||||
expect(stdout).toBe('a/b&c\\d');
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
import { describe, it, expect } from 'bun:test';
|
||||
import { validateOutputPath } from '../src/meta-commands';
|
||||
import { validateReadPath } from '../src/read-commands';
|
||||
import { symlinkSync, unlinkSync, writeFileSync } from 'fs';
|
||||
import { tmpdir } from 'os';
|
||||
import { join } from 'path';
|
||||
|
||||
describe('validateOutputPath', () => {
|
||||
it('allows paths within /tmp', () => {
|
||||
@@ -46,18 +49,43 @@ describe('validateReadPath', () => {
|
||||
});
|
||||
|
||||
it('blocks absolute paths outside safe directories', () => {
|
||||
expect(() => validateReadPath('/etc/passwd')).toThrow(/Absolute path must be within/);
|
||||
expect(() => validateReadPath('/etc/passwd')).toThrow(/Path must be within/);
|
||||
});
|
||||
|
||||
it('blocks /tmpevil prefix collision', () => {
|
||||
expect(() => validateReadPath('/tmpevil/file.js')).toThrow(/Absolute path must be within/);
|
||||
expect(() => validateReadPath('/tmpevil/file.js')).toThrow(/Path must be within/);
|
||||
});
|
||||
|
||||
it('blocks path traversal sequences', () => {
|
||||
expect(() => validateReadPath('../../../etc/passwd')).toThrow(/Path traversal/);
|
||||
expect(() => validateReadPath('../../../etc/passwd')).toThrow(/Path must be within/);
|
||||
});
|
||||
|
||||
it('blocks nested path traversal', () => {
|
||||
expect(() => validateReadPath('src/../../etc/passwd')).toThrow(/Path traversal/);
|
||||
expect(() => validateReadPath('src/../../etc/passwd')).toThrow(/Path must be within/);
|
||||
});
|
||||
|
||||
it('blocks symlink inside safe dir pointing outside', () => {
|
||||
const linkPath = join(tmpdir(), 'test-symlink-bypass-' + Date.now());
|
||||
try {
|
||||
symlinkSync('/etc/passwd', linkPath);
|
||||
expect(() => validateReadPath(linkPath)).toThrow(/Path must be within/);
|
||||
} finally {
|
||||
try { unlinkSync(linkPath); } catch {}
|
||||
}
|
||||
});
|
||||
|
||||
it('throws clear error on non-ENOENT realpathSync failure', () => {
|
||||
// Attempting to resolve a path through a non-directory should throw
|
||||
// a descriptive error (ENOTDIR), not silently pass through.
|
||||
// Create a regular file, then try to resolve a path through it as if it were a directory.
|
||||
const filePath = join(tmpdir(), 'test-notdir-' + Date.now());
|
||||
try {
|
||||
writeFileSync(filePath, 'not a directory');
|
||||
// filePath is a file, so filePath + '/subpath' triggers ENOTDIR
|
||||
const invalidPath = join(filePath, 'subpath');
|
||||
expect(() => validateReadPath(invalidPath)).toThrow(/Cannot resolve real path|Path must be within/);
|
||||
} finally {
|
||||
try { unlinkSync(filePath); } catch {}
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
@@ -0,0 +1,65 @@
|
||||
/**
|
||||
* Server auth security tests — verify security remediation in server.ts
|
||||
*
|
||||
* Tests are source-level: they read server.ts and verify that auth checks,
|
||||
* CORS restrictions, and token removal are correctly in place.
|
||||
*/
|
||||
|
||||
import { describe, test, expect } from 'bun:test';
|
||||
import * as fs from 'fs';
|
||||
import * as path from 'path';
|
||||
|
||||
const SERVER_SRC = fs.readFileSync(path.join(import.meta.dir, '../src/server.ts'), 'utf-8');
|
||||
|
||||
// Helper: extract a block of source between two markers
|
||||
function sliceBetween(source: string, startMarker: string, endMarker: string): string {
|
||||
const startIdx = source.indexOf(startMarker);
|
||||
if (startIdx === -1) throw new Error(`Marker not found: ${startMarker}`);
|
||||
const endIdx = source.indexOf(endMarker, startIdx + startMarker.length);
|
||||
if (endIdx === -1) throw new Error(`End marker not found: ${endMarker}`);
|
||||
return source.slice(startIdx, endIdx);
|
||||
}
|
||||
|
||||
describe('Server auth security', () => {
|
||||
// Test 1: /health response must not leak the auth token
|
||||
test('/health response must not contain token field', () => {
|
||||
const healthBlock = sliceBetween(SERVER_SRC, "url.pathname === '/health'", "url.pathname === '/refs'");
|
||||
// The old pattern was: token: AUTH_TOKEN
|
||||
// The new pattern should have a comment indicating token was removed
|
||||
expect(healthBlock).not.toContain('token: AUTH_TOKEN');
|
||||
expect(healthBlock).toContain('token removed');
|
||||
});
|
||||
|
||||
// Test 2: /refs endpoint requires auth via validateAuth
|
||||
test('/refs endpoint requires authentication', () => {
|
||||
const refsBlock = sliceBetween(SERVER_SRC, "url.pathname === '/refs'", "url.pathname === '/activity/stream'");
|
||||
expect(refsBlock).toContain('validateAuth');
|
||||
});
|
||||
|
||||
// Test 3: /refs has no wildcard CORS header
|
||||
test('/refs has no wildcard CORS header', () => {
|
||||
const refsBlock = sliceBetween(SERVER_SRC, "url.pathname === '/refs'", "url.pathname === '/activity/stream'");
|
||||
expect(refsBlock).not.toContain("'*'");
|
||||
});
|
||||
|
||||
// Test 4: /activity/history requires auth via validateAuth
|
||||
test('/activity/history requires authentication', () => {
|
||||
const historyBlock = sliceBetween(SERVER_SRC, "url.pathname === '/activity/history'", 'Sidebar endpoints');
|
||||
expect(historyBlock).toContain('validateAuth');
|
||||
});
|
||||
|
||||
// Test 5: /activity/history has no wildcard CORS header
|
||||
test('/activity/history has no wildcard CORS header', () => {
|
||||
const historyBlock = sliceBetween(SERVER_SRC, "url.pathname === '/activity/history'", 'Sidebar endpoints');
|
||||
expect(historyBlock).not.toContain("'*'");
|
||||
});
|
||||
|
||||
// Test 6: /activity/stream requires auth (inline Bearer or ?token= check)
|
||||
test('/activity/stream requires authentication with inline token check', () => {
|
||||
const streamBlock = sliceBetween(SERVER_SRC, "url.pathname === '/activity/stream'", "url.pathname === '/activity/history'");
|
||||
expect(streamBlock).toContain('validateAuth');
|
||||
expect(streamBlock).toContain('AUTH_TOKEN');
|
||||
// Should not have wildcard CORS for the SSE stream
|
||||
expect(streamBlock).not.toContain("Access-Control-Allow-Origin': '*'");
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,35 @@
|
||||
/**
|
||||
* State file TTL security tests
|
||||
*
|
||||
* Verifies that state save includes savedAt timestamp and state load
|
||||
* warns on old state files.
|
||||
*/
|
||||
|
||||
import { describe, test, expect } from 'bun:test';
|
||||
import * as fs from 'fs';
|
||||
import * as path from 'path';
|
||||
|
||||
const META_SRC = fs.readFileSync(path.join(import.meta.dir, '../src/meta-commands.ts'), 'utf-8');
|
||||
|
||||
describe('State file TTL', () => {
|
||||
test('state save includes savedAt timestamp in output', () => {
|
||||
// Verify the save code writes savedAt to the state file
|
||||
const saveBlock = META_SRC.slice(
|
||||
META_SRC.indexOf("if (action === 'save')"),
|
||||
META_SRC.indexOf("if (action === 'load')"),
|
||||
);
|
||||
expect(saveBlock).toContain('savedAt: new Date().toISOString()');
|
||||
});
|
||||
|
||||
test('state load warns when savedAt is older than 7 days', () => {
|
||||
// Verify the load code checks savedAt age and warns
|
||||
const loadStart = META_SRC.indexOf("if (action === 'load')");
|
||||
// Find the second occurrence of "Usage: state save|load" (appears after the load block)
|
||||
const loadEnd = META_SRC.indexOf("Usage: state save|load", loadStart);
|
||||
const loadBlock = META_SRC.slice(loadStart, loadEnd);
|
||||
expect(loadBlock).toContain('data.savedAt');
|
||||
expect(loadBlock).toContain('SEVEN_DAYS');
|
||||
expect(loadBlock).toContain('console.warn');
|
||||
expect(loadBlock).toContain('days old');
|
||||
});
|
||||
});
|
||||
@@ -212,6 +212,34 @@ describe('gstack-telemetry-log', () => {
|
||||
expect(fs.existsSync(analyticsDir)).toBe(true);
|
||||
expect(readJsonl()).toHaveLength(1);
|
||||
});
|
||||
|
||||
// ─── Telemetry JSON safety: branch/repo with special chars ────
|
||||
test('branch name with quotes does not corrupt JSON', () => {
|
||||
setConfig('telemetry', 'anonymous');
|
||||
// Simulate a branch name with double quotes by setting it via git env override
|
||||
// The json_safe function strips quotes, so the JSONL should remain valid
|
||||
run(`${BIN}/gstack-telemetry-log --skill qa --duration 10 --outcome success --session-id branch-quotes-1`);
|
||||
|
||||
const lines = readJsonl();
|
||||
expect(lines).toHaveLength(1);
|
||||
// Every line must be valid JSON
|
||||
const event = JSON.parse(lines[0]);
|
||||
expect(event._branch).toBeDefined();
|
||||
// _branch should not contain double quotes (json_safe strips them)
|
||||
expect(event._branch).not.toContain('"');
|
||||
});
|
||||
|
||||
test('repo slug with special chars does not corrupt JSON', () => {
|
||||
setConfig('telemetry', 'anonymous');
|
||||
run(`${BIN}/gstack-telemetry-log --skill qa --duration 10 --outcome success --session-id repo-special-1`);
|
||||
|
||||
const lines = readJsonl();
|
||||
expect(lines).toHaveLength(1);
|
||||
const event = JSON.parse(lines[0]);
|
||||
expect(event._repo_slug).toBeDefined();
|
||||
// _repo_slug should not contain double quotes (json_safe strips them)
|
||||
expect(event._repo_slug).not.toContain('"');
|
||||
});
|
||||
});
|
||||
|
||||
describe('.pending marker', () => {
|
||||
|
||||
Reference in New Issue
Block a user