<commit_before>/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
 * You can obtain one at http://mozilla.org/MPL/2.0/. */

const AppDispatcher = require('../dispatcher/appDispatcher')
const AppStore = require('../stores/appStore')
const AppConstants = require('../constants/appConstants')
const appConfig = require('../constants/appConfig')
const settings = require('../constants/settings')
const urlParse = require('url').parse
const siteSettings = require('./siteSettings')
const { registerUserPrefs } = require('./userPrefs')

// backward compatibility with appState siteSettings
const parseSiteSettingsPattern = (pattern) => {
  let normalizedPattern = pattern.replace('https?', 'https')
  let parsed = urlParse(normalizedPattern)
  return '[*.]' + parsed.host
}

const addContentSettings = (settingList, hostPattern, secondaryPattern = undefined, setting = 'block') => {
  let contentSettingsPattern = parseSiteSettingsPattern(hostPattern)
  settingList.push({
    setting,
    secondaryPattern,
    primaryPattern: contentSettingsPattern
  })
}

const getPasswordManagerEnabled = (appState) => {
  let appSettings = appState.get('settings')
  if (appSettings) {
    if (typeof appSettings.get(settings.PASSWORD_MANAGER_ENABLED) === 'boolean') {
      return appSettings.get(settings.PASSWORD_MANAGER_ENABLED)
    }
  }
  return appConfig.defaultSettings[settings.PASSWORD_MANAGER_ENABLED]
}

const getBlock3rdPartyStorage = (braveryDefaults) => {
  if (braveryDefaults.cookieControl === 'block3rdPartyCookie') {
    return [
      {
        setting: 'block',
        primaryPattern: '*',
        secondaryPattern: '*'
      },
      {
        setting: 'allow',
        primaryPattern: '*',
        secondaryPattern: '[firstParty]'
      }
    ]
  } else {
    return [
      {
        setting: 'allow',
        primaryPattern: '*',
        secondaryPattern: '*'
      }
    ]
  }
}

