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
This commit is contained in:
Lucas Marcouiller 2025-03-08 14:22:06 +01:00 committed by GitHub
parent c97722c1cc
commit 4e99ab02df
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 91 additions and 23 deletions

View file

@ -734,6 +734,52 @@ print '</div>';
print '<br>';
print '<br>';
// Websites
print load_fiche_titre($langs->trans("Website"), '', 'website');
print '<div class="divsection wordbreak">';
$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 "<strong>".$langs->trans("RefWebsite").": ".$obj->ref."</strong>";
print'<br><br>';
print '<strong>WEBSITE_'.$obj->id.'_SECURITY_FORCERP</strong> = '.getDolGlobalString('WEBSITE_'.$obj->id.'_SECURITY_FORCERP', '<span class="opacitymedium">'.$langs->trans("Undefined").'</span>');
print ' &nbsp; <span class="opacitymedium">('.$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 '")</span><br>';
print '<strong>WEBSITE_'.$obj->id.'_SECURITY_FORCECSP</strong> = '.getDolGlobalString('WEBSITE_'.$obj->id.'_SECURITY_FORCECSP', '<span class="opacitymedium">'.$langs->trans("Undefined").'</span>');
print ' &nbsp; <span class="opacitymedium">('.$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 '")</span><br>';
print '<strong>WEBSITE_'.$obj->id.'_SECURITY_FORCERP</strong> = '.getDolGlobalString('WEBSITE_'.$obj->id.'_SECURITY_FORCERP', '<span class="opacitymedium">'.$langs->trans("Undefined").'</span>').' &nbsp; <span class="opacitymedium">('.$langs->trans("Recommended").': '.$langs->trans("Undefined").'="strict-origin-when-cross-origin" '.$langs->trans("or").' "same-origin"=more secured)</span><br>';
print '<strong>WEBSITE_'.$obj->id.'_SECURITY_FORCESTS</strong> = '.getDolGlobalString('WEBSITE_'.$obj->id.'_SECURITY_FORCESTS', '<span class="opacitymedium">'.$langs->trans("Undefined").'</span>').' &nbsp; <span class="opacitymedium">('.$langs->trans("Example").": \"max-age=31536000; includeSubDomains\")</span><br>";
print '<strong>WEBSITE_'.$obj->id.'_SECURITY_FORCEPP</strong> = '.getDolGlobalString('WEBSITE_'.$obj->id.'_SECURITY_FORCEPP', '<span class="opacitymedium">'.$langs->trans("Undefined").'</span>').' &nbsp; <span class="opacitymedium">('.$langs->trans("Example").": \"camera=(), microphone=(), geolocation=*\")</span><br>";
$i++;
if ($i != $num_rows) {
print '<br>';
}
}
}
} else {
dol_print_error($db);
}
print '</div>';
// Other - experimental
@ -845,7 +891,6 @@ print '<br>';
print '<strong>MAIN_SECURITY_FORCE_ACCESS_CONTROL_ALLOW_ORIGIN</strong> = '.getDolGlobalString('MAIN_SECURITY_FORCE_ACCESS_CONTROL_ALLOW_ORIGIN', '<span class="opacitymedium">'.$langs->trans("Undefined").'</span>').' &nbsp; <span class="opacitymedium">('.$langs->trans("Recommended").": 1)</span><br>";
print '<br>';
// For websites
print '<strong>WEBSITE_MAIN_SECURITY_FORCECSPRO</strong> = '.getDolGlobalString('WEBSITE_MAIN_SECURITY_FORCECSPRO', '<span class="opacitymedium">'.$langs->trans("Undefined").'</span>');
print ' &nbsp; <span class="opacitymedium">('.$langs->trans("Example").': "';

View file

@ -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

View file

@ -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

View file

@ -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 '<div class="div-table-responsive-no-min">';
print '<input class="minwidth500 quatrevingtpercent" name="WEBSITE_'.$object->id.'_SECURITY_FORCECSP" id="WEBSITE_'.$object->id.'_SECURITY_FORCECSP" value="'.$forceCSP.'"><br>';
print '<input class="minwidth500 quatrevingtpercent" name="WEBSITE_'.$object->id.'_SECURITY_FORCECSP" id="WEBSITE_'.$object->id.'_SECURITY_FORCECSP" value="'.$forceCSP.'"> <a href="#" id="btnaddcontentsecuritypolicy">'.img_picto('', 'add').'</a><br>';
print '<br>';
print '<div class="">';
print img_picto('', 'graph', 'class="pictofixedwidth"').$langs->trans("HierarchicView").'<br>';
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 '<div id="selectaddcontentsecuritypolicy" class="hidden">';
print $form->selectarray("select_identifier_WEBSITE_SECURITY_FORCECSP", $selectarrayCSPDirectives, "select_identifier_WEBSITE_SECURITY_FORCECSP", $langs->trans("FillCSPDirective"), 0, 0, '', 0, 0, 0, '', 'minwidth200 maxwidth350 inline-block');
print ' ';
print '<input type="hidden" id="select_source_WEBSITE_SECURITY_FORCECSP" name="select_source_WEBSITE_SECURITY_FORCECSP">';
foreach ($selectarrayCSPSources as $key => $values) {
print '<div class="div_WEBSITE_SECURITY_FORCECSP hidden inline-block maxwidth200" id="div_'.$key.'_WEBSITE_SECURITY_FORCECSP">';
print $form->selectarray("select_".$key."_WEBSITE_SECURITY_FORCECSP", $values, "select_".$key."_WEBSITE_SECURITY_FORCECSP", 1, 0, 0, '', 0, 0, 0, '', 'minwidth200 maxwidth300 inline-block select_WEBSITE_SECURITY_FORCECSP');
print '<div class="div_WEBSITE_SECURITY_FORCECSP hidden inline-block maxwidth350" id="div_'.$key.'_WEBSITE_SECURITY_FORCECSP">';
print $form->selectarray("select_".$key."_WEBSITE_SECURITY_FORCECSP", $values, "select_".$key."_WEBSITE_SECURITY_FORCECSP", $langs->trans("FillCSPSource"), 0, 0, '', 0, 0, 0, '', 'minwidth200 maxwidth300 inline-block select_WEBSITE_SECURITY_FORCECSP');
print '</div>';
}
print ' ';
print '<div class="div_input_data_WEBSITE_SECURITY_FORCECSP hidden inline-block maxwidth200"><input id="input_data_WEBSITE_SECURITY_FORCECSP" name="input_data_WEBSITE_SECURITY_FORCECSP"></div>';
print ' ';
print '<div class="div_btn_class_WEBSITE_SECURITY_FORCECSP hidden inline-block maxwidth200"><input type="submit" id="btn_WEBSITE_SECURITY_FORCECSP" name="btn_WEBSITE_SECURITY_FORCECSP" class="butAction small smallpaddingimp" value="'.$langs->trans("Add").'"></div>';
print '<div class="div_btn_class_WEBSITE_SECURITY_FORCECSP inline-block maxwidth200"><input type="submit" id="btn_WEBSITE_SECURITY_FORCECSP" name="btn_WEBSITE_SECURITY_FORCECSP" class="butAction small smallpaddingimp" value="'.$langs->trans("Add").'" disabled></div>';
print '</div>';
print '</div>';
// Content Security Policy list of selected rules
print '<div class="div-table-responsive-no-min">';
print '<ul>';
foreach ($forceCSPArr as $directive => $sources) {
print '<li><span>'.$directive.'</span>';
print '<li>';
if (in_array($directive, array_keys($selectarrayCSPDirectives))) {
print '<span>'.$directive.'</span>';
} else {
print $form->textwithpicto($directive, $langs->trans("UnknowContentSecurityPolicyDirective"), 1, 'warning');
}
if (!empty($sources)) {
print '<ul>';
foreach ($sources as $key => $source) {
@ -4477,7 +4482,7 @@ if ($action == 'editsecurity') {
print '<div class="center">';
print '<input type="submit" class="button small" name="addcontainer" value="'.$langs->trans("Save").'">';
print '<input type="submit" class="button small" name="updateandstay" value="'.$langs->trans("Save").'">';
print '<input class="button button-cancel small" type="submit" name="preview" value="'.$langs->trans("Cancel").'">';
print '</div>';
@ -4485,15 +4490,25 @@ if ($action == 'editsecurity') {
print '<script>
$(document).ready(function() {
$("#btnaddcontentsecuritypolicy").on("click", function(){
if($("#selectaddcontentsecuritypolicy").is(":visible")){
console.log("We hide select to add Content Security Policy");
$("#selectaddcontentsecuritypolicy").hide();
} else {
console.log("We show select to add Content Security Policy");
$("#selectaddcontentsecuritypolicy").show();
}
});
$("#select_identifier_WEBSITE_SECURITY_FORCECSP").on("change", function() {
key = $(this).find(":selected").data("directivetype");
console.log("We hide all select div");
$(".div_WEBSITE_SECURITY_FORCECSP").hide();
$(".select_WEBSITE_SECURITY_FORCECSP").val(null).trigger("change");
$(".div_input_data_WEBSITE_SECURITY_FORCECSP").hide();
$(".div_btn_class_WEBSITE_SECURITY_FORCECSP").hide();
$("#btn_WEBSITE_SECURITY_FORCECSP").prop("disabled",true);
if (key == "none"){
$(".div_btn_class_WEBSITE_SECURITY_FORCECSP").css("display", "inline-block");
$("#btn_WEBSITE_SECURITY_FORCECSP").prop("disabled",false);
} else {
console.log("We show div select with key "+key);
$("#div_"+key+"_WEBSITE_SECURITY_FORCECSP").css("display", "inline-block");
@ -4510,9 +4525,9 @@ if ($action == 'editsecurity') {
$("#input_data_WEBSITE_SECURITY_FORCECSP").val("");
$(".div_input_data_WEBSITE_SECURITY_FORCECSP").hide();
if (keysource != undefined) {
$(".div_btn_class_WEBSITE_SECURITY_FORCECSP").css("display", "inline-block");
$("#btn_WEBSITE_SECURITY_FORCECSP").prop("disabled",false);
} else {
$(".div_btn_class_WEBSITE_SECURITY_FORCECSP").hide();
$("#btn_WEBSITE_SECURITY_FORCECSP").prop("disabled",true);
}
}
});
@ -4520,10 +4535,10 @@ if ($action == 'editsecurity') {
$("#input_data_WEBSITE_SECURITY_FORCECSP").on("change", function(){
if ($(this).val() != undefined) {
console.log("We show add button");
$(".div_btn_class_WEBSITE_SECURITY_FORCECSP").css("display", "inline-block");
$("#btn_WEBSITE_SECURITY_FORCECSP").prop("disabled",false);
} else {
console.log("We hide add button");
$(".div_btn_class_WEBSITE_SECURITY_FORCECSP").hide();
$("#btn_WEBSITE_SECURITY_FORCECSP").prop("disabled",true);
}
});
});

View file

@ -221,13 +221,17 @@ function websiteGetContentPolicyToArray($forceCSP)
$sources = $securitypolicyarr;
$issourcedata = 0;
if (empty($sources)) {
$forceCSPArr[$directive] = array();
$forceCSPArr[$directive] = array();
} else {
//Loop on each sources to add to the right directive array key
foreach ($sources as $key2 => $source) {
$source = str_replace(":", "", $source);
$source = str_replace("'", "", $source);
if (empty($source)) {
$forceCSPArr[$directive] = array();
continue;
}
if ($source == "data") {
$issourcedata = 1;
if (empty($forceCSPArr[$directive])) {