All files / core/util validation.ts

69.93% Statements 100/143
54.82% Branches 91/166
81.82% Functions 18/22
72.18% Lines 96/133
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 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544                                12x 12x 12x 12x 12x               12x               12x             12x           12x 1158x                 12x 1769x                     12x 47x   47x     47x             12x 234x                                 12x             950x   616x                           12x           2088x   2088x     2088x                 2088x             2088x                                     2088x 473x 473x 473x 1321x 128x 1193x 965x 965x                         1321x 1321x 1321x     473x                                 12x         11x 27x 27x 27x 27x   27x                                 11x 11x 11x 27x 27x                 27x                           12x             11x   11x   11x           11x 11x 27x 27x 27x                     27x   11x     12x           98x 98x                 98x               12x           618x   610x           610x                   12x           234x 119x 18x                 12x           1722x   1722x 1x                 12x           822x   822x     822x     12x 848x         12x           47x 47x                             12x                           12x           78x 15x           12x                             12x                             12x                                                                                                              
/**
 * Copyright 2017 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
 
import { Path, ValidationPath } from './Path';
import { forEach, contains, safeGet } from '@firebase/util';
import { isInvalidJSONNumber } from './util';
import { errorPrefix as errorPrefixFxn } from '@firebase/util';
import { stringLength } from '@firebase/util';
import { RepoInfo } from '../RepoInfo';
 
/**
 * True for invalid Firebase keys
 * @type {RegExp}
 * @private
 */
