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/hgTablesTest/hgTablesTest.c src/hg/hgTablesTest/hgTablesTest.c
index 9580928..476eca0 100644
--- src/hg/hgTablesTest/hgTablesTest.c
+++ src/hg/hgTablesTest/hgTablesTest.c
@@ -209,31 +209,33 @@
 }
 
 int tableSize(char *db, char *table)
 /* Return number of rows in table. */
 {
 struct sqlConnection *conn = sqlConnect(db);
 int size = sqlTableSize(conn, table);
 sqlDisconnect(&conn);
 return size;
 }
 
 void showConnectInfo(char *db)
 /* Show connection info used by this program. */
 {
 struct sqlConnection *conn = sqlConnect(db);
-char *user = sqlQuickString(conn, NOSQLINJ "select current_user()");
+char query[1024];
+sqlSafef(query, sizeof query, "select current_user()");
+char *user = sqlQuickString(conn, query);
 char *hostinfo = sqlHostInfo(conn);
       verbose(1, "Connecting as %s to database server %s\n", user, hostinfo);
 fprintf(logFile, "Connecting as %s to database server %s\n", user, hostinfo); fflush(logFile);
 sqlDisconnect(&conn);
 }
 
 void showRunningHostName()
 /* Show hostname of the machine we are running on. */
 {
 char hostname[HOST_NAME_MAX];
 if (gethostname(hostname, sizeof hostname))
     {
     perror("gethostname");
     safecpy(hostname, sizeof hostname, "error-reading-hostname");
     }
@@ -871,31 +873,33 @@
     if (!sameString("allTables", group->name))
 	{
 	if (clGroup && !sameString(clGroup, group->name))
 	    continue;
 	testOneGroup(dbPage, org, db, group->name, clTracks);
 	++groupIx;
 	}
     }
 }
 
 void getTestRegion(char *db, char region[256], int regionSize)
 /* Look up first chromosome in database and grab five million bases
  * from the middle of it. */
 {
 struct sqlConnection *conn = sqlConnect(db);
-struct sqlResult *sr = sqlGetResult(conn, NOSQLINJ "select * from chromInfo limit 1");
+char query[1024];
+sqlSafef(query, sizeof query, "select * from chromInfo limit 1");
+struct sqlResult *sr = sqlGetResult(conn, query);
 char **row;
 struct chromInfo ci;
 int start,end,middle;
 
 if ((row = sqlNextRow(sr)) == NULL)
     errAbort("Couldn't get one row from chromInfo");
 chromInfoStaticLoad(row, &ci);
 middle = ci.size/2;
 start = middle-2500000;
 end = middle+2500000;
 if (start < 0) start = 0;
 if (end > ci.size) end = ci.size;
 safef(region, regionSize, "%s:%d-%d", ci.chrom, start+1, end);
 verbose(1, "Testing %s at position %s\n", db, region);
 fprintf(logFile, "Testing %s at position %s\n", db, region);