From 3a4c5a9af66b51aa4906b5b51c98c4c6b51e3990 Mon Sep 17 00:00:00 2001 From: Nikita Akilov Date: Mon, 2 Nov 2020 19:37:39 +0300 Subject: [PATCH] dbeaver/dbeaver-ee#521 add escaping to cron entry params --- .../src/org/jkiss/utils/CommonUtils.java | 11 ++++++++ .../src/org/jkiss/utils/CommonUtilsTest.java | 25 +++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/bundles/org.jkiss.utils/src/org/jkiss/utils/CommonUtils.java b/bundles/org.jkiss.utils/src/org/jkiss/utils/CommonUtils.java index d3bc49ae05..e2e5cb0997 100644 --- a/bundles/org.jkiss.utils/src/org/jkiss/utils/CommonUtils.java +++ b/bundles/org.jkiss.utils/src/org/jkiss/utils/CommonUtils.java @@ -801,4 +801,15 @@ public class CommonUtils { cal1.get(Calendar.YEAR) == cal2.get(Calendar.YEAR) && cal1.get(Calendar.DAY_OF_YEAR) == cal2.get(Calendar.DAY_OF_YEAR)); } + + public static String escapeBourneShellString(@NotNull String s) { + return "'" + s.replace("'", "'\\''") + "'"; + } + + public static String unescapeBourneShellString(@NotNull String s) { + if (!s.startsWith("'") || !s.endsWith("'") || s.length() < 2) { //not an escaped bourne shell string + return s; + } + return s.substring(1, s.length() - 1).replace("'\\''", "'"); + } } diff --git a/test/org.jkiss.dbeaver.test.platform/src/org/jkiss/utils/CommonUtilsTest.java b/test/org.jkiss.dbeaver.test.platform/src/org/jkiss/utils/CommonUtilsTest.java index 35a0063770..00931a5f58 100644 --- a/test/org.jkiss.dbeaver.test.platform/src/org/jkiss/utils/CommonUtilsTest.java +++ b/test/org.jkiss.dbeaver.test.platform/src/org/jkiss/utils/CommonUtilsTest.java @@ -522,4 +522,29 @@ public class CommonUtilsTest { public void testgetSingleLineString() { Assert.assertEquals("a¶bc d ", CommonUtils.getSingleLineString("a\nb\rc\td\0")); } + + @Test + public void testEscapeStringForBourneShell() { + Assert.assertEquals("''", CommonUtils.escapeBourneShellString("")); + Assert.assertEquals("'string'", CommonUtils.escapeBourneShellString("string")); + Assert.assertEquals("'string with '\\''one single quote symbol'", CommonUtils.escapeBourneShellString("string with 'one single quote symbol")); + Assert.assertEquals("'string with '\\''two '\\''single quote symbols'", CommonUtils.escapeBourneShellString("string with 'two 'single quote symbols")); + Assert.assertEquals("'string with '\\''three '\\''single '\\''quote symbols'", CommonUtils.escapeBourneShellString("string with 'three 'single 'quote symbols")); + Assert.assertEquals("''\\'''", CommonUtils.escapeBourneShellString("'")); + Assert.assertEquals("'unit'\\'''\\''test'", CommonUtils.escapeBourneShellString("unit''test")); + Assert.assertEquals("'unit'\\'''\\'''\\''test'", CommonUtils.escapeBourneShellString("unit'''test")); + } + + @Test + public void testUnescapeStringForBourneShell() { + Assert.assertEquals("", CommonUtils.unescapeBourneShellString("''")); + Assert.assertEquals("string", CommonUtils.unescapeBourneShellString("'string'")); + Assert.assertEquals("string with 'one single quote symbol", CommonUtils.unescapeBourneShellString("'string with '\\''one single quote symbol'")); + Assert.assertEquals("string with 'two 'single quote symbols", CommonUtils.unescapeBourneShellString("'string with '\\''two '\\''single quote symbols'")); + Assert.assertEquals("string with 'three 'single 'quote symbols", CommonUtils.unescapeBourneShellString("'string with '\\''three '\\''single '\\''quote symbols'")); + Assert.assertEquals("'", CommonUtils.unescapeBourneShellString("''\\'''")); + Assert.assertEquals("unit''test", CommonUtils.unescapeBourneShellString("'unit'\\'''\\''test'")); + Assert.assertEquals("unit'''test", CommonUtils.unescapeBourneShellString("'unit'\\'''\\'''\\''test'")); + Assert.assertEquals("'''unit'''test'''", CommonUtils.unescapeBourneShellString("''\\'''\\'''\\''unit'\\'''\\'''\\''test'\\'''\\'''\\'''")); + } } -- GitLab