mirror of
https://github.com/yusufipk/OpenFrame.git
synced 2026-09-11 17:46:06 +00:00
The suite that landed in #43/#44 was written against existing behaviour, so a number of tests pinned bugs rather than asserting correct behaviour. This fixes the production code and moves each of those tests onto the fixed behaviour in the same change. Security: - project-download: derive the archive entry extension from the last path segment and restrict it to a short alphanumeric run, so an extensionless allowlisted url can no longer contribute a path separator; validate the r2 branch against the strict proxy-path pattern instead of a `startsWith`, which let `/api/upload/video/clip.mp4/../../etc/passwd` through verbatim. - rate-limit: hash a key or action wider than its column instead of skipping the query. Both the guard and the failing INSERT used to answer "allowed", so the limit stopped applying entirely. Warn at startup when TRUSTED_PROXY_MODE is unset in production. - video uploads: the file name decides the content type; a client-declared video mime no longer makes `payload.exe` acceptable. - email templates: escape in the helpers rather than relying on every caller, with an explicit `rawEmailHtml()` opt-out for the one call site that builds markup. `escapeHtml` now covers the single quote. - CSP: allow loopback object storage outside production only. - route-access: reach the billing redirect only for the workspace owner. Keying it off the owner's billing status alone made the redirect target an oracle for whose subscription had lapsed, and sent members to a page they cannot act on. - search: carry the same billing condition every other read path carries. - logger: check `err.name` as well as `err.constructor.name`, so a re-thrown, deserialised or minified Prisma error is still redacted. - upload tokens: resolve the signing secret outside the try, so a server booted without one fails loudly instead of reporting every grant as a forgery. - invitations: never downgrade an existing membership, and report a scoped invitation that points at nothing as not_found rather than accepted. - auth: resolve the workspace role for every signed-in caller, so checkProjectAccess and computeProjectAccess stop disagreeing about the owner who also owns the workspace. The `intent` option is gone with it. - r2-media-proxy: validate the object key inside the proxy so the guard travels with the function; delete the unused, unanchored `mediaUrlToR2Key`. - r2: sign the content type into presigned PUT grants. Correctness: - frame rate snapping picks the nearest standard, not the first within tolerance, so 24, 30 and 60 fps are reachable at all. - a version upload registers its Bunny cleanup as soon as bunny-init answers, so a failed tus upload no longer leaves a billed video behind. - deleting videos clears storage before the rows, so a refused DELETE leaves a retryable row rather than an orphaned object. - an expired upload session can be cancelled, which is what releases its quota. - `voice/` joins the delete allowlist, so a voice note can be removed by the module that wrote it. - a failed CORS write propagates instead of being mistaken for an empty config and replacing the bucket's rules. - filtering projects by workspace no longer hides projects the unfiltered call returns. - upload retries skip aborts and permanent 4xx; progress no longer divides by zero. - reply edits no longer clear the comment's tag; optimistic resolve rolls back to the state it replaced; the delete snapshot is captured once. - assorted UI fixes: duplicate React keys, double-click guards reading stale closures, the tag list fetched twice per load, a failed member list rendering as an empty one, a stale "Initializing upload..." beside a failure, and a registration banner pointing at an email that never arrives. Consistency and access: - the two download routes answer 404 for an id belonging to another tenant, as the comment export route already did. A caller who does belong still gets 403. - accessible names for the share-link password field, the guest name gates, the version dialog inputs and the comment-tag controls. Repository health: - the runner image installs production dependencies only. - a setup file for the unit project restores stubbed env centrally. - native tsconfig path resolution replaces vite-tsconfig-paths. - `uploadBytesWithProgress` exists once. - admin stats bill Bunny storage to the workspace owner like every other quota, gate on the configured flag, wire up the single-flight guard and count the statuses that belonged to no bucket. - `r2Client.destroy()` releases the presign client too. - `prepare` tolerates a production install, where husky is absent.
213 lines
7.0 KiB
TypeScript
213 lines
7.0 KiB
TypeScript
import { describe, expect, it } from 'vitest';
|
|
import {
|
|
EMAIL_COLORS,
|
|
brandedEmailTemplate,
|
|
emailButton,
|
|
emailHeading,
|
|
emailHighlight,
|
|
emailRow,
|
|
escapeAttr,
|
|
escapeHtml,
|
|
rawEmailHtml,
|
|
} from '@/lib/email-brand';
|
|
|
|
describe('escapeHtml', () => {
|
|
it.each([
|
|
['&', '&'],
|
|
['<', '<'],
|
|
['>', '>'],
|
|
['"', '"'],
|
|
])('escapes %s as %s', (input, expected) => {
|
|
expect(escapeHtml(input)).toBe(expected);
|
|
});
|
|
|
|
it('neutralises a script tag', () => {
|
|
expect(escapeHtml('<script>alert("xss")</script>')).toBe(
|
|
'<script>alert("xss")</script>'
|
|
);
|
|
});
|
|
|
|
it('escapes the ampersand first so an existing entity is not double-decoded', () => {
|
|
expect(escapeHtml('<')).toBe('&lt;');
|
|
});
|
|
|
|
// Single-quoted attributes exist in the templates, so leaving the quote alone left a
|
|
// value able to close one.
|
|
it('escapes the single quote', () => {
|
|
expect(escapeHtml("it's")).toBe('it's');
|
|
});
|
|
|
|
it('leaves plain text untouched', () => {
|
|
expect(escapeHtml('Alice reviewed your video')).toBe('Alice reviewed your video');
|
|
});
|
|
|
|
it('returns an empty string unchanged', () => {
|
|
expect(escapeHtml('')).toBe('');
|
|
});
|
|
});
|
|
|
|
describe('escapeAttr', () => {
|
|
it('escapes the same four characters as escapeHtml', () => {
|
|
expect(escapeAttr('&<>"')).toBe('&<>"');
|
|
});
|
|
|
|
it('breaks an attribute injection attempt', () => {
|
|
const escaped = escapeAttr('https://x.com" onmouseover="alert(1)');
|
|
|
|
expect(escaped).not.toContain('" onmouseover');
|
|
expect(escaped).toContain('" onmouseover="');
|
|
});
|
|
|
|
it('agrees with escapeHtml on every input despite the different replacement order', () => {
|
|
for (const input of ['&', '<', '>', '"', '<', 'a&b<c>d"e']) {
|
|
expect(escapeAttr(input)).toBe(escapeHtml(input));
|
|
}
|
|
});
|
|
});
|
|
|
|
describe('brandedEmailTemplate', () => {
|
|
it('produces a full HTML document carrying the brand colours', () => {
|
|
const html = brandedEmailTemplate('<td>Body</td>');
|
|
|
|
expect(html.startsWith('<!DOCTYPE html>')).toBe(true);
|
|
expect(html.trimEnd().endsWith('</html>')).toBe(true);
|
|
expect(html).toContain(EMAIL_COLORS.bg);
|
|
expect(html).toContain('OpenFrame');
|
|
});
|
|
|
|
it('inserts the body markup verbatim', () => {
|
|
expect(brandedEmailTemplate('<td>Hello & welcome</td>')).toContain(
|
|
'<td>Hello & welcome</td>'
|
|
);
|
|
});
|
|
|
|
it('omits the footer block when no footer options are given', () => {
|
|
expect(brandedEmailTemplate('<td>Body</td>')).not.toContain(
|
|
'padding:20px 0 0;text-align:center'
|
|
);
|
|
});
|
|
|
|
it('renders footer text on its own', () => {
|
|
const html = brandedEmailTemplate('<td>Body</td>', { footerText: 'Sent by OpenFrame' });
|
|
|
|
expect(html).toContain('Sent by OpenFrame');
|
|
expect(html).not.toContain('<a href=');
|
|
});
|
|
|
|
it('renders the footer link only when both the text and the url are present', () => {
|
|
const withTextOnly = brandedEmailTemplate('<td>Body</td>', { footerLinkText: 'Unsubscribe' });
|
|
const withBoth = brandedEmailTemplate('<td>Body</td>', {
|
|
footerLinkText: 'Unsubscribe',
|
|
footerLinkUrl: 'https://open-frame.net/settings',
|
|
});
|
|
|
|
expect(withTextOnly).not.toContain('Unsubscribe');
|
|
expect(withBoth).toContain('href="https://open-frame.net/settings"');
|
|
expect(withBoth).toContain('>Unsubscribe<');
|
|
});
|
|
|
|
it('escapes the footer link url as an attribute', () => {
|
|
const html = brandedEmailTemplate('<td>Body</td>', {
|
|
footerLinkText: 'Unsubscribe',
|
|
footerLinkUrl: 'https://x.com" onmouseover="alert(1)',
|
|
});
|
|
|
|
expect(html).not.toContain('" onmouseover="alert(1)"');
|
|
expect(html).toContain('" onmouseover="alert(1)');
|
|
});
|
|
|
|
it('escapes the footer link text as HTML', () => {
|
|
const html = brandedEmailTemplate('<td>Body</td>', {
|
|
footerLinkText: '<script>alert(1)</script>',
|
|
footerLinkUrl: 'https://open-frame.net',
|
|
});
|
|
|
|
expect(html).not.toContain('<script>alert(1)</script>');
|
|
expect(html).toContain('<script>alert(1)</script>');
|
|
});
|
|
});
|
|
|
|
describe('email fragment builders', () => {
|
|
it('emailHeading renders the icon and title in the accent colour', () => {
|
|
const html = emailHeading('🎬', 'New comment');
|
|
|
|
expect(html).toContain('🎬');
|
|
expect(html).toContain('New comment');
|
|
expect(html).toContain(EMAIL_COLORS.accent);
|
|
});
|
|
|
|
it('emailRow renders the label and value in a table row', () => {
|
|
const html = emailRow('Project', 'Launch video');
|
|
|
|
expect(html.startsWith('<tr>')).toBe(true);
|
|
expect(html).toContain('Project');
|
|
expect(html).toContain('Launch video');
|
|
});
|
|
|
|
it('emailRow switches to the highlight style when asked', () => {
|
|
const plain = emailRow('Project', 'Launch video');
|
|
const highlighted = emailRow('Project', 'Launch video', true);
|
|
|
|
expect(plain).toContain(EMAIL_COLORS.textSecondary);
|
|
expect(highlighted).toContain('font-weight:600');
|
|
expect(highlighted).not.toContain(EMAIL_COLORS.textSecondary);
|
|
});
|
|
|
|
it('emailButton escapes both the href and the label', () => {
|
|
const html = emailButton('<b>Open</b>', 'https://x.com" onclick="alert(1)');
|
|
|
|
expect(html).toContain('" onclick="alert(1)');
|
|
expect(html).toContain('<b>Open</b>');
|
|
expect(html).not.toContain('<b>Open</b>');
|
|
});
|
|
|
|
// The escaping lives in the helpers rather than in every call site, so a project name
|
|
// or a display name is safe whether or not the next caller remembers to escape it.
|
|
it.each([
|
|
['emailHeading title', () => emailHeading('*', '<script>alert(1)</script>')],
|
|
['emailRow label', () => emailRow('<script>alert(1)</script>', 'value')],
|
|
['emailRow value', () => emailRow('label', '<script>alert(1)</script>')],
|
|
['emailHighlight text', () => emailHighlight('<script>alert(1)</script>')],
|
|
['emailButton label', () => emailButton('<script>alert(1)</script>', 'https://x.test')],
|
|
])('%s is escaped', (_label, build) => {
|
|
const html = build();
|
|
|
|
expect(html).not.toContain('<script>');
|
|
expect(html).toContain('<script>alert(1)</script>');
|
|
});
|
|
|
|
it('rawEmailHtml opts a value out of escaping', () => {
|
|
const html = emailRow('From', rawEmailHtml('<span>Alice</span>'));
|
|
|
|
expect(html).toContain('<span>Alice</span>');
|
|
});
|
|
|
|
it('escapes the footer text', () => {
|
|
const html = brandedEmailTemplate('<tr><td>body</td></tr>', {
|
|
footerText: '<script>alert(1)</script>',
|
|
});
|
|
|
|
expect(html).not.toContain('<script>');
|
|
});
|
|
|
|
it('inserts the body markup verbatim', () => {
|
|
const html = brandedEmailTemplate('<tr><td>body</td></tr>');
|
|
|
|
expect(html).toContain('<tr><td>body</td></tr>');
|
|
});
|
|
|
|
it('emailHighlight wraps the text in a bordered block', () => {
|
|
const html = emailHighlight('Your trial ends in 2 days');
|
|
|
|
expect(html.startsWith('<div')).toBe(true);
|
|
expect(html).toContain('Your trial ends in 2 days');
|
|
expect(html).toContain(EMAIL_COLORS.cardInner);
|
|
});
|
|
|
|
it('every brand colour is a six digit hex value', () => {
|
|
for (const value of Object.values(EMAIL_COLORS)) {
|
|
expect(value).toMatch(/^#[0-9a-f]{6}$/i);
|
|
}
|
|
});
|
|
});
|