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/makeDb/hgKgGetText/hgKgGetText.c src/hg/makeDb/hgKgGetText/hgKgGetText.c
index e4829a8..24b9fce 100644
--- src/hg/makeDb/hgKgGetText/hgKgGetText.c
+++ src/hg/makeDb/hgKgGetText/hgKgGetText.c
@@ -37,31 +37,33 @@
 };
 
 struct kgXref *getKgList(struct sqlConnection *conn)
 /* Get list of all known genes. */
 {
 /* Verify that the number of fields present in this kgXref table is what's
  * expected, since more fields were added to the schema recently (10/19/2011) */
 struct slName *kgXrefFields = sqlListFields(conn, "kgXref");
 if (slCount(kgXrefFields) != KGXREF_NUM_COLS) 
     {
     errAbort("This genome has %d columns in kgXref but %d are expected - old genome?", 
 	     slCount(kgXrefFields), KGXREF_NUM_COLS);
     }
 slFreeList(kgXrefFields);
 
-struct sqlResult *sr = sqlGetResult(conn, NOSQLINJ "select * from kgXref");
+char query[1024];
+sqlSafef(query, sizeof query, "select * from kgXref");
+struct sqlResult *sr = sqlGetResult(conn, query);
 struct kgXref *kgList = NULL, *kg;
 char **row;
 struct hash *uniqHash = hashNew(18);
 
 while ((row = sqlNextRow(sr)) != NULL)
     {
     kg = kgXrefLoad(row);
     if (hashLookup(uniqHash, kg->kgID) == NULL)
 	{
 	hashAdd(uniqHash, kg->kgID, NULL);
 	slAddHead(&kgList, kg);
 	}
     }
 sqlFreeResult(&sr);
 slReverse(&kgList);
@@ -149,38 +151,38 @@
 if (refSeqHash != NULL)
     {
     char *s = hashFindVal(refSeqHash, kg->refseq);
     if (s == NULL && strchr(kg->refseq, '.') != NULL)
 	{
 	char *accOnly = cloneString(kg->refseq);
 	chopSuffix(accOnly);
 	s = hashFindVal(refSeqHash, accOnly);
 	freeMem(accOnly);
 	}
     if (s == NULL)
         s = "";
     fprintf(f, "\t%s", s);
     }
 
-safef(query, sizeof(query),
-    "NOSQLINJ select commentVal.val from comment,commentVal "
+sqlSafef(query, sizeof(query),
+    "select commentVal.val from comment,commentVal "
     "where comment.acc='%s' and comment.commentVal=commentVal.id"
     , spAcc);
 addText(query, spConn, f);
 
-safef(query, sizeof(query),
-    "NOSQLINJ select term.name from goaPart,term "
+sqlSafef(query, sizeof(query),
+    "select term.name from goaPart,term "
     "where goaPart.dbObjectId='%s' "
     "and goaPart.goId=term.acc"
     , spAcc);
 addText(query, goConn, f);
 
 fprintf(f, "\n");
 hashFree(&uniqHash);
 freeMem(spAcc);
 }
 
 void hgKgGetText(char *database, char *outFile)
 /* hgKgGetText - Get text from known genes into a file. */
 {
 FILE *f = mustOpen(outFile, "w");
 struct sqlConnection *conn = sqlConnect(database);