All files / src/where operator-allowlist.ts

100% Statements 14/14
100% Branches 6/6
100% Functions 2/2
100% Lines 14/14

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120    144x                                             144x 30x 3x   27x       144x     144x 137x 5x   132x       144x                                                                               144x               144x           144x                                          
import { Op } from '../expressions/op.ts';
 
const ALLOWED_CAST_TYPES: ReadonlySet<string> = new Set([
  'integer',
  'int',
  'smallint',
  'bigint',
  'float',
  'real',
  'double precision',
  'decimal',
  'numeric',
  'boolean',
  'text',
  'char',
  'varchar',
  'date',
  'timestamp',
  'timestamptz',
  'json',
  'jsonb',
  'uuid',
]);
 
/** Checks a `attribute::type` cast against the allowlist. */
export const validCastType = (type: string): string => {
  if (!ALLOWED_CAST_TYPES.has(type.trim().toLowerCase())) {
    throw new Error(`Invalid cast type: ${type}`);
  }
  return type;
};
 
const SQL_COMPARATOR =
  /^(?:[=<>!~*@&|?#+\-/%^]{1,3}|(?:NOT\s+)?(?:I?LIKE|IN|SIMILAR\s+TO)|IS(?:\s+NOT)?(?:\s+DISTINCT\s+FROM)?)$/i;
 
/** Checks the string comparator of `where(attr, comparator, value)` against the allowlist. */
export const validComparator = (comparator: string): string => {
  if (!SQL_COMPARATOR.test(comparator.trim()) || /--|\/\*|\*\//.test(comparator)) {
    throw new Error(`Invalid comparator: ${comparator}`);
  }
  return comparator.trim();
};
 
/** Operators compiled to a binary SQL operator. */
export const binaryOps: ReadonlyMap<symbol, string> = new Map<symbol, string>([
  [Op.gt, '>'],
  [Op.gte, '>='],
  [Op.lt, '<'],
  [Op.lte, '<='],
  [Op.like, 'LIKE'],
  [Op.notLike, 'NOT LIKE'],
  [Op.iLike, 'ILIKE'],
  [Op.notILike, 'NOT ILIKE'],
  [Op.regexp, '~'],
  [Op.notRegexp, '!~'],
  [Op.iRegexp, '~*'],
  [Op.notIRegexp, '!~*'],
  [Op.overlap, '&&'],
  [Op.contains, '@>'],
  [Op.contained, '<@'],
  [Op.adjacent, '-|-'],
  [Op.strictLeft, '<<'],
  [Op.strictRight, '>>'],
  [Op.noExtendRight, '&<'],
  [Op.noExtendLeft, '&>'],
  [Op.match, '@@'],
  [Op.isDistinctFrom, 'IS DISTINCT FROM'],
  [Op.isNotDistinctFrom, 'IS NOT DISTINCT FROM'],
  [Op.hasKey, '?'],
  [Op.hasAnyKeys, '?|'],
  [Op.hasAllKeys, '?&'],
  [Op.similarTo, 'SIMILAR TO'],
  [Op.notSimilarTo, 'NOT SIMILAR TO'],
  [Op.jsonPathExists, '@?'],
  [Op.containedOrEqual, '<<='],
  [Op.containsOrEqual, '>>='],
  [Op.trigramSimilar, '%'],
  [Op.trigramWordSimilar, '<%'],
  [Op.trigramStrictWordSimilar, '<<%'],
  [Op.trigramHasSimilarWord, '%>'],
  [Op.trigramHasSimilarStrictWord, '%>>'],
]);
 
/** jsonb key existence and jsonpath operators; their values are never serialized as JSON. */
export const KEY_OPERATORS: ReadonlySet<symbol> = new Set([
  Op.jsonPathExists,
  Op.hasKey,
  Op.hasAnyKeys,
  Op.hasAllKeys,
]);
 
/** Containment operators (jsonb when applied to a JSON path). */
export const CONTAINMENT_OPERATORS: ReadonlySet<symbol> = new Set([Op.contains, Op.contained]);
 
/** Families of the operators that are not plain binary SQL operators. */
export type OperatorFamily = 'equality' | 'membership' | 'pattern' | 'logical' | 'quantifier';
 
/** Operator family of every non-binary operator. */
export const operatorFamilies: ReadonlyMap<symbol, OperatorFamily> = new Map<
  symbol,
  OperatorFamily
>([
  [Op.eq, 'equality'],
  [Op.ne, 'equality'],
  [Op.is, 'equality'],
  [Op.not, 'equality'],
  [Op.col, 'equality'],
  [Op.in, 'membership'],
  [Op.notIn, 'membership'],
  [Op.between, 'membership'],
  [Op.notBetween, 'membership'],
  [Op.startsWith, 'pattern'],
  [Op.endsWith, 'pattern'],
  [Op.substring, 'pattern'],
  [Op.and, 'logical'],
  [Op.or, 'logical'],
  [Op.any, 'quantifier'],
  [Op.all, 'quantifier'],
]);