in lib/appium/helpers/appPatcher.js [47:80]
module.exports.addCspSource = function (appPath, directive, source) {
const cspInclFile = path.join(appPath, 'www', 'csp-incl.js');
const indexFile = path.join(appPath, 'www', 'index.html');
const cspFile = fs.existsSync(cspInclFile) ? cspInclFile : indexFile;
let cspContent = fs.readFileSync(cspFile, utilities.DEFAULT_ENCODING);
const cspTagOpening = '<meta http-equiv="Content-Security-Policy" content=\'';
const cspRule = directive + ' ' + source;
const cspRuleReg = new RegExp(directive + '[^;"]+' + source.replace('*', '\\*'));
logger.normal('paramedic-appium: Adding CSP source "' + source + '" to directive "' + directive + '"');
if (cspContent.match(cspRuleReg)) {
logger.normal('paramedic-appium: It\'s already there.');
} else if (utilities.contains(cspContent, directive)) {
// if the directive is there, just add the source to it
cspContent = cspContent.replace(directive, cspRule);
fs.writeFileSync(cspFile, cspContent, utilities.DEFAULT_ENCODING);
} else if (cspContent.match(/content=".*?default-src.+?"/)) {
// needed directive is not there but there is default-src directive
// creating needed directive and copying default-src sources to it
const defaultSrcReg = /(content=".*?default-src)(.+?);/;
cspContent = cspContent.replace(defaultSrcReg, '$1$2; ' + cspRule + '$2;');
fs.writeFileSync(cspFile, cspContent, utilities.DEFAULT_ENCODING);
} else if (utilities.contains(cspContent, cspTagOpening)) {
// needed directive is not there and there is no default-src directive
// but the CSP tag is till present
// just adding needed directive to a start of CSP tag content
cspContent = cspContent.replace(cspTagOpening, cspTagOpening + directive + ' ' + source + '; ');
fs.writeFileSync(cspFile, cspContent, utilities.DEFAULT_ENCODING);
} else {
// no CSP tag, skipping
logger.normal('paramedic-appium: WARNING: No CSP tag found.');
}
};