44ccfacbe3a3d4b300f80d48651c77837a4b571e
galt
  Tue Apr 26 11:12:02 2022 -0700
SQL INJECTION Prevention Version 2 - this improves our methods by making subclauses of SQL that get passed around be both easy and correct to use. The way that was achieved was by getting rid of the obscure and not well used functions sqlSafefFrag and sqlDyStringPrintfFrag and replacing them with the plain versions of those functions, since these are not needed anymore. The new version checks for NOSQLINJ in unquoted %-s which is used to include SQL clauses, and will give an error the NOSQLINJ clause is not present, and this will automatically require the correct behavior by developers. sqlDyStringPrint is a very useful function, however because it was not enforced, users could use various other dyString functions and they operated without any awareness or checking for SQL correct use. Now those dyString functions are prohibited and it will produce an error if you try to use a dyString function on a SQL string, which is simply detected by the presence of the NOSQLINJ prefix.

diff --git src/hg/lib/hubSearchText.c src/hg/lib/hubSearchText.c
index aab9395..8b5955d 100644
--- src/hg/lib/hubSearchText.c
+++ src/hg/lib/hubSearchText.c
@@ -297,44 +297,45 @@
         char *hubSearchTerms, bool checkLongText, char *dbFilter, struct hash *hubLookup,
         struct hash **searchResultHashRet, struct slName **hubsToPrintRet, char *extra)
 /* Find hubs, genomes, and tracks that match the provided search terms.
  * Return all hits that satisfy the (optional) supplied assembly filter.
  * if checkLongText is FALSE, skip searching within the long description text entries
  * the string 'extra' is used for additional mysql filter(s) by the caller and
  * must be escaped by them */
 {
 char *cleanSearchTerms = cloneString(hubSearchTerms);
 if (isNotEmpty(cleanSearchTerms))
     tolowers(cleanSearchTerms);
 bool isStrictSearch = FALSE;
 char *modifiedSearchTerms = modifyTermsForHubSearch(cleanSearchTerms, isStrictSearch);
 struct hubSearchText *hubSearchResultsList = NULL;
 struct dyString *query = dyStringNew(100);
-char *noLongText = NULL;
-
-if (!checkLongText)
-    noLongText = cloneString("textLength = 'Short'");
-else
-    noLongText = cloneString("");
 
 sqlDyStringPrintf(query, "select * from %s where ", hubSearchTableName);
-// caller has manually escaped extra, so safe to dyStringPrintf:
 if (isNotEmpty(extra))
-    dyStringPrintf(query, "%s %s", extra,
-        (isNotEmpty(noLongText) || isNotEmpty(modifiedSearchTerms)) ? "and ": "");
-if (isNotEmpty(noLongText))
-    dyStringPrintf(query, "%s %s", noLongText, isNotEmpty(modifiedSearchTerms) ? "and " : "");
+    {
+    sqlDyStringPrintf(query, "%-s ", extra);
+    if (!checkLongText || isNotEmpty(modifiedSearchTerms))
+        sqlDyStringPrintf(query, "and ");
+    }
+if (!checkLongText)
+    {
+    sqlDyStringPrintf(query, "textLength = 'Short'"); 
+    if (isNotEmpty(modifiedSearchTerms))
+       sqlDyStringPrintf(query, "and ");
+    }
+
 if (isNotEmpty(modifiedSearchTerms))
     {
     sqlDyStringPrintf(query, "match(text) against ('%s' in boolean mode)"
         " order by match(text) against ('%s' in boolean mode)",
         modifiedSearchTerms, modifiedSearchTerms);
     }
 struct sqlResult *sr = sqlGetResult(conn, dyStringContents(query));
 char **row;
 while ((row = sqlNextRow(sr)) != NULL)
     {
     struct hubSearchText *hst = hubSearchTextLoadWithNullGiveContext(row, cleanSearchTerms);
     // Skip rows where the long text matched the more lax MySQL search (punctuation just
     // splits terms into two words, so "rna-seq" finds "rna" and "seq" separately, but
     // not the more strict rules used to find context for the search terms.
     if ((hst->textLength == hubSearchTextLong) && isEmpty(hst->text))