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/tablesTables.c src/hg/lib/tablesTables.c
index 4965c90..5bca118 100644
--- src/hg/lib/tablesTables.c
+++ src/hg/lib/tablesTables.c
@@ -809,34 +809,37 @@
     slCount(table->rowList), 
     0, NULL, NULL, NULL, NULL, NULL, FALSE);
 }
 
 
 void webTableBuildQuery(struct cart *cart, char *from, char *initialWhere, 
     char *varPrefix, char *fields, boolean withFilters, 
     struct dyString **retQuery, struct dyString **retWhere)
 /* Construct select, from and where clauses in query, keeping an additional copy of where 
  * Returns the SQL query and the SQL where expression as two dyStrings (need to be freed)  */
 {
 struct dyString *query = dyStringNew(0);
 struct dyString *where = dyStringNew(0);
 struct slName *field, *fieldList = commaSepToSlNames(fields);
 boolean gotWhere = FALSE;
-sqlDyStringPrintf(query, "select %-s from %-s", sqlCkIl(fields), sqlCkIl(from));
+sqlCkIl(fieldsSafe,fields)
+sqlCkIl(fromSafe,from)
+
+sqlDyStringPrintf(query, "select %-s from %-s", fieldsSafe, fromSafe);
 if (!isEmpty(initialWhere))
     {
-    sqlDyStringPrintfFrag(where, " where ");
+    sqlDyStringPrintf(where, " where ");
     sqlSanityCheckWhere(initialWhere, where);
     gotWhere = TRUE;
     }
 
 /* If we're doing filters, have to loop through the row of filter controls */
 if (withFilters)
     {
     for (field = fieldList; field != NULL; field = field->next)
         {
 	char varName[128];
 	safef(varName, sizeof(varName), "%s_f_%s", varPrefix, field->name);
 	char *val = trimSpaces(cartUsualString(cart, varName, ""));
 	if (!isEmpty(val))
 	    {
 	    if (gotWhere)
@@ -984,31 +987,32 @@
 int page = 0;
 struct fieldedTableSegment context;
 page = cartUsualInt(cart, pageVar, 0) - 1;
 if (page < 0)
     page = 0;
 context.tableOffset = page * pageSize;
 
 if (visibleFacetList)
     {
     table = fieldedTableAndCountsFromDbQuery(conn, query->string, pageSize, 
 	context.tableOffset, selectedFacetValues, &ffArray, &context.tableSize);
     }
 else
     {
     /* Figure out size of query result */
-    struct dyString *countQuery = sqlDyStringCreate("select count(*) from %-s", sqlCkIl(from));
+    sqlCkIl(fromSafe,from)
+    struct dyString *countQuery = sqlDyStringCreate("select count(*) from %-s", fromSafe);
     sqlDyStringPrintf(countQuery, "%-s", where->string);   // trust
     context.tableSize = sqlQuickNum(conn, countQuery->string);
     dyStringFree(&countQuery);
     }
 
 if (context.tableSize > pageSize)
     {
     int lastPage = (context.tableSize-1)/pageSize;
     if (page > lastPage)
         page = lastPage;
     context.tableOffset = page * pageSize;
     }
 
 if (!visibleFacetList)
     {