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/visiGene/vgGetText/vgGetText.c src/hg/visiGene/vgGetText/vgGetText.c
index 0b7184e..b80763a 100644
--- src/hg/visiGene/vgGetText/vgGetText.c
+++ src/hg/visiGene/vgGetText/vgGetText.c
@@ -73,93 +73,100 @@
 }
 
 void *hashMustFindValFromInt(struct hash *hash, int key)
 /* Return hash value using an integer key */
 {
 char buf[32];
 safef(buf, sizeof(buf), "%d", key);
 return hashMustFindVal(hash, buf);
 }
 
 void hashComplexTables(struct sqlConnection *conn)
 /* Make hashes for more complicated tables. */
 {
 struct sqlResult *sr;
 char **row;
+char query[1024];
 
 probeHash = hashSizedForTable(conn, "probe");
-sr = sqlGetResult(conn, NOSQLINJ "select * from probe");
+sqlSafef(query, sizeof query, "select * from probe");
+sr = sqlGetResult(conn, query);
 while ((row = sqlNextRow(sr)) != NULL)
      hashAdd(probeHash, row[0], probeLoad(row));
 sqlFreeResult(&sr);
 
 geneHash = hashSizedForTable(conn, "gene");
-sr = sqlGetResult(conn, NOSQLINJ "select * from gene");
+sqlSafef(query, sizeof query, "select * from gene");
+sr = sqlGetResult(conn, query);
 while ((row = sqlNextRow(sr)) != NULL)
      hashAdd(geneHash, row[0], geneLoad(row));
 sqlFreeResult(&sr);
 
 imageProbeHash = hashSizedForTable(conn, "imageProbe");
-sr = sqlGetResult(conn, NOSQLINJ "select * from imageProbe");
+sqlSafef(query, sizeof query, "select * from imageProbe");
+sr = sqlGetResult(conn, query);
 while ((row = sqlNextRow(sr)) != NULL)
      hashAdd(imageProbeHash, row[1], imageProbeLoad(row)); /* Key on image, not id. */
 sqlFreeResult(&sr);
 }
 
 void makeKnownGeneHashes(int knownDbCount, char **knownDbs)
 /* Create hashes containing info on known genes. */
 {
+char query[1024];
 int i;
 knownTextHash = hashNew(18);
 uniProtToKnown = hashNew(18);
 refSeqToKnown = hashNew(18);
 aliasToKnown = hashNew(19);
 nameToKnown = hashNew(18);
 
 for (i=0; i<knownDbCount; i += 1)
     {
     char *gdb = knownDbs[i];
     char *knownDatabase = hdbDefaultKnownDb(gdb);
     struct sqlConnection *conn = sqlConnect(knownDatabase);
     struct sqlResult *sr;
     char **row;
 
-
-    sr = sqlGetResult(conn, NOSQLINJ "select kgID,geneSymbol,spID,spDisplayID,refseq,description from kgXref");
+    sqlSafef(query, sizeof query, "select kgID,geneSymbol,spID,spDisplayID,refseq,description from kgXref");
+    sr = sqlGetResult(conn, query);
     while ((row = sqlNextRow(sr)) != NULL)
         {
 	char *kgID = cloneString(row[0]);
 	touppers(kgID);
 	touppers(row[1]);
 	hashAdd(nameToKnown, row[1], kgID);
 	hashAdd(uniProtToKnown, row[2], kgID);
 	hashAdd(uniProtToKnown, row[3], kgID);
 	hashAdd(refSeqToKnown, row[4], kgID);
 	hashAdd(knownTextHash, kgID, cloneString(row[5]));
 	}
     sqlFreeResult(&sr);
 
-    sr = sqlGetResult(conn, NOSQLINJ "select kgID,alias from kgAlias");
+    sqlSafef(query, sizeof query, "select kgID,alias from kgAlias");
+    sr = sqlGetResult(conn, query);
     while ((row = sqlNextRow(sr)) != NULL)
 	{
 	char *upc = cloneString(row[0]);
 	touppers(upc);
         hashAdd(aliasToKnown, row[1], upc);
 	}
     sqlFreeResult(&sr);
 
-    sr = sqlGetResult(conn, NOSQLINJ "select kgID,alias from kgProtAlias");
+    sqlSafef(query, sizeof query, "select kgID,alias from kgProtAlias");
+    sr = sqlGetResult(conn, query);
     while ((row = sqlNextRow(sr)) != NULL)
 	{
 	char *upc = cloneString(row[0]);
 	touppers(upc);
         hashAdd(aliasToKnown, row[1], upc);
 	}
     sqlFreeResult(&sr);
     }
 }
 
 struct imageProbe *ipForImage(struct sqlConnection *conn, char *image)
 /* Get list of probes associated with image. */
 {
 struct hashEl *hel;
 struct imageProbe *ipList = NULL, *ip;
@@ -258,32 +265,33 @@
 }
 
 void vgGetText(char *outText, int knownDbCount, char **knownDbs)
 /* vgGetText - Get text for full text indexing for VisiGene. */
 {
 FILE *f = mustOpen(outText, "w");
 struct sqlConnection *conn = sqlConnect(db);
 struct sqlConnection *imageConn = sqlConnect(db);
 struct sqlResult *sr;
 char **row;
 
 verbose(2, "hashComplexTables\n");
 hashComplexTables(conn);
 verbose(2, "makeKnownGeneHashes\n");
 makeKnownGeneHashes(knownDbCount, knownDbs);
-sr = sqlGetResult(imageConn, 
-    NOSQLINJ "select id from image");
+char query[1024];
+sqlSafef(query, sizeof query, "select id from image");
+sr = sqlGetResult(imageConn, query);
 while ((row = sqlNextRow(sr)) != NULL)
     {
     verbose(3, "imageText on %s\n", row[0]);
     imageText(conn, row[0], f);
     }
 sqlFreeResult(&sr);
 carefulClose(&f);
 }
 
 int main(int argc, char *argv[])
 /* Process command line. */
 {
 optionInit(&argc, argv, options);
 db = optionVal("db", db);
 if (argc < 3)