<commit_before>/* eslint-env node */

'use strict';

const fs = require('fs');
const path = require('path');

const CSP_SELF = "'self'";
const CSP_NONE = "'none'";

const CSP_HEADER = 'Content-Security-Policy';

const DELIVERY_HEADER = 'header';
const DELIVERY_META   = 'meta';

const unique = function(array) {
  return array.filter(function(value, index, self) {
    return self.indexOf(value) === index;
  });
};

const buildPolicyString = function(policyObject) {
  return Object.keys(policyObject).reduce(function(memo, name) {
    var value = policyObject[name];
    if (value === null) {
      // Override the default value of `'self'`. Instead no entry will be included
      // in the CSP. This, in turn, will cause the CSP to fallback to `default-src`
      // for this directive. http://www.w3.org/TR/CSP2/#default-src-usage
      return memo;
    } else {
      var sourceList = Array.isArray(value) ? unique(value).join(' ') : value;
      return memo + name + ' ' + sourceList + '; ';
    }
  }, '').trim();
};

const getConfigPath = function(projectPkg, projectRoot) {
  let configDir = 'config';

  if (projectPkg['ember-addon'] && projectPkg['ember-addon']['configPath']) {
    configDir = projectPkg['ember-addon']['configPath'];
  }

  return path.join(projectRoot, configDir, 'content-security-policy.js');
};

/**
 * Returns the configuration stored in `config/content-security-policy.js`.
 * Returns an empty object if that file does not exist.
 *
 * @param {string} project
 * @return {object}
 */
const readConfig = function(project, environment) {
  let configPath = getConfigPath(project.pkg, project.root);

  return fs.existsSync(configPath) ? require(configPath)(environment) : {};
};

/**
 * Calculates the configuration based on
 * - own config (`config/content-security-policy.js`) and
 * - run-time config (`config/environment.js`) for legacy support.
 *
 * @params {string} environment
 * @params {object} ownConfig
 * @params {object} runConfig
 * @params {object} ui
 * @returns {object}
 */
function calculateConfig(environment, ownConfig, runConfig, ui) {
  let config = {
    delivery: [DELIVERY_HEADER],
    enabled: true,
    failTests: true,
    policy: {
      'default-src':  [CSP_NONE],
      'script-src':   [CSP_SELF],
      'font-src':     [CSP_SELF],
      'connect-src':  [CSP_SELF],
      'img-src':      [CSP_SELF],
      'style-src':    [CSP_SELF],
      'media-src':    [CSP_SELF],
    },
    reportOnly: true,
  };

  // testem requires frame-src to run
  if (environment === 'test') {
    config.policy['frame-src'] = CSP_SELF;
  }

  ui.writeWarnLine(
    'Configuring ember-cli-content-security-policy using `contentSecurityPolicy`, ' +
    '`contentSecurityPolicyHeader` and `contentSecurityPolicyMeta` keys in `config/environment.js` ' +
    'is deprecate and will be removed in v2.0.0. ember-cli-content-security-policy is now configured ' +
    'using `ember-cli-build.js`. Please find detailed information about new configuration options ' +
    'in addon documentation at https://github.com/rwjblue/ember-cli-content-security-policy/blob/master/DEPRECATIONS.md.',
    !runConfig.contentSecurityPolicy || !runConfig.contentSecurityPolicyHeader || !runConfig.contentSecurityPolicyMeta<commit_msg>upgrade deprecation message<commit_after>/* eslint-env node */

'use strict';

const fs = require('fs');
const path = require('path');

const CSP_SELF = "'self'";
const CSP_NONE = "'none'";

const CSP_HEADER = 'Content-Security-Policy';

const DELIVERY_HEADER = 'header';
const DELIVERY_META   = 'meta';

const unique = function(array) {
  return array.filter(function(value, index, self) {
    return self.indexOf(value) === index;
  });
};

const buildPolicyString = function(policyObject) {
  return Object.keys(policyObject).reduce(function(memo, name) {
    var value = policyObject[name];
    if (value === null) {
      // Override the default value of `'self'`. Instead no entry will be included
      // in the CSP. This, in turn, will cause the CSP to fallback to `default-src`
      // for this directive. http://www.w3.org/TR/CSP2/#default-src-usage
      return memo;
    } else {
      var sourceList = Array.isArray(value) ? unique(value).join(' ') : value;
      return memo + name + ' ' + sourceList + '; ';
    }
  }, '').trim();
};

const getConfigPath = function(projectPkg, projectRoot) {
  let configDir = 'config';

  if (projectPkg['ember-addon'] && projectPkg['ember-addon']['configPath']) {
    configDir = projectPkg['ember-addon']['configPath'];
  }

  return path.join(projectRoot, configDir, 'content-security-policy.js');
};

/**
 * Returns the configuration stored in `config/content-security-policy.js`.
 * Returns an empty object if that file does not exist.
 *
 * @param {string} project
 * @return {object}
 */
const readConfig = function(project, environment) {
  let configPath = getConfigPath(project.pkg, project.root);

  return fs.existsSync(configPath) ? require(configPath)(environment) : {};
};

/**
 * Calculates the configuration based on
 * - own config (`config/content-security-policy.js`) and
 * - run-time config (`config/environment.js`) for legacy support.
 *
 * @params {string} environment
 * @params {object} ownConfig
 * @params {object} runConfig
 * @params {object} ui
 * @returns {object}
 */
function calculateConfig(environment, ownConfig, runConfig, ui) {
  let config = {
    delivery: [DELIVERY_HEADER],
    enabled: true,
    failTests: true,
    policy: {
      'default-src':  [CSP_NONE],
      'script-src':   [CSP_SELF],
      'font-src':     [CSP_SELF],
      'connect-src':  [CSP_SELF],
      'img-src':      [CSP_SELF],
      'style-src':    [CSP_SELF],
      'media-src':    [CSP_SELF],
    },
    reportOnly: true,
  };

  // testem requires frame-src to run
  if (environment === 'test') {
    config.policy['frame-src'] = CSP_SELF;
  }

  ui.writeWarnLine(
    'Configuring ember-cli-content-security-policy using `contentSecurityPolicy`, ' +
    '`contentSecurityPolicyHeader` and `contentSecurityPolicyMeta` keys in `config/environment.js` ' +
    'is deprecate and will be removed in v3.0.0. ember-cli-content-security-policy is now configured ' +
    'using `ember-cli-build.js`. Please find detailed information about new configuration options ' +
    'in addon documentation at https://github.com/rwjblue/ember-cli-content-security-policy/blob/master/DEPRECATIONS.md.',
    !runConfig.contentSecurityPolicy || !runConfig.contentSecurityPolicyHeader || !runConfig.contentSecurityPolicyMeta
{'fraction_non_alphanumeric': 0.1457886676875957, 'fraction_numerical': 0.0015313935681470138, 'mean_word_length': 3.72234273318872, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, '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': '3456578', 'n_tokens_mistral': 2064, 'n_tokens_neox': 2045, 'n_words': 499}