const getContentSettingsFromSiteSettings = (appState) => {
  let braveryDefaults = siteSettings.braveryDefaults(appState, appConfig)

  let contentSettings = {
    cookies: getBlock3rdPartyStorage(braveryDefaults),
    adInsertion: [{
      setting: braveryDefaults.adControl === 'showBraveAds' ? 'allow' : 'block',
      primaryPattern: '*'
    }],
    passwordManager: [{
      setting: getPasswordManagerEnabled(appState) ? 'allow' : 'block',
      primaryPattern: '*'
    }],
    javascript: [],
    canvasFingerprinting: [{
      setting: braveryDefaults.fingerprintingProtection ? 'block' : 'allow',
      primaryPattern: '*'
    }]
  }

  let hostSettings = appState.get('siteSettings').toJS()
  for (var hostPattern in hostSettings) {
    let hostSetting = hostSettings[hostPattern]
    if (hostSetting.noScript) {
      // TODO(bridiver) - enable this when we support temporary overrides
      // addContentSettings(contentSettings.javascript, hostPattern)
    }
    if (hostSetting.cookieControl) {
      if (hostSetting.cookieControl === 'block3rdPartyCookie') {
        addContentSettings(contentSettings.cookies, hostPattern, '*', 'block')
        addContentSettings(contentSettings.cookies, hostPattern, parseSiteSettingsPattern(hostPattern), 'allow')
      } else {
        addContentSettings(contentSettings.cookies, hostPattern, '*', 'allow')
      }
    }
    if (hostSetting.fingerprintingProtection) {
      addContentSettings(contentSettings.canvasFingerprinting, hostPattern, '*', hostSetting.fingerprintingProtection ? 'block' : 'allow')<commit_msg>fix site-specific fingerprinting opt-out fixes #2300 auditors: @diracdeltas<commit_after>/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
 * You can obtain one at http://mozilla.org/MPL/2.0/. */

const AppDispatcher = require('../dispatcher/appDispatcher')
const AppStore = require('../stores/appStore')
const AppConstants = require('../constants/appConstants')
const appConfig = require('../constants/appConfig')
const settings = require('../constants/settings')
const urlParse = require('url').parse
const siteSettings = require('./siteSettings')
const { registerUserPrefs } = require('./userPrefs')

// backward compatibility with appState siteSettings
const parseSiteSettingsPattern = (pattern) => {
  let normalizedPattern = pattern.replace('https?', 'https')
  let parsed = urlParse(normalizedPattern)
  return '[*.]' + parsed.host
}

const addContentSettings = (settingList, hostPattern, secondaryPattern = undefined, setting = 'block') => {
  let contentSettingsPattern = parseSiteSettingsPattern(hostPattern)
  settingList.push({
    setting,
    secondaryPattern,
    primaryPattern: contentSettingsPattern
  })
}

const getPasswordManagerEnabled = (appState) => {
  let appSettings = appState.get('settings')
  if (appSettings) {
    if (typeof appSettings.get(settings.PASSWORD_MANAGER_ENABLED) === 'boolean') {
      return appSettings.get(settings.PASSWORD_MANAGER_ENABLED)
    }
  }
  return appConfig.defaultSettings[settings.PASSWORD_MANAGER_ENABLED]
}

const getBlock3rdPartyStorage = (braveryDefaults) => {
  if (braveryDefaults.cookieControl === 'block3rdPartyCookie') {
    return [
      {
        setting: 'block',
        primaryPattern: '*',
        secondaryPattern: '*'
      },
      {
        setting: 'allow',
        primaryPattern: '*',
        secondaryPattern: '[firstParty]'
      }
    ]
  } else {
    return [
      {
        setting: 'allow',
        primaryPattern: '*',
        secondaryPattern: '*'
      }
    ]
  }
}

const getContentSettingsFromSiteSettings = (appState) => {
  let braveryDefaults = siteSettings.braveryDefaults(appState, appConfig)

  let contentSettings = {
    cookies: getBlock3rdPartyStorage(braveryDefaults),
    adInsertion: [{
      setting: braveryDefaults.adControl === 'showBraveAds' ? 'allow' : 'block',
      primaryPattern: '*'
    }],
    passwordManager: [{
      setting: getPasswordManagerEnabled(appState) ? 'allow' : 'block',
      primaryPattern: '*'
    }],
    javascript: [],
    canvasFingerprinting: [{
      setting: braveryDefaults.fingerprintingProtection ? 'block' : 'allow',
      primaryPattern: '*'
    }]
  }

  let hostSettings = appState.get('siteSettings').toJS()
  for (var hostPattern in hostSettings) {
    let hostSetting = hostSettings[hostPattern]
    if (hostSetting.noScript) {
      // TODO(bridiver) - enable this when we support temporary overrides
      // addContentSettings(contentSettings.javascript, hostPattern)
    }
    if (hostSetting.cookieControl) {
      if (hostSetting.cookieControl === 'block3rdPartyCookie') {
        addContentSettings(contentSettings.cookies, hostPattern, '*', 'block')
        addContentSettings(contentSettings.cookies, hostPattern, parseSiteSettingsPattern(hostPattern), 'allow')
      } else {
        addContentSettings(contentSettings.cookies, hostPattern, '*', 'allow')
      }
    }
    if (typeof hostSetting.fingerprintingProtection === 'boolean') {
      addContentSettings(contentSettings.canvasFingerprinting, hostPattern, '*', hostSetting.fingerprintingProtection ? 'block' : 'allow')
{'fraction_non_alphanumeric': 0.12276072767671156, 'fraction_numerical': 0.002777392028884877, 'mean_word_length': 3.856372218476062, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '6315269', 'n_tokens_mistral': 2068, 'n_tokens_neox': 2005, 'n_words': 422}