Skip to content

Commit

Permalink
YQ-3566 fix sql injection in create binding request (ydb-platform#8275)
Browse files Browse the repository at this point in the history
  • Loading branch information
GrigoriyPA authored and uzhastik committed Sep 10, 2024
1 parent a9f3aaa commit bf6858e
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
6 changes: 6 additions & 0 deletions ydb/core/fq/libs/common/util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,18 +62,24 @@ class TIssueDatabaseRemover {
TString DatabasePath;
};

void EscapeBackslashes(TString& value) {
SubstGlobal(value, "\\", "\\\\");
}

}

TString EscapeString(const TString& value,
const TString& enclosingSeq,
const TString& replaceWith) {
auto escapedValue = value;
EscapeBackslashes(escapedValue);
SubstGlobal(escapedValue, enclosingSeq, replaceWith);
return escapedValue;
}

TString EscapeString(const TString& value, char enclosingChar) {
auto escapedValue = value;
EscapeBackslashes(escapedValue);
SubstGlobal(escapedValue,
TString{enclosingChar},
TStringBuilder{} << '\\' << enclosingChar);
Expand Down
4 changes: 4 additions & 0 deletions ydb/core/fq/libs/common/util_ut.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,19 @@ Y_UNIT_TEST_SUITE(EscapingBasics) {
UNIT_ASSERT_VALUES_EQUAL(EscapeString("some_secret1", '"'), "some_secret1");
UNIT_ASSERT_VALUES_EQUAL(EscapeString("some_secret1", "}+{", "[*]"), "some_secret1");
UNIT_ASSERT_VALUES_EQUAL(EscapeString("some\"_\"secret1", '"'), "some\\\"_\\\"secret1");
UNIT_ASSERT_VALUES_EQUAL(EscapeString("some\"_\\\"secret1", '"'), "some\\\"_\\\\\\\"secret1");
UNIT_ASSERT_VALUES_EQUAL(EscapeString("some}+{_}+{secret1", "}+{", "[*]"), "some[*]_[*]secret1");
UNIT_ASSERT_VALUES_EQUAL(EscapeString("some}+{\\}+{secret1", "}+{", "[*]"), "some[*]\\\\[*]secret1");
}

Y_UNIT_TEST(EncloseAndEscapeStringShouldWork) {
UNIT_ASSERT_VALUES_EQUAL(EncloseAndEscapeString("some_secret1", '"'), "\"some_secret1\"");
UNIT_ASSERT_VALUES_EQUAL(EncloseAndEscapeString("some_secret1\nsome_secret2", "}+{", "[*]"), "}+{some_secret1\nsome_secret2}+{");

UNIT_ASSERT_VALUES_EQUAL(EncloseAndEscapeString("some\"_\"secret1", '"'), "\"some\\\"_\\\"secret1\"");
UNIT_ASSERT_VALUES_EQUAL(EncloseAndEscapeString("some\"_\\\"secret1", '"'), "\"some\\\"_\\\\\\\"secret1\"");
UNIT_ASSERT_VALUES_EQUAL(EncloseAndEscapeString("some_secret1}+{\n}+{some_secret2", "}+{", "[*]"), "}+{some_secret1[*]\n[*]some_secret2}+{");
UNIT_ASSERT_VALUES_EQUAL(EncloseAndEscapeString("some_secret1}+{\\}+{some_secret2", "}+{", "[*]"), "}+{some_secret1[*]\\\\[*]some_secret2}+{");
}
}

Expand Down

0 comments on commit bf6858e

Please sign in to comment.