diff --git a/htdocs/website/index.php b/htdocs/website/index.php
index 8cfc688f3a9..18b9cc8790a 100644
--- a/htdocs/website/index.php
+++ b/htdocs/website/index.php
@@ -1942,6 +1942,67 @@ if ($action == 'updatecss' && $usercanedit) {
}
}
+if ($action == "updatesecurity" && $usercanedit && GETPOST("btn_WEBSITE_SECURITY_FORCECSP")) {
+ $directivecsp = GETPOST("select_identifier_WEBSITE_SECURITY_FORCECSP");
+ $sourcecsp = GETPOST("select_source_WEBSITE_SECURITY_FORCECSP");
+ $sourcedatacsp = GETPOST("input_data_WEBSITE_SECURITY_FORCECSP");
+
+ $forceCSPArr = websiteGetContentPolicyToArray($forceCSP);
+ $directivesarray = websiteGetContentPolicyDirectives();
+ $sourcesarray = websiteGetContentPolicySources();
+ if (empty($sourcecsp) || empty($directivecsp)) {
+ $error++;
+ }
+ if (!$error) {
+ $directivetype = $directivesarray[$directivecsp]["data-directivetype"];
+ $sourcetype = $sourcesarray[$directivetype][$sourcecsp]["data-sourcetype"];
+ if ($sourcetype == "data") {
+ if (empty($forceCSPArr[$directivecsp]["data"])) {
+ $forceCSPArr[$directivecsp]["data"] = array();
+ }
+ $forceCSPArr[$directivecsp]["data"][] = $sourcedatacsp;
+
+ } else {
+ $forceCSPArr[$directivecsp][] = $sourcecsp;
+ }
+
+ foreach ($forceCSPArr as $directive => $sourcekeys) {
+ if ($securityspstring != ""){
+ $securityspstring .= "; ";
+ }
+ $sourcestring = "";
+ foreach ($sourcekeys as $key => $source) {
+ if (is_array($source)) {
+ $sourcestring .= " data: ". implode(" ", $source);
+ } else {
+ $directivetype = $directivesarray[$directive]["data-directivetype"];
+ $sourcetype = $sourcesarray[$directivetype][$source]["data-sourcetype"];
+ if ($sourcetype == "quoted") {
+ $sourcestring .= " '".$source."'";
+ } else {
+ $sourcestring .= " ".$source;
+ }
+ }
+ }
+ $securityspstring .= $directive . $sourcestring;
+ }
+ $res = dolibarr_set_const($db, 'WEBSITE_'.$object->id.'_SECURITY_FORCECSP', $securityspstring, 'chaine', 0, '', $conf->entity);
+ if ($res <= 0) {
+ $error++;
+ }
+ }
+
+ if (!$error) {
+ $db->commit();
+ setEventMessages($langs->trans("SecurityPolicySucesfullyAdded"), null, 'mesgs');
+ } else {
+ $db->rollback();
+ setEventMessages($langs->trans("ErrorAddingSecurityPolicy"), null, 'errors');
+ }
+ header("Location: ".$_SERVER["PHP_SELF"].'?websiteid='.$websiteid."&action=editsecurity");
+ exit();
+}
+
if ($action == "updatesecurity" && $usercanedit) {
$db->begin();
$res1 = $res2 = $res3 = $res4 = 0;
@@ -2911,15 +2972,31 @@ if ($action == 'removecspsource' && $usercanedit) {
$sourcecsp = explode("_", GETPOST("sourcecsp"));
$directive = $sourcecsp[0];
$sourcekey = $sourcecsp[1];
+ $sourcedata = $sourcecsp[2];
$forceCSPArr = websiteGetContentPolicyToArray($forceCSP);
+ $directivesarray = websiteGetContentPolicyDirectives();
+ $sourcesarray = websiteGetContentPolicySources();
if (!isset($directive) || !isset($sourcekey)) {
$error++;
}
+ $directivetype = $directivesarray[$directive]["data-directivetype"];
+ $sourcetype = $sourcesarray[$directivetype][$sourcekey]["data-sourcetype"];
+
$securityspstring = "";
if (!$error && !empty($forceCSPArr)) {
if (!empty($forceCSPArr[$directive][$sourcekey])) {
- unset($forceCSPArr[$directive][$sourcekey]);
+ if ($sourcetype == "data") {
+ $keydata = array_search($sourcedata, $forceCSPArr[$directive][$sourcekey]);
+ if ($keydata !== false) {
+ unset($forceCSPArr[$directive][$sourcekey][$keydata]);
+ }
+ if (count($forceCSPArr[$directive][$sourcekey]) == 0) {
+ unset($forceCSPArr[$directive][$sourcekey]);
+ }
+ }else {
+ unset($forceCSPArr[$directive][$sourcekey]);
+ }
}
if (count($forceCSPArr[$directive]) == 0) {
unset($forceCSPArr[$directive]);
@@ -2928,7 +3005,21 @@ if ($action == 'removecspsource' && $usercanedit) {
if ($securityspstring != ""){
$securityspstring .= "; ";
}
- $securityspstring .= $directive . " " . implode(" ", $sourcekeys);
+ $sourcestring = "";
+ foreach ($sourcekeys as $key => $source) {
+ if (is_array($source)) {
+ $sourcestring .= " data: ". implode(" ", $source);
+ } else {
+ $directivetype = $directivesarray[$directive]["data-directivetype"];
+ $sourcetype = $sourcesarray[$directivetype][$source]["data-sourcetype"];
+ if ($sourcetype == "quoted") {
+ $sourcestring .= " '".$source."'";
+ } else {
+ $sourcestring .= " ".$source;
+ }
+ }
+ }
+ $securityspstring .= $directive . $sourcestring;
}
$res = dolibarr_set_const($db, 'WEBSITE_'.$object->id.'_SECURITY_FORCECSP', $securityspstring, 'chaine', 0, '', $conf->entity);
if ($res <= 0) {
@@ -4294,18 +4385,20 @@ if ($action == 'editsecurity') {
print '
| ';
print ''.$form->selectarray("select_identifier_WEBSITE_SECURITY_FORCECSP", $selectarraySP, "select_identifier_WEBSITE_SECURITY_FORCECSP", 1, 0, 0, '', 0, 0, 0, '', 'minwidth300').' | ';
print '';
+ print '';
foreach ($selectarraySPlevel2 as $key => $values) {
print ' ';
print $form->selectarray("select_".$key."_WEBSITE_SECURITY_FORCECSP", $values, "select_".$key."_WEBSITE_SECURITY_FORCECSP", 1, 0, 0, '', 0, 0, 0, '', 'minwidth300 select_WEBSITE_SECURITY_FORCECSP');
print ' ';
}
print ' | ';
- print ' | ';
- print ' | ';
+ print ' | ';
+ print ' | ';
print '
';
print '';
print '';
+ //TODO: add comment to explain + better look
$forceCSPArr = websiteGetContentPolicyToArray($forceCSP);
print '';
@@ -4314,84 +4407,26 @@ if ($action == 'editsecurity') {
print '
'.$directive.'';
print '';
foreach ($sources as $key => $source) {
- print '- '.$source.' '.img_delete().'
';
+ if (is_array($source)){
+ print '- '.$key.'';
+ print '
';
+ foreach ($source as $keysource => $sourcedata) {
+ print '- '.$sourcedata.' '.img_delete().'
';
+ }
+ print '
';
+ print ' ';
+ } else {
+ print '- '.$source.' '.img_delete().'
';
+ }
}
print '
';
print '';
}
print '';
print '
';
-
+ //TODO: add console.log in js
print '';
diff --git a/htdocs/website/lib/website.lib.php b/htdocs/website/lib/website.lib.php
index a55150c6194..ba6039683ca 100644
--- a/htdocs/website/lib/website.lib.php
+++ b/htdocs/website/lib/website.lib.php
@@ -94,7 +94,7 @@ function websiteconfigPrepareHead($object)
/**
* Prepare array of directives for Website
*
- * @return array Array of tabs
+ * @return array Array of directives
*/
function websiteGetContentPolicyDirectives()
{
@@ -128,7 +128,7 @@ function websiteGetContentPolicyDirectives()
"report-to" => array("label" => "report-to", "data-directivetype" => "reporting"),
// Other directives
"require-trusted-types-for" => array("label" => "require-trusted-types-for", "data-directivetype" => "other"),
- "othertrusted-types",
+ "othertrusted-types" => array("label" => "othertrusted-types", "data-directivetype" => "other"),
"upgrade-insecure-requests" => array("label" => "upgrade-insecure-requests", "data-directivetype" => "other"),
);
}
@@ -136,7 +136,7 @@ function websiteGetContentPolicyDirectives()
/**
* Prepare array of sources for Website
*
- * @return array Array of tabs
+ * @return array Array of sources
*/
function websiteGetContentPolicySources()
{
@@ -145,32 +145,43 @@ function websiteGetContentPolicySources()
"fetch" => array(
"*" => array("label" => "*", "data-sourcetype" => "select"),
"data" => array("label" => "data", "data-sourcetype" => "data"),
- "self" => array("label" => "self", "data-sourcetype" => "select"),
+ "self" => array("label" => "self", "data-sourcetype" => "quoted"),
),
// Document directives
"document" => array(
- "base-uri",
- "sandbox",
+ "base-uri" => array("label" => "base-uri", "data-sourcetype" => "select"),
+ "sandbox" => array("label" => "sandbox", "data-sourcetype" => "select"),
),
// Navigation directives
"navigation" => array(
- "self",
+ "self" => array("label" => "self", "data-sourcetype" => "quoted"),
),
// Reporting directives
"reporting" => array(
- "report-to",
+ "report-to" => array("label" => "report-to", "data-sourcetype" => "select"),
),
// Other directives
"other" => array(
- "require-trusted-types-for",
- "trusted-types",
- "upgrade-insecure-requests",
+ "require-trusted-types-for" => array("label" => "require-trusted-types-for", "data-sourcetype" => "select"),
+ "trusted-types" => array("label" => "trusted-types", "data-sourcetype" => "select"),
+ "upgrade-insecure-requests" => array("label" => "upgrade-insecure-requests", "data-sourcetype" => "select"),
),
);
}
+/**
+ * Transform a Content Security Policy to an array
+ * @param string content security policy
+ *
+ * @return array Array of sources
+ */
function websiteGetContentPolicyToArray($forceCSP){
$forceCSPArr = array();
+ $sourceCSPArr = websiteGetContentPolicySources();
+ $sourceCSPArrflatten = array();
+ foreach ($sourceCSPArr as $key => $arr) {
+ $sourceCSPArrflatten = array_merge($sourceCSPArrflatten, array_keys($arr));
+ }
$securitypolicies = explode(";", $forceCSP);
foreach ($securitypolicies as $key => $securitypolicy) {
if ($securitypolicy == "") continue;
@@ -183,11 +194,29 @@ function websiteGetContentPolicyToArray($forceCSP){
continue;
}
$sources = $securitypolicyarr;
+ $issourcedata = 0;
foreach ($sources as $key => $source) {
- if (empty($forceCSPArr[$directive])) {
- $forceCSPArr[$directive] = array($source);
+ $source = str_replace(":", "", $source);
+ $source = str_replace("'", "", $source);
+
+ if ($source == "data") {
+ $issourcedata = 1;
+ if (empty($forceCSPArr[$directive])) {
+ $forceCSPArr[$directive] = array($source => array());
+ } else {
+ $forceCSPArr[$directive][$source] = array();
+ }
+ continue;
+ }
+ if ($issourcedata && !in_array($source, $sourceCSPArrflatten)) {
+ $forceCSPArr[$directive]["data"][] = $source;
} else {
- $forceCSPArr[$directive][] = $source;
+ $issourcedata = 0;
+ if (empty($forceCSPArr[$directive])) {
+ $forceCSPArr[$directive] = array($source);
+ } else {
+ $forceCSPArr[$directive][] = $source;
+ }
}
}
}