Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

1.5 KB
Raw Blame
import { describe, it, expect } from 'vitest';
import { isSafeHref } from './isSafeHref.js';

describe('isSafeHref', () => {
  it('allows http:// URLs', () => {
    expect(isSafeHref('http://x.com')).toBe(true);
  });

  it('allows https:// URLs', () => {
    expect(isSafeHref('https://x.com')).toBe(true);
  });

  it('rejects javascript: URLs', () => {
    expect(isSafeHref('javascript:alert(1)')).toBe(false);
  });

  it('rejects data: URLs', () => {
    expect(isSafeHref('data:text/html,x')).toBe(false);
  });

  it('rejects vbscript: URLs', () => {
    expect(isSafeHref('vbscript:x')).toBe(false);
  });

  it('rejects an empty string', () => {
    expect(isSafeHref('')).toBe(false);
  });

  it('rejects undefined', () => {
    expect(isSafeHref(undefined)).toBe(false);
  });

  it('rejects non-string input', () => {
    expect(isSafeHref(42)).toBe(false);
    expect(isSafeHref(null)).toBe(false);
    expect(isSafeHref({})).toBe(false);
  });

  it('rejects a garbage non-URL string', () => {
    expect(isSafeHref('not a url at all')).toBe(false);
  });
  // The scheme-only spellings a URL parser still reads as http(s) (#6303). These
  // used to be the drift between this rule and a hand-maintained client copy —
  // the client now imports this module, so they are pinned once, here.
  it.each([
    ['https:foo', true],
    ['https:/host', true],
    ['https:///host', true],
    ['//host', false],
  ])('reads %p as %p', (input, expected) => {
    expect(isSafeHref(input)).toBe(expected);
  });
});