[Web] Validate Regex in browser

This commit is contained in:
andryyy 2019-10-29 21:56:35 +01:00
parent 02bb3a44a7
commit 78f5ac34dd
No known key found for this signature in database
GPG Key ID: 8EC34FF2794E25EF
4 changed files with 26 additions and 42 deletions

View File

@ -1,25 +0,0 @@
<?php
session_start();
require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/prerequisites.inc.php';
header('Content-Type: application/json');
if (!isset($_SESSION['mailcow_cc_role'])) {
exit();
}
if (isset($_GET['regex'])) {
$regex_lines = preg_split("/(\r\n|\n|\r)/", $_GET['regex']);
foreach ($regex_lines as $line => $regex) {
if (empty($regex) || substr($regex, 0, 1) == "#") {
continue;
}
if (empty($regex) || substr($regex, 0, 1) != "/") {
echo json_encode(array('type' => 'danger', 'msg' => 'Line ' . ($line + 1) . ': Invalid regex'));
exit();
}
if (@preg_match($regex, 'Lorem Ipsum') === false) {
echo json_encode(array('type' => 'danger', 'msg' => 'Line ' . ($line + 1) . ': Invalid regex "' . $regex . '"'));
exit();
}
}
echo json_encode(array('type' => 'success', 'msg' => $lang['add']['validation_success']));
}
?>

View File

