Commit 78f5ac34 authored by andryyy's avatar andryyy

[Web] Validate Regex in browser

parent 02bb3a44
<?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']));
}
?>
......@@ -5,6 +5,7 @@ jQuery(function($){
var entityMap={"&":"&amp;","<":"&lt;",">":"&gt;",'"':"&quot;","'":"&#39;","/":"&#x2F;","`":"&#x60;","=":"&#x3D;"};
function jq(myid) {return "#" + myid.replace( /(:|\.|\[|\]|,|=|@)/g, "\\$1" );}
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 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}
......@@ -33,21 +34,27 @@ jQuery(function($){
$(".validate_rspamd_regex").click(function( event ) {
event.preventDefault();
var regex_map_id = $(this).data('regex-map');
var regex_data = $(jq(regex_map_id)).val();
$.ajax({
dataType: 'json',
url: "/inc/ajax/regex_validation.php",
type: "get",
data: { regex: regex_data },
complete: function(data) {
var response = (data.responseText);
response_obj = JSON.parse(response);
if (response_obj.type == "success") {
$('button[data-id="' + regex_map_id + '"]').attr({"disabled": false});
}
mailcow_alert_box(response_obj.msg, response_obj.type);
},
});
var regex_data = $(jq(regex_map_id)).val().split(/\r?\n/);
var regex_valid = true;
for(var i = 0;i < regex_data.length;i++){
if(regex_data[i].startsWith('#') || !regex_data[i]){
continue;
}
if(!validateRegex(regex_data[i])) {
mailcow_alert_box('Cannot build regex from line ' + (i+1), 'danger');
var regex_valid = false;
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});
}
});
$('.textarea-code').on('keyup', function() {
$('.submit_rspamd_regex').attr({"disabled": true});
......
......@@ -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.
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>
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>.';
......@@ -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'] = '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>
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.';
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment