HyperFlow / extension /test /security.test.ts
Gaurav711's picture
Deploy 891f179
2b86c60 verified
Raw History Blame Contribute Delete
3.91 kB
// @vitest-environment node
// Guards on the extension's attack surface: permissions, message input and HTML sinks.
import { readFileSync, readdirSync } from 'node:fs';
import { fileURLToPath } from 'node:url';
import { parseMsg } from '../src/messages';
const root = fileURLToPath(new URL('..', import.meta.url));
const manifest = JSON.parse(readFileSync(root + 'manifest.json', 'utf8'));
it('requests exactly the reviewed permissions', () => {
// Changing any of these widens what the extension can read; update only with a security review.
expect(manifest.permissions).toEqual(['storage', 'activeTab']);
expect(manifest.host_permissions).toEqual(['https://www.swiggy.com/*', 'https://blinkit.com/*', 'https://www.zeptonow.com/*']);
expect(manifest.content_security_policy).toEqual({ extension_pages: "script-src 'self' 'wasm-unsafe-eval'; object-src 'none'" });
expect(manifest.optional_permissions).toBeUndefined();
expect(manifest.externally_connectable).toBeUndefined();
expect(manifest.web_accessible_resources).toBeUndefined();
// Firefox's data-collection declaration must stay "none": nothing leaves the browser.
expect(manifest.browser_specific_settings.gecko.data_collection_permissions).toEqual({ required: ['none'] });
});
it('never writes HTML strings into the page or popup', () => {
const sources = readdirSync(root + 'src', { recursive: true, encoding: 'utf8' }).filter((f) => /\.ts$/.test(f));
expect(sources.length).toBeGreaterThan(0);
for (const file of sources) {
const text = readFileSync(root + 'src/' + file, 'utf8');
expect(text, file).not.toMatch(/\.(innerHTML|outerHTML)\s*=|insertAdjacentHTML|document\.write|\beval\(|new Function\(/);
}
});
const line = { store: 'zepto', name: 'Atta', brand: null, sizeValue: 5000, sizeUnit: 'g', quantity: 1, priceMinor: 30000, mrpMinor: 32000 };
const okCart = { status: 'ok', store: 'zepto', lines: [line], fees: [{ label: 'Delivery fee', amountMinor: 2500, kind: 'delivery', preselected: false }],
payableMinor: 32500, capturedAt: '2026-10-23T08:00:00.000Z' };
it('accepts well-formed messages', () => {
expect(parseMsg({ type: 'cart', cart: okCart, locationKey: 'a'.repeat(64) })).not.toBeNull();
expect(parseMsg({ type: 'cart', cart: { status: 'unsupported_layout', store: 'blinkit', reason: 'cart container missing' }, locationKey: 'b'.repeat(64) })).not.toBeNull();
expect(parseMsg({ type: 'analyze' })).toEqual({ type: 'analyze' });
expect(parseMsg({ type: 'confirm', lineIndex: 0, store: 'blinkit' })).toEqual({ type: 'confirm', lineIndex: 0, store: 'blinkit' });
});
it.each([
['unknown type', { type: 'run' }],
['extra key', { type: 'analyze', extra: 1 }],
['extra cart key', { type: 'cart', locationKey: 'a'.repeat(64), cart: { ...okCart, script: 'x' } }],
['unknown store', { type: 'cart', locationKey: 'a'.repeat(64), cart: { ...okCart, store: 'amazon' } }],
['float price', { type: 'cart', locationKey: 'a'.repeat(64), cart: { ...okCart, lines: [{ ...line, priceMinor: 10.5 }] } }],
['negative fee', { type: 'cart', locationKey: 'a'.repeat(64), cart: { ...okCart, fees: [{ ...okCart.fees[0], amountMinor: -1 }] } }],
['too many lines', { type: 'cart', locationKey: 'a'.repeat(64), cart: { ...okCart, lines: Array(101).fill(line) } }],
['oversized name', { type: 'cart', locationKey: 'a'.repeat(64), cart: { ...okCart, lines: [{ ...line, name: 'x'.repeat(301) }] } }],
['bad location key', { type: 'cart', locationKey: 'Main Street, Bengaluru', cart: okCart }],
['bad date', { type: 'cart', locationKey: 'a'.repeat(64), cart: { ...okCart, capturedAt: 'yesterday' } }],
['confirm with negative index', { type: 'confirm', lineIndex: -1, store: 'blinkit' }],
['confirm with unknown store', { type: 'confirm', lineIndex: 0, store: 'amazon' }],
['not an object', 'analyze'],
['null', null],
])('rejects %s', (_, raw) => expect(parseMsg(raw)).toBeNull());