From 4e99ab02dfac8b0ad067bc2802491ce5d32a439c Mon Sep 17 00:00:00 2001 From: Lucas Marcouiller <45882981+Hystepik@users.noreply.github.com> Date: Sat, 8 Mar 2025 14:22:06 +0100 Subject: [PATCH] Fix content website security page (#33363) * Fix look and feel of website security page and add Wesite card in admin/system/security * fix error * fix add gray text on select --- htdocs/admin/system/security.php | 47 +++++++++++++++++++++++- htdocs/langs/en_US/admin.lang | 1 + htdocs/langs/en_US/website.lang | 3 ++ htdocs/website/index.php | 57 +++++++++++++++++++----------- htdocs/website/lib/website.lib.php | 6 +++- 5 files changed, 91 insertions(+), 23 deletions(-) diff --git a/htdocs/admin/system/security.php b/htdocs/admin/system/security.php index 2861b0fce9b..951bdc93b9e 100644 --- a/htdocs/admin/system/security.php +++ b/htdocs/admin/system/security.php @@ -734,6 +734,52 @@ print ''; print '
'; print '
'; +// Websites + +print load_fiche_titre($langs->trans("Website"), '', 'website'); +print '
'; + +$sql = "SELECT w.rowid as id, w.ref"; +$sql .= " FROM ".MAIN_DB_PREFIX."website as w"; +$sql .= " WHERE w.entity = ".((int) $conf->entity); +$sql .= " AND w.status = 1"; + +$resql = $db->query($sql); +if ($resql) { + $num_rows = $db->num_rows($resql); + if ($num_rows > 0) { + $i = 0; + while ($obj = $db->fetch_object($resql)) { + print "".$langs->trans("RefWebsite").": ".$obj->ref.""; + print'

'; + print 'WEBSITE_'.$obj->id.'_SECURITY_FORCERP = '.getDolGlobalString('WEBSITE_'.$obj->id.'_SECURITY_FORCERP', ''.$langs->trans("Undefined").''); + print '   ('.$langs->trans("Example").': "'; + $examplecsprule = "default-src 'self' 'unsafe-inline' matomo.".getDomainFromURL($_SERVER["SERVER_NAME"], 1)." *.transifex.net *.transifex.com *.cloudflare.com *.cloudflareinsights.com *.google-analytics.com *.googletagmanager.com *.google.com *.gstatic.com *.googleapis.com *.googleadservices.com *.ads-twitter.com *.doubleclick.net; frame-ancestors 'self'; object-src *.youtube.com; frame-src 'self' *.twitter.com *.facebook.com *.youtube.com; img-src * data:;"; + print $examplecsprule; + print '")
'; + + print 'WEBSITE_'.$obj->id.'_SECURITY_FORCECSP = '.getDolGlobalString('WEBSITE_'.$obj->id.'_SECURITY_FORCECSP', ''.$langs->trans("Undefined").''); + print '   ('.$langs->trans("Example").': "'; + $examplecsprule = "default-src 'self' 'unsafe-inline' matomo.".getDomainFromURL($_SERVER["SERVER_NAME"], 1)." *.transifex.net *.transifex.com *.cloudflare.com *.cloudflareinsights.com *.google-analytics.com *.googletagmanager.com *.google.com *.gstatic.com *.googleapis.com *.googleadservices.com *.ads-twitter.com *.doubleclick.net; frame-ancestors 'self'; object-src *.youtube.com; frame-src 'self' *.twitter.com *.facebook.com *.youtube.com; img-src * data:;"; + print $examplecsprule; + print '")
'; + + print 'WEBSITE_'.$obj->id.'_SECURITY_FORCERP = '.getDolGlobalString('WEBSITE_'.$obj->id.'_SECURITY_FORCERP', ''.$langs->trans("Undefined").'').'   ('.$langs->trans("Recommended").': '.$langs->trans("Undefined").'="strict-origin-when-cross-origin" '.$langs->trans("or").' "same-origin"=more secured)
'; + + print 'WEBSITE_'.$obj->id.'_SECURITY_FORCESTS = '.getDolGlobalString('WEBSITE_'.$obj->id.'_SECURITY_FORCESTS', ''.$langs->trans("Undefined").'').'   ('.$langs->trans("Example").": \"max-age=31536000; includeSubDomains\")
"; + + print 'WEBSITE_'.$obj->id.'_SECURITY_FORCEPP = '.getDolGlobalString('WEBSITE_'.$obj->id.'_SECURITY_FORCEPP', ''.$langs->trans("Undefined").'').'   ('.$langs->trans("Example").": \"camera=(), microphone=(), geolocation=*\")
"; + $i++; + if ($i != $num_rows) { + print '
'; + } + } + } +} else { + dol_print_error($db); +} +print '
'; + // Other - experimental @@ -845,7 +891,6 @@ print '
'; print 'MAIN_SECURITY_FORCE_ACCESS_CONTROL_ALLOW_ORIGIN = '.getDolGlobalString('MAIN_SECURITY_FORCE_ACCESS_CONTROL_ALLOW_ORIGIN', ''.$langs->trans("Undefined").'').'   ('.$langs->trans("Recommended").": 1)
"; print '
'; -// For websites print 'WEBSITE_MAIN_SECURITY_FORCECSPRO = '.getDolGlobalString('WEBSITE_MAIN_SECURITY_FORCECSPRO', ''.$langs->trans("Undefined").''); print '   ('.$langs->trans("Example").': "'; diff --git a/htdocs/langs/en_US/admin.lang b/htdocs/langs/en_US/admin.lang index 9bd3a458969..261d0066f0d 100644 --- a/htdocs/langs/en_US/admin.lang +++ b/htdocs/langs/en_US/admin.lang @@ -2639,3 +2639,4 @@ NotConnected=Not connected HTTPHeaderEditor=This page allows you to edit security related HTTP headers ReservedToAdvancedUsers=It is reserverd for advanced users HTTPHeader=HTTP Header +RefWebsite=Website reference \ No newline at end of file diff --git a/htdocs/langs/en_US/website.lang b/htdocs/langs/en_US/website.lang index 5d2ed2e3910..ee42f9a75e5 100644 --- a/htdocs/langs/en_US/website.lang +++ b/htdocs/langs/en_US/website.lang @@ -377,3 +377,6 @@ SecurityPolicySucesfullyRemoved=Content Security Policy successfully removed ErrorRemovingSecurityPolicy=An error occurred when trying to remove a Content Security Policy SecurityPolicySucesfullyAdded=Content Security Policy successfully added ErrorAddingSecurityPolicy=An error occurred when trying to add a Content Security Policy +UnknowContentSecurityPolicyDirective=Unknown Content Security Policy directive syntax +FillCSPDirective=Please fill Content Security Policy directive +FillCSPSource=Please fill Content Security Policy source \ No newline at end of file diff --git a/htdocs/website/index.php b/htdocs/website/index.php index cbf9b94cdbb..3abb5465ce5 100644 --- a/htdocs/website/index.php +++ b/htdocs/website/index.php @@ -2982,7 +2982,6 @@ if ($action == 'generatesitemaps' && $usercanedit) { if ($action == 'removecspsource' && $usercanedit) { $db->begin(); - $sourcetype = ""; $sourcecsp = explode("_", GETPOST("sourcecsp")); $directive = $sourcecsp[0]; @@ -2994,19 +2993,18 @@ if ($action == 'removecspsource' && $usercanedit) { if (empty($directive)) { $error++; } - if ($error || (!isset($sourcekey) && $directivesarray[$directive]["data-directivetype"] != "none")) { - $error++; - } - $directivetype = (string) $directivesarray[$directive]["data-directivetype"]; - if (isset($sourcekey)) { - $sourcetype = $sourcesarray[$directivetype][$sourcekey]["data-sourcetype"]; + if (!empty($directivesarray[$directive])) { + $directivetype = (string) $directivesarray[$directive]["data-directivetype"]; + if (isset($sourcekey)) { + $sourcetype = $sourcesarray[$directivetype][$sourcekey]["data-sourcetype"]; + } } $securityspstring = ""; if (!$error && !empty($forceCSPArr)) { if (isset($sourcekey) && !empty($forceCSPArr[$directive][$sourcekey])) { - if ($sourcetype == "data") { + if ($sourcetype == "data" || preg_match('/data/', $sourcekey)) { $keydata = array_search($sourcedata, $forceCSPArr[$directive][$sourcekey]); if ($keydata !== false) { unset($forceCSPArr[$directive][$sourcekey][$keydata]); @@ -4417,31 +4415,38 @@ if ($action == 'editsecurity') { print '
'; - print '
'; + print ' '.img_picto('', 'add').'
'; print '
'; print '
'; print img_picto('', 'graph', 'class="pictofixedwidth"').$langs->trans("HierarchicView").'
'; - print $form->selectarray("select_identifier_WEBSITE_SECURITY_FORCECSP", $selectarrayCSPDirectives, "select_identifier_WEBSITE_SECURITY_FORCECSP", 1, 0, 0, '', 0, 0, 0, '', 'minwidth200 maxwidth300 inline-block'); + print ''; // Content Security Policy list of selected rules print '
'; print '
    '; foreach ($forceCSPArr as $directive => $sources) { - print '
  • '.$directive.''; + print '
  • '; + if (in_array($directive, array_keys($selectarrayCSPDirectives))) { + print ''.$directive.''; + } else { + print $form->textwithpicto($directive, $langs->trans("UnknowContentSecurityPolicyDirective"), 1, 'warning'); + } if (!empty($sources)) { print '
      '; foreach ($sources as $key => $source) { @@ -4477,7 +4482,7 @@ if ($action == 'editsecurity') { print '
      '; - print ''; + print ''; print ''; print '
      '; @@ -4485,15 +4490,25 @@ if ($action == 'editsecurity') { print '