app

Local-first trade for farms and co-ops
git clone https://radroots.dev/git/app.git
Log | Files | Refs | README | LICENSE

query-input.test.ts (3616B)


      1 import { describe, expect, it } from 'vitest';
      2 import {
      3   normalizePublicQuery,
      4   readPublicQuery
      5 } from '../../src/lib/catalog/query-input';
      6 import { searchHref, safeContextBack } from '../../src/lib/routes';
      7 
      8 describe('bounded public search copies', () => {
      9   it('normalizes only a search copy using NFKC, lowercase and whitespace', () => {
     10     const raw = '  CARROTS\nVICTORIA 菜  ';
     11     expect(normalizePublicQuery(raw)).toEqual({
     12       ok: true,
     13       text: 'carrots victoria 菜',
     14       terms: ['carrots', 'victoria', '菜']
     15     });
     16     expect(raw).toBe('  CARROTS\nVICTORIA 菜  ');
     17     expect(searchHref(raw)).toBe('/search?q=carrots+victoria+%E8%8F%9C');
     18   });
     19   for (const raw of ['', ' \n  '])
     20     it('omits empty q for bounded browse', () => {
     21       expect(searchHref(raw)).toBe('/search');
     22       expect(normalizePublicQuery(raw)).toEqual({
     23         ok: true,
     24         text: '',
     25         terms: []
     26       });
     27     });
     28   it('admits the byte boundary and rejects excess before search execution', () => {
     29     expect(normalizePublicQuery('a'.repeat(512)).ok).toBe(true);
     30     expect(normalizePublicQuery('菜'.repeat(170)).ok).toBe(true);
     31     for (const raw of [
     32       'a'.repeat(513),
     33       '菜'.repeat(171),
     34       'İ'.repeat(171),
     35       '㍿'.repeat(128),
     36       'x'.repeat(100000)
     37     ]) {
     38       expect(normalizePublicQuery(raw).ok).toBe(false);
     39       expect(searchHref(raw)).toBeUndefined();
     40     }
     41   });
     42   it('admits twelve terms but rejects thirteen', () => {
     43     expect(normalizePublicQuery(Array(12).fill('a').join(' ')).ok).toBe(true);
     44     expect(normalizePublicQuery(Array(13).fill('a').join(' ')).ok).toBe(false);
     45   });
     46   for (const raw of [
     47     undefined,
     48     null,
     49     {},
     50     { body: 'private', peer: 'alice' },
     51     '\ud800',
     52     'carrots\0private'
     53   ])
     54     it(`rejects malformed public input ${JSON.stringify(raw)}`, () => {
     55       expect(normalizePublicQuery(raw).ok).toBe(false);
     56       expect(searchHref(raw)).toBeUndefined();
     57     });
     58   it('encodes text as q without creating extra private parameters or fragments', () => {
     59     const href = searchHref('carrots&peer=alice#body');
     60     const url = new URL(href!, 'https://example.invalid');
     61     expect([...url.searchParams.keys()]).toEqual(['q']);
     62     expect(url.searchParams.get('q')).toBe('carrots&peer=alice#body');
     63     expect(url.hash).toBe('');
     64   });
     65 });
     66 
     67 describe('public URL query context', () => {
     68   it('restores the one bounded allowed q and canonicalizes back links', () => {
     69     const url = new URL('https://example.invalid/search?q=CARROTS%0A%E8%8F%9C');
     70     expect(readPublicQuery(url)).toEqual({
     71       ok: true,
     72       text: 'carrots 菜',
     73       terms: ['carrots', '菜']
     74     });
     75     expect(safeContextBack('/search?q=CARROTS%0A%E8%8F%9C')).toBe(
     76       '/search?q=carrots+%E8%8F%9C'
     77     );
     78   });
     79   for (const context of [
     80     '/search?q=carrots&body=private',
     81     '/search?q=a&q=b',
     82     '/search?q=a#peer',
     83     '//evil/search?q=a',
     84     '/search/../messages?q=a'
     85   ])
     86     it(`rejects arbitrary or private URL context ${context}`, () => {
     87       expect(safeContextBack(context)).toBeUndefined();
     88     });
     89   it('rejects excess URL queries and does not expose them as executable search', () => {
     90     for (const value of ['x'.repeat(513), Array(13).fill('a').join(' ')])
     91       expect(
     92         readPublicQuery(
     93           new URL(
     94             '/search?q=' + encodeURIComponent(value),
     95             'https://example.invalid'
     96           )
     97         ).ok
     98       ).toBe(false);
     99     expect(
    100       readPublicQuery(
    101         new URL('/search?body=private', 'https://example.invalid')
    102       ).ok
    103     ).toBe(false);
    104   });
    105 });