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/hgTracks/cgapSageTrack.c src/hg/hgTracks/cgapSageTrack.c
index 6172583..af90c1b 100644
--- src/hg/hgTracks/cgapSageTrack.c
+++ src/hg/hgTracks/cgapSageTrack.c
@@ -13,32 +13,33 @@
 #include "cgapSage/cgapSage.h"
 #include "cgapSage/cgapSageLib.h"
 
 static int grayIxForCgap(double tpm)
 /* Return a grayIx based on the score. */
 {
 int val = (int)ceil(tpm);
 return grayInRange(val, 0, 150);
 }
 
 static struct hash *libTissueHash(struct sqlConnection *conn)
 /* Read two columns of a table and hash em up. */
 {
 struct hash *ret = newHash(9);
 struct sqlResult *sr = NULL;
-char query[49] = NOSQLINJ "select libId,tissue from cgapSageLib";
+char query[49];
 char **row;
+sqlSafef(query, sizeof query, "select libId,tissue from cgapSageLib");
 sr = sqlGetResult(conn, query);
 while ((row = sqlNextRow(sr)) != NULL)
     hashAdd(ret, row[0], cloneString(row[1]));
 sqlFreeResult(&sr);
 return ret;
 }
 
 struct cgapSageTpmHashEl 
 /* A convenience struct for computing means. */
     {
     double total;
     long freqTotal;
     long libTotals;
     int count;
     };
@@ -230,31 +231,33 @@
 	    lf->grayIx = grayIxForCgap(tag->tagTpms[i]);
 	    lf->extra = cloneString(link);
 	    addSimpleFeature(lf);	
 	    slAddHead(&libList, lf);
 	    }
 	}
     }
 slSort(&libList, cgapLinkedFeaturesCmp);
 slReverse(&libList);
 return libList;
 }
 
 struct hash *getTotTagsHashFromTable(struct sqlConnection *conn)
 /* Load the cgapSageLib table for the db then call getTotTagsHash. */
 {
-struct cgapSageLib *libs = cgapSageLibLoadByQuery(conn, NOSQLINJ "select * from cgapSageLib");
+char query[1024];
+sqlSafef(query, sizeof query, "select * from cgapSageLib");
+struct cgapSageLib *libs = cgapSageLibLoadByQuery(conn, query);
 struct hash *libTotHash = getTotTagsHash(libs);
 cgapSageLibFreeList(&libs);
 return libTotHash;
 }
 
 void cgapSageLoadItems(struct track *tg)
 /* This function loads the beds in the current window into a linkedFeatures list. */
 /* Each bed entry may turn into multiple linkedFeatures because one is made for */
 /* each library at a given tag (bed). */
 {
 struct linkedFeatures *itemList = NULL;
 struct sqlConnection *conn = hAllocConn(database);
 struct hash *libHash = libTissueHash(conn);
 struct hash *libTotHash = getTotTagsHashFromTable(conn);
 struct sqlResult *sr = NULL;