<commit_before>/**
 * Mailvelope - secure email with OpenPGP encryption for Webmail
 * Copyright (C) 2014-2015 Mailvelope GmbH
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License version 3
 * as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

'use strict';

var mvelo = mvelo || {};

mvelo.domAPI = {};

mvelo.domAPI.active = false;

mvelo.domAPI.containers = new Map();

mvelo.domAPI.host = null;

// must be a singelton
mvelo.syncHandler = null;

mvelo.domAPI.init = function() {
  this.active = mvelo.main.watchList.some(function(site) {
    return site.active && site.frames && site.frames.some(function(frame) {
      var hostRegex = mvelo.util.matchPattern2RegEx(frame.frame);
      var validHost = hostRegex.test(window.location.hostname);
      if (frame.scan && frame.api && validHost) {
        // host = match pattern without *. prefix
        mvelo.domAPI.host = frame.frame.replace(/^\*\./, '');
        return true;
      }
    });
  });
  if (this.active) {
    window.addEventListener('message', mvelo.domAPI.eventListener);
    if (!window.mailvelope) {
      $('<script/>', {
        src: mvelo.extension.getURL('common/client-API/mailvelope-client-api.js')
      }).appendTo($('head'));
    }
  }
};

mvelo.domAPI.postMessage = function(eventName, id, data, error) {
  window.postMessage({
    event: eventName,
    mvelo_extension: true,
    id: id,
    data: data,
    error: error
  }, window.location.origin);
};

mvelo.domAPI.reply = function(id, error, data) {
  if (error) {
    error = { message: error.message || error, code: error.code  || 'INTERNAL_ERROR' };
  }
  mvelo.domAPI.postMessage('callback-reply', id, data, error);
};

mvelo.domAPI.dataTypes = {
  identifier: 'string',
  selector: 'string',
  armored: 'string',
  options: 'object',
  recipients: 'array',
  emailAddr: 'string',
  dataURL: 'string',
  revision: 'number',
  fingerprint: 'string',
  syncHandlerObj: 'object',
  editorId: 'string',
  generatorId: 'string',
  popupId: 'string',
  syncHandlerId: 'string',
  syncType: 'string',
  syncData: 'object',
  error: 'object',
  restoreId: 'string',
  restoreBackup: 'string',
  id: 'string'
};

mvelo.domAPI.optionsTypes = {
  showExternalContent: 'boolean',
  quota: 'number',
  predefinedText: 'string',
  quotedMail: 'string',
  signMsg: 'boolean',
  quotedMailIndent: 'boolean',
  quotedMailHeader: 'string',
  userIds: 'array',
  keySize: 'number',
  initialSetup: 'boolean',
  senderAddress: 'string',
  restorePassword: '<PASSWORD>',
  email: 'string',
  fullName: 'string',
  keepAttachments: 'boolean'
};

mvelo.domAPI.checkTypes = function(data) {
  var error;
  if (data.id && typeof data.id !== 'string') {
    error = new Error('Type mismatch: data.id should be of type string.');
    error.code = 'TYPE_MISMATCH';
    throw error;
  }
  if (!data.data) {
    return;
  }
  this.enforceTypeWhitelist(data.data, mvelo.domAPI.dataTypes);
  if (data.data.options && typeof data.data.options === 'object') {
    this.enforceTypeWhitelist(data.data.options, mvelo.domAPI.optionsTypes);
  }
};

mvelo.domAPI.enforceTypeWhitelist = function(data, whitelist) {
  var error;
  var parameters = Object.keys(data) || [];
  for (var i = 0; i < parameters.length; i++) {
    var parameter = parameters[i];
    var dataType = whitelist[parameter];
    var value = data[parameter];
    if (dataType === undefined) {
      console.log('Mailvelope client-API type checker: parameter ' + parameter + ' not accepted.');
      delete data[parameter];
      continue;
    }
    if (value === undefined) {
      continue;
    }
    var wrong = false;
    switch (dataType) {
      case 'array':
        if (!Array.isArray(value)) {
          wrong = true;
        }
        break;
      default:
        if (typeof value !== dataType) {
          wrong = true;
        }
    }
    if (wrong) {
      error = new Error('Type mismatch: ' + parameter + ' should be of type ' + dataType + '.');
      error.code = 'TYPE_MISMATCH';
      throw error;
    }
  }
};
<commit_msg>Ignore null values in API type checker<commit_after>/**
 * Mailvelope - secure email with OpenPGP encryption for Webmail
 * Copyright (C) 2014-2015 Mailvelope GmbH
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License version 3
 * as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

'use strict';

var mvelo = mvelo || {};

mvelo.domAPI = {};

mvelo.domAPI.active = false;

mvelo.domAPI.containers = new Map();

mvelo.domAPI.host = null;

// must be a singelton
mvelo.syncHandler = null;

mvelo.domAPI.init = function() {
  this.active = mvelo.main.watchList.some(function(site) {
    return site.active && site.frames && site.frames.some(function(frame) {
      var hostRegex = mvelo.util.matchPattern2RegEx(frame.frame);
      var validHost = hostRegex.test(window.location.hostname);
      if (frame.scan && frame.api && validHost) {
        // host = match pattern without *. prefix
        mvelo.domAPI.host = frame.frame.replace(/^\*\./, '');
        return true;
      }
    });
  });
  if (this.active) {
    window.addEventListener('message', mvelo.domAPI.eventListener);
    if (!window.mailvelope) {
      $('<script/>', {
        src: mvelo.extension.getURL('common/client-API/mailvelope-client-api.js')
      }).appendTo($('head'));
    }
  }
};

mvelo.domAPI.postMessage = function(eventName, id, data, error) {
  window.postMessage({
    event: eventName,
    mvelo_extension: true,
    id: id,
    data: data,
    error: error
  }, window.location.origin);
};

mvelo.domAPI.reply = function(id, error, data) {
  if (error) {
    error = { message: error.message || error, code: error.code  || 'INTERNAL_ERROR' };
  }
  mvelo.domAPI.postMessage('callback-reply', id, data, error);
};

mvelo.domAPI.dataTypes = {
  identifier: 'string',
  selector: 'string',
  armored: 'string',
  options: 'object',
  recipients: 'array',
  emailAddr: 'string',
  dataURL: 'string',
  revision: 'number',
  fingerprint: 'string',
  syncHandlerObj: 'object',
  editorId: 'string',
  generatorId: 'string',
  popupId: 'string',
  syncHandlerId: 'string',
  syncType: 'string',
  syncData: 'object',
  error: 'object',
  restoreId: 'string',
  restoreBackup: 'string',
  id: 'string'
};

mvelo.domAPI.optionsTypes = {
  showExternalContent: 'boolean',
  quota: 'number',
  predefinedText: 'string',
  quotedMail: 'string',
  signMsg: 'boolean',
  quotedMailIndent: 'boolean',
  quotedMailHeader: 'string',
  userIds: 'array',
  keySize: 'number',
  initialSetup: 'boolean',
  senderAddress: 'string',
  restorePassword: 'boolean',
  email: 'string',
  fullName: 'string',
  keepAttachments: 'boolean'
};

mvelo.domAPI.checkTypes = function(data) {
  var error;
  if (data.id && typeof data.id !== 'string') {
    error = new Error('Type mismatch: data.id should be of type string.');
    error.code = 'TYPE_MISMATCH';
    throw error;
  }
  if (!data.data) {
    return;
  }
  this.enforceTypeWhitelist(data.data, mvelo.domAPI.dataTypes);
  if (data.data.options && typeof data.data.options === 'object') {
    this.enforceTypeWhitelist(data.data.options, mvelo.domAPI.optionsTypes);
  }
};

mvelo.domAPI.enforceTypeWhitelist = function(data, whitelist) {
  var error;
  var parameters = Object.keys(data) || [];
  for (var i = 0; i < parameters.length; i++) {
    var parameter = parameters[i];
    var dataType = whitelist[parameter];
    var value = data[parameter];
    if (dataType === undefined) {
      console.log('Mailvelope client-API type checker: parameter ' + parameter + ' not accepted.');
      delete data[parameter];
      continue;
    }
    if (value === undefined || value === null) {
      continue;
    }
    var wrong = false;
    switch (dataType) {
      case 'array':
        if (!Array.isArray(value)) {
          wrong = true;
        }
        break;
      default:
        if (typeof value !== dataType) {
          wrong = true;
        }
    }
    if (wrong) {
      error = new Error('Type mismatch: ' + parameter + ' should be of type ' + dataType + '.');
      error.code = 'TYPE_MISMATCH';
      throw error;
    }
  }
};

{'fraction_non_alphanumeric': 0.1419889502762431, 'fraction_numerical': 0.002430939226519337, 'mean_word_length': 3.2195804195804194, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '486135', 'n_tokens_mistral': 2941, 'n_tokens_neox': 2913, 'n_words': 766}