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/utils/safePush/safePush.c src/hg/utils/safePush/safePush.c
index e1cfbf6..f3d6360 100644
--- src/hg/utils/safePush/safePush.c
+++ src/hg/utils/safePush/safePush.c
@@ -66,31 +66,33 @@
    {"chrom", OPTION_BOOLEAN},
    {"prefix", OPTION_BOOLEAN},
    {"allTables", OPTION_BOOLEAN},
    {"allDatabases", OPTION_BOOLEAN},
    {"test", OPTION_BOOLEAN},
    {"list", OPTION_STRING},
    {"single", OPTION_STRING},
    {NULL, 0},
 };
 
 struct hash *getMysqlVars(char *host)
 /* Return hash full of MYSQL variables. */
 {
 struct hash *hash = hashNew(0);
 struct sqlConnection *conn = sqlConnectRemote(host, user, password, NULL);
-struct sqlResult *sr = sqlGetResult(conn, NOSQLINJ "show variables");
+char query[1024];
+sqlSafef(query, sizeof query, "show variables");
+struct sqlResult *sr = sqlGetResult(conn, query);
 char **row;
 while ((row = sqlNextRow(sr)) != NULL)
     hashAdd(hash, row[0], cloneString(row[1]));
 sqlFreeResult(&sr);
 sqlDisconnect(&conn);
 verbose(2, "Got %d mysqlVars from %s\n", hash->elCount, host);
 return hash;
 }
 
 boolean isAllGoodChars(char *s)
 /* Return TRUE if all characters are alphanumeric or _ */
 {
 char c;
 while ((c = *s++) != 0)
     if (!isalnum(c) && c != '_')
@@ -141,31 +143,33 @@
 void pushAllTables(char *sourceHost, char *destHost, char *database)
 /* Do the big rsync on database from sourceHost to destHost */
 {
 char command[2048];
 safef(command, sizeof(command),
 	"rsync -avr --progress --rsh=rsh %s/%s %s:%s/",
 	mysqlDataDir, database, destHost, mysqlDataDir);
 execAndCheck(command);
 }
 
 struct hash *tableTimeHash(char *host, boolean is5, char *database)
 /* Get hash of tables and the time they were last updated. */
 {
 struct hash *hash = hashNew(0);
 struct sqlConnection *conn = sqlConnectRemote(host, user, password, database);
-struct sqlResult *sr = sqlGetResult(conn, NOSQLINJ "show table status");
+char query[1024];
+sqlSafef(query, sizeof query, "show table status");
+struct sqlResult *sr = sqlGetResult(conn, query);
 char **row;
 while ((row = sqlNextRow(sr)) != NULL)
     {
     char *table = row[0];
     char *time = (is5 ? row[12] : row[11]);
     verbose(3, "%s:%s.%s %s\n", host, database, table, time);
     hashAdd(hash, table, cloneString(time));
     }
 sqlDisconnect(&conn);
 return hash;
 }
 
 void pushTable(char *sourceHost, struct hash *sourceTimes,
 	       char *destHost, struct hash *destTimes,
 	       char *database, char *table, boolean missingTableOk)
@@ -274,31 +278,33 @@
 boolean destIs5 = is5(destVars);
 
 if (sourceIs5 || destIs5)
     errAbort("Sorry, can't yet handle mySQL 5");
 if (sourceIs5 && !destIs5)
     errAbort("%s is running Mysql pre-5, and %s is running MySQL post-5, sorry",
     	destHost, sourceHost);
 forceAllGoodChars(sourceHost);
 forceAllGoodChars(destHost);
 forceAllGoodChars(database);
 forceAllGoodChars(table);
 if (allDatabases)
     {
     struct sqlConnection *conn = 
     	sqlConnectRemote(sourceHost, user, password, NULL);
-    struct sqlResult *sr = sqlGetResult(conn, NOSQLINJ "show databases");
+    char query[1024];
+    sqlSafef(query, sizeof query, "show databases");
+    struct sqlResult *sr = sqlGetResult(conn, query);
     char **row;
     while ((row = sqlNextRow(sr)) != NULL)
         {
 	char *db = row[0];
 	if (!sameWord(db, "mysql"))
 	    pushDatabase(sourceHost, sourceIs5, destHost, destIs5, db,
 	    	table, FALSE);
 	}
     sqlDisconnect(&conn);
     }
 else
     pushDatabase(sourceHost, sourceIs5, destHost, destIs5, database, table,
     	TRUE);
 flushTables(destHost);
 }