function isValidRequest()

in src/common/providers/https.ts [422:462]


function isValidRequest(req: Request): req is HttpRequest {
  // The body must not be empty.
  if (!req.body) {
    logger.warn('Request is missing body.');
    return false;
  }

  // Make sure it's a POST.
  if (req.method !== 'POST') {
    logger.warn('Request has invalid method.', req.method);
    return false;
  }

  // Check that the Content-Type is JSON.
  let contentType = (req.header('Content-Type') || '').toLowerCase();
  // If it has a charset, just ignore it for now.
  const semiColon = contentType.indexOf(';');
  if (semiColon >= 0) {
    contentType = contentType.substr(0, semiColon).trim();
  }
  if (contentType !== 'application/json') {
    logger.warn('Request has incorrect Content-Type.', contentType);
    return false;
  }

  // The body must have data.
  if (typeof req.body.data === 'undefined') {
    logger.warn('Request body is missing data.', req.body);
    return false;
  }

  // TODO(klimt): Allow only specific http headers.

  // Verify that the body does not have any extra fields.
  const extraKeys = Object.keys(req.body).filter((field) => field !== 'data');
  if (extraKeys.length !== 0) {
    logger.warn('Request body has extra fields: ', extraKeys.join(', '));
    return false;
  }
  return true;
}