export const INVALID_KEY_REGEX_ = /[\[\].#$\/\u0000-\u001F\u007F]/;
 
/**
 * True for invalid Firebase paths.
 * Allows '/' in paths.
 * @type {RegExp}
 * @private
 */
export const INVALID_PATH_REGEX_ = /[\[\].#$\u0000-\u001F\u007F]/;
 
/**
 * Maximum number of characters to allow in leaf value
 * @type {number}
 * @private
 */
export const MAX_LEAF_SIZE_ = 10 * 1024 * 1024;
 
/**
 * @param {*} key
 * @return {boolean}
 */
export const isValidKey = function(key: any): boolean {
  return (
    typeof key === 'string' && key.length !== 0 && !INVALID_KEY_REGEX_.test(key)
  );
};
 
/**
 * @param {string} pathString
 * @return {boolean}
 */
export const isValidPathString = function(pathString: string): boolean {
  return (
    typeof pathString === 'string' &&
    pathString.length !== 0 &&
    !INVALID_PATH_REGEX_.test(pathString)
  );
};
 
/**
 * @param {string} pathString
 * @return {boolean}
 */
export const isValidRootPathString = function(pathString: string): boolean {
  Eif (pathString) {
    // Allow '/.info/' at the beginning.
    pathString = pathString.replace(/^\/*\.info(\/|$)/, '/');
  }
 
  return isValidPathString(pathString);
};
 
/**
 * @param {*} priority
 * @return {boolean}
 */
export const isValidPriority = function(priority: any): boolean {
  return (
    priority === null ||
    typeof priority === 'string' ||
    (typeof priority === 'number' && !isInvalidJSONNumber(priority)) ||
    (priority && typeof priority === 'object' && contains(priority, '.sv'))
  );
};
 
/**
 * Pre-validate a datum passed as an argument to Firebase function.
 *
 * @param {string} fnName
 * @param {number} argumentNumber
 * @param {*} data
 * @param {!Path} path
 * @param {boolean} optional
 */
export const validateFirebaseDataArg = function(
  fnName: string,
  argumentNumber: number,
  data: any,
  path: Path,
  optional: boolean
) {
  if (optional && data === undefined) return;
 
  validateFirebaseData(
    errorPrefixFxn(fnName, argumentNumber, optional),
    data,
    path
  );
};
 
/**
 * Validate a data object client-side before sending to server.
 *
 * @param {string} errorPrefix
 * @param {*} data
 * @param {!Path|!ValidationPath} path_
 */
export const validateFirebaseData = function(
  errorPrefix: string,
  data: any,
  path_: Path | ValidationPath
) {
  const path =
    path_ instanceof Path ? new ValidationPath(path_, errorPrefix) : path_;
 
  Iif (data === undefined) {
    throw new Error(errorPrefix + 'contains undefined ' + path.toErrorString());
  }
  Iif (typeof data === 'function') {
    throw new Error(
      errorPrefix +
        'contains a function ' +
        path.toErrorString() +
        ' with contents = ' +
        data.toString()
    );
  }
  Iif (isInvalidJSONNumber(data)) {
    throw new Error(
      errorPrefix + 'contains ' + data.toString() + ' ' + path.toErrorString()
    );
  }
 
  // Check max leaf size, but try to avoid the utf8 conversion if we can.
  Iif (
    typeof data === 'string' &&
    data.length > MAX_LEAF_SIZE_ / 3 &&
    stringLength(data) > MAX_LEAF_SIZE_
  ) {
    throw new Error(
      errorPrefix +
        'contains a string greater than ' +
        MAX_LEAF_SIZE_ +
        ' utf8 bytes ' +
        path.toErrorString() +
        " ('" +
        data.substring(0, 50) +
        "...')"
    );
  }
 
  // TODO = Perf = Consider combining the recursive validation of keys into NodeFromJSON
  // to save extra walking of large objects.
  if (data && typeof data === 'object') {
    let hasDotValue = false,
      hasActualChild = false;
    forEach(data, function(key: string, value: any) {
      if (key === '.value') {
        hasDotValue = true;
      } else if (key !== '.priority' && key !== '.sv') {
        hasActualChild = true;
        Iif (!isValidKey(key)) {
          throw new Error(
            errorPrefix +
              ' contains an invalid key (' +
              key +
              ') ' +
              path.toErrorString() +
              '.  Keys must be non-empty strings ' +
              'and can\'t contain ".", "#", "$", "/", "[", or "]"'
          );
        }
      }
 
      path.push(key);
      validateFirebaseData(errorPrefix, value, path);
      path.pop();
    });
 
    Iif (hasDotValue && hasActualChild) {
      throw new Error(
        errorPrefix +
          ' contains ".value" child ' +
          path.toErrorString() +
          ' in addition to actual children.'
      );
    }
  }
};
 
/**
 * Pre-validate paths passed in the firebase function.
 *
 * @param {string} errorPrefix
 * @param {Array<!Path>} mergePaths
 */
export const validateFirebaseMergePaths = function(
  errorPrefix: string,
  mergePaths: Path[]
) {
  let i, curPath;
  for (i = 0; i < mergePaths.length; i++) {
    curPath = mergePaths[i];
    const keys = curPath.slice();
    for (let j = 0; j < keys.length; j++) {
      Iif (keys[j] === '.priority' && j === keys.length - 1) {
        // .priority is OK
      } else Iif (!isValidKey(keys[j])) {
        throw new Error(
          errorPrefix +
            'contains an invalid key (' +
            keys[j] +
            ') in path ' +
            curPath.toString() +
            '. Keys must be non-empty strings ' +
            'and can\'t contain ".", "#", "$", "/", "[", or "]"'
        );
      }
    }
  }
 
  // Check that update keys are not descendants of each other.
  // We rely on the property that sorting guarantees that ancestors come
  // right before descendants.
  mergePaths.sort(Path.comparePaths);
  let prevPath: Path | null = null;
  for (i = 0; i < mergePaths.length; i++) {
    curPath = mergePaths[i];
    Iif (prevPath !== null && prevPath.contains(curPath)) {
      throw new Error(
        errorPrefix +
          'contains a path ' +
          prevPath.toString() +
          ' that is ancestor of another path ' +
          curPath.toString()
      );
    }
    prevPath = curPath;
  }
};
 
/**
 * pre-validate an object passed as an argument to firebase function (
 * must be an object - e.g. for firebase.update()).
 *
 * @param {string} fnName
 * @param {number} argumentNumber
 * @param {*} data
 * @param {!Path} path
 * @param {boolean} optional
 */
export const validateFirebaseMergeDataArg = function(
  fnName: string,
  argumentNumber: number,
  data: any,
  path: Path,
  optional: boolean
) {
  Iif (optional && data === undefined) return;
 
  const errorPrefix = errorPrefixFxn(fnName, argumentNumber, optional);
 
  Iif (!(data && typeof data === 'object') || Array.isArray(data)) {
    throw new Error(
      errorPrefix + ' must be an object containing the children to replace.'
    );
  }
 
  const mergePaths: Path[] = [];
  forEach(data, function(key: string, value: any) {
    const curPath = new Path(key);
    validateFirebaseData(errorPrefix, value, path.child(curPath));
    Iif (curPath.getBack() === '.priority') {
      if (!isValidPriority(value)) {
        throw new Error(
          errorPrefix +
            "contains an invalid value for '" +
            curPath.toString() +
            "', which must be a valid " +
            'Firebase priority (a string, finite number, server value, or null).'
        );
      }
    }
    mergePaths.push(curPath);
  });
  validateFirebaseMergePaths(errorPrefix, mergePaths);
};
 
export const validatePriority = function(
  fnName: string,
  argumentNumber: number,
  priority: any,
  optional: boolean
) {
  Iif (optional && priority === undefined) return;
  Iif (isInvalidJSONNumber(priority))
    throw new Error(
      errorPrefixFxn(fnName, argumentNumber, optional) +
        'is ' +
        priority.toString() +
        ', but must be a valid Firebase priority (a string, finite number, ' +
        'server value, or null).'
    );
  // Special case to allow importing data with a .sv.
  Iif (!isValidPriority(priority))
    throw new Error(
      errorPrefixFxn(fnName, argumentNumber, optional) +
        'must be a valid Firebase priority ' +
        '(a string, finite number, server value, or null).'
    );
};
 
export const validateEventType = function(
  fnName: string,
  argumentNumber: number,
  eventType: string,
  optional: boolean
) {
  if (optional && eventType === undefined) return;
 
  switch (eventType) {
    case 'value':
    case 'child_added':
    case 'child_removed':
    case 'child_changed':
    case 'child_moved':
      break;
    default:
      throw new Error(
        errorPrefixFxn(fnName, argumentNumber, optional) +
          'must be a valid event type = "value", "child_added", "child_removed", ' +
          '"child_changed", or "child_moved".'
      );
  }
};
 
export const validateKey = function(
  fnName: string,
  argumentNumber: number,
  key: string,
  optional: boolean
) {
  if (optional && key === undefined) return;
  if (!isValidKey(key))
    throw new Error(
      errorPrefixFxn(fnName, argumentNumber, optional) +
        'was an invalid key = "' +
        key +
        '".  Firebase keys must be non-empty strings and ' +
        'can\'t contain ".", "#", "$", "/", "[", or "]").'
    );
};
 
export const validatePathString = function(
  fnName: string,
  argumentNumber: number,
  pathString: string,
  optional: boolean
) {
  Iif (optional && pathString === undefined) return;
 
  if (!isValidPathString(pathString))
    throw new Error(
      errorPrefixFxn(fnName, argumentNumber, optional) +
        'was an invalid path = "' +
        pathString +
        '". Paths must be non-empty strings and ' +
        'can\'t contain ".", "#", "$", "[", or "]"'
    );
};
 
export const validateRootPathString = function(
  fnName: string,
  argumentNumber: number,
  pathString: string,
  optional: boolean
) {
  Eif (pathString) {
    // Allow '/.info/' at the beginning.
    pathString = pathString.replace(/^\/*\.info(\/|$)/, '/');
  }
 
  validatePathString(fnName, argumentNumber, pathString, optional);
};
 
export const validateWritablePath = function(fnName: string, path: Path) {
  Iif (path.getFront() === '.info') {
    throw new Error(fnName + " failed = Can't modify data under /.info/");
  }
};
 
export const validateUrl = function(
  fnName: string,
  argumentNumber: number,
  parsedUrl: { repoInfo: RepoInfo; path: Path }
) {
  // TODO = Validate server better.
  const pathString = parsedUrl.path.toString();
  Iif (
    !(typeof parsedUrl.repoInfo.host === 'string') ||
    parsedUrl.repoInfo.host.length === 0 ||
    (!isValidKey(parsedUrl.repoInfo.namespace) &&
      parsedUrl.repoInfo.host.split(':')[0] !== 'localhost') ||
    (pathString.length !== 0 && !isValidRootPathString(pathString))
  ) {
    throw new Error(
      errorPrefixFxn(fnName, argumentNumber, false) +
        'must be a valid firebase URL and ' +
        'the path can\'t contain ".", "#", "$", "[", or "]".'
    );
  }
};
 
export const validateCredential = function(
  fnName: string,
  argumentNumber: number,
  cred: any,
  optional: boolean
) {
  if (optional && cred === undefined) return;
  if (!(typeof cred === 'string'))
    throw new Error(
      errorPrefixFxn(fnName, argumentNumber, optional) +
        'must be a valid credential (a string).'
    );
};
 
export const validateBoolean = function(
  fnName: string,
  argumentNumber: number,
  bool: any,
  optional: boolean
) {
  if (optional && bool === undefined) return;
  Iif (typeof bool !== 'boolean')
    throw new Error(
      errorPrefixFxn(fnName, argumentNumber, optional) + 'must be a boolean.'
    );
};
 
export const validateString = function(
  fnName: string,
  argumentNumber: number,
  string: any,
  optional: boolean
) {
  if (optional && string === undefined) return;
  if (!(typeof string === 'string')) {
    throw new Error(
      errorPrefixFxn(fnName, argumentNumber, optional) +
        'must be a valid string.'
    );
  }
};
 
export const validateObject = function(
  fnName: string,
  argumentNumber: number,
  obj: any,
  optional: boolean
) {
  if (optional && obj === undefined) return;
  if (!(obj && typeof obj === 'object') || obj === null) {
    throw new Error(
      errorPrefixFxn(fnName, argumentNumber, optional) +
        'must be a valid object.'
    );
  }
};
 
export const validateObjectContainsKey = function(
  fnName: string,
  argumentNumber: number,
  obj: any,
  key: string,
  optional: boolean,
  opt_type?: string
) {
  const objectContainsKey =
    obj && typeof obj === 'object' && contains(obj, key);
 
  if (!objectContainsKey) {
    if (optional) {
      return;
    } else {
      throw new Error(
        errorPrefixFxn(fnName, argumentNumber, optional) +
          'must contain the key "' +
          key +
          '"'
      );
    }
  }
 
  if (opt_type) {
    const val = safeGet(obj, key);
    if (
      (opt_type === 'number' && !(typeof val === 'number')) ||
      (opt_type === 'string' && !(typeof val === 'string')) ||
      (opt_type === 'boolean' && !(typeof val === 'boolean')) ||
      (opt_type === 'function' && !(typeof val === 'function')) ||
      (opt_type === 'object' && !(typeof val === 'object') && val)
    ) {
      if (optional) {
        throw new Error(
          errorPrefixFxn(fnName, argumentNumber, optional) +
            'contains invalid value for key "' +
            key +
            '" (must be of type "' +
            opt_type +
            '")'
        );
      } else {
        throw new Error(
          errorPrefixFxn(fnName, argumentNumber, optional) +
            'must contain the key "' +
            key +
            '" with type "' +
            opt_type +
            '"'
        );
      }
    }
  }
};