From c48ad50f24458b28f4fc2f7d2845c519b541673a Mon Sep 17 00:00:00 2001 From: Christian Hesse Date: Fri, 26 Feb 2021 14:44:54 +0100 Subject: global-functions: introduce and use $ValidateSyntax --- global-functions | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) (limited to 'global-functions') diff --git a/global-functions b/global-functions index 8396f60..aba1cff 100644 --- a/global-functions +++ b/global-functions @@ -47,6 +47,7 @@ :global SymbolForNotification; :global TimeIsSync; :global UrlEncode; +:global ValidateSyntax; :global VersionToNum; :global WaitDefaultRouteReachable; :global WaitDNSResolving; @@ -615,6 +616,7 @@ :global ScriptInstallUpdate; :global SendNotification; :global SymbolForNotification; + :global ValidateSyntax; :if ([ $CertificateAvailable "R3" ] = false) do={ $LogPrintExit2 warning $0 ("Downloading certificate failed, trying without.") false; @@ -670,13 +672,7 @@ :if ([ :len $SourceNew ] > 0) do={ :if ($SourceNew != $ScriptVal->"source") do={ :if ([ :pick $SourceNew 0 18 ] = "#!rsc by RouterOS\n") do={ - :local SyntaxOK true; - :do { - [ :parse (":local ValidateFunction do={ " . $SourceNew . " }") ] - } on-error={ - :set SyntaxOK false; - } - :if ($SyntaxOK = true) do={ + :if ([ $ValidateSyntax $SourceNew ] = true) do={ :local DontRequirePermissions \ ($SourceNew~"\n# requires: dont-require-permissions=yes\n"); $LogPrintExit2 info $0 ("Updating script: " . $ScriptVal->"name") false; @@ -1030,6 +1026,18 @@ :return $Return; } +# basic syntax validation +:set ValidateSyntax do={ + :local Code [ :tostr $1 ]; + + :do { + [ :parse (":local Validate do={ " . $Code . " }") ]; + } on-error={ + :return false; + } + :return true; +} + # convert version string to numeric value :set VersionToNum do={ :local Input [ :tostr $1 ]; -- cgit v1.2.3-70-g09d2