@ -5,6 +5,7 @@ jQuery(function($){
var entityMap={"&":"&amp;","<":"&lt;",">":"&gt;",'"':"&quot;","'":"&#39;","/":"&#x2F;","`":"&#x60;","=":"&#x3D;"}; var entityMap={"&":"&amp;","<":"&lt;",">":"&gt;",'"':"&quot;","'":"&#39;","/":"&#x2F;","`":"&#x60;","=":"&#x3D;"};
function jq(myid) {return "#" + myid.replace( /(:|\.|\[|\]|,|=|@)/g, "\\$1" );} function jq(myid) {return "#" + myid.replace( /(:|\.|\[|\]|,|=|@)/g, "\\$1" );}
function escapeHtml(n){return String(n).replace(/[&<>"'`=\/]/g,function(n){return entityMap[n]})} function escapeHtml(n){return String(n).replace(/[&<>"'`=\/]/g,function(n){return entityMap[n]})}
function validateRegex(e){var t=e.split("/"),n=e,r="";t.length>1&&(n=t[1],r=t[2]);try{return new RegExp(n,r),!0}catch(e){return!1}}
function humanFileSize(i){if(Math.abs(i)<1024)return i+" B";var B=["KiB","MiB","GiB","TiB","PiB","EiB","ZiB","YiB"],e=-1;do{i/=1024,++e}while(Math.abs(i)>=1024&&e<B.length-1);return i.toFixed(1)+" "+B[e]} function humanFileSize(i){if(Math.abs(i)<1024)return i+" B";var B=["KiB","MiB","GiB","TiB","PiB","EiB","ZiB","YiB"],e=-1;do{i/=1024,++e}while(Math.abs(i)>=1024&&e<B.length-1);return i.toFixed(1)+" "+B[e]}
function hashCode(t){for(var n=0,r=0;r<t.length;r++)n=t.charCodeAt(r)+((n<<5)-n);return n} function hashCode(t){for(var n=0,r=0;r<t.length;r++)n=t.charCodeAt(r)+((n<<5)-n);return n}
function intToRGB(t){var n=(16777215&t).toString(16).toUpperCase();return"00000".substring(0,6-n.length)+n} function intToRGB(t){var n=(16777215&t).toString(16).toUpperCase();return"00000".substring(0,6-n.length)+n}
@ -33,21 +34,27 @@ jQuery(function($){
$(".validate_rspamd_regex").click(function( event ) { $(".validate_rspamd_regex").click(function( event ) {
event.preventDefault(); event.preventDefault();
var regex_map_id = $(this).data('regex-map'); var regex_map_id = $(this).data('regex-map');
var regex_data = $(jq(regex_map_id)).val(); var regex_data = $(jq(regex_map_id)).val().split(/\r?\n/);
$.ajax({ var regex_valid = true;
dataType: 'json', for(var i = 0;i < regex_data.length;i++){
url: "/inc/ajax/regex_validation.php", if(regex_data[i].startsWith('#') || !regex_data[i]){
type: "get", continue;
data: { regex: regex_data }, }
complete: function(data) { if(!validateRegex(regex_data[i])) {
var response = (data.responseText); mailcow_alert_box('Cannot build regex from line ' + (i+1), 'danger');
response_obj = JSON.parse(response); var regex_valid = false;
if (response_obj.type == "success") { break;
}
if(!regex_data[i].startsWith('/') || !/\/[ims]?$/.test(regex_data[i])){
mailcow_alert_box('Line ' + (i+1) + ' is invalid', 'danger');
var regex_valid = false;
break;
}
}
if (regex_valid) {
mailcow_alert_box('Regex OK', 'success');
$('button[data-id="' + regex_map_id + '"]').attr({"disabled": false}); $('button[data-id="' + regex_map_id + '"]').attr({"disabled": false});
} }
mailcow_alert_box(response_obj.msg, response_obj.type);
},
});
}); });
$('.textarea-code').on('keyup', function() { $('.textarea-code').on('keyup', function() {
$('.submit_rspamd_regex').attr({"disabled": true}); $('.submit_rspamd_regex').attr({"disabled": true});

View File

@ -922,4 +922,5 @@ $lang['admin']['rspamd_global_filters'] = 'Globale Filter-Maps';
$lang['admin']['rspamd_global_filters_info'] = 'Globale Filter-Maps steuern globales White- und Blacklisting dieses Servers. Die akzeptierte Form für Einträge sind <b>ausschließlich</b> Regular Expressions. $lang['admin']['rspamd_global_filters_info'] = 'Globale Filter-Maps steuern globales White- und Blacklisting dieses Servers. Die akzeptierte Form für Einträge sind <b>ausschließlich</b> Regular Expressions.
Trotz rudimentärer Überprüfung der Map, kann es zu fehlerhaften Einträgen kommen, die Rspamd im schlechtesten Fall mit unvorhersehbarer Funktionalität bestraft.<br> Trotz rudimentärer Überprüfung der Map, kann es zu fehlerhaften Einträgen kommen, die Rspamd im schlechtesten Fall mit unvorhersehbarer Funktionalität bestraft.<br>
Das korrekte Format lautet "/pattern/options" (Beispiel: <code>/.+@domain\.tld/i</code>).<br> Das korrekte Format lautet "/pattern/options" (Beispiel: <code>/.+@domain\.tld/i</code>).<br>
Der Name der Map beschreibt die jeweilige Funktion.'; Der Name der Map beschreibt die jeweilige Funktion.<br>
Rspamd versucht die Maps umgehend aufzulösen. Bei Problemen sollte <a href="" data-toggle="modal" data-container="rspamd-mailcow" data-target="#RestartContainer">Rspamd manuell neugestartet werden</a>.';

View File

@ -945,5 +945,6 @@ $lang['danger']['extra_acl_invalid_domain'] = 'External sender "%s" uses an inva
$lang['admin']['rspamd_global_filters_agree'] = "I will be careful!"; $lang['admin']['rspamd_global_filters_agree'] = "I will be careful!";
$lang['admin']['rspamd_global_filters'] = 'Global filter maps'; $lang['admin']['rspamd_global_filters'] = 'Global filter maps';
$lang['admin']['rspamd_global_filters_info'] = 'Global filter maps contain different kind of global black and whitelists. Their names explain their purpose. All content must contain valid regular expression in the format of "/pattern/options" (e.g. <code>/.+@domain\.tld/i</code>).<br> $lang['admin']['rspamd_global_filters_info'] = 'Global filter maps contain different kind of global black and whitelists. Their names explain their purpose. All content must contain valid regular expression in the format of "/pattern/options" (e.g. <code>/.+@domain\.tld/i</code>).<br>
Although rudimentary checks are being executed on each line of regex, Rspamds functionality can be broken, if it fails to read the syntax correctly.'; Although rudimentary checks are being executed on each line of regex, Rspamds functionality can be broken, if it fails to read the syntax correctly.<br>
Rspamd will try to read the map content when changed. If you experience problems, <a href="" data-toggle="modal" data-container="rspamd-mailcow" data-target="#RestartContainer">restart Rspamd</a> to enforce a map reload.';