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/oneShot/randomEst/randomEst.c src/hg/oneShot/randomEst/randomEst.c
index 25bb89a..798cdc1 100644
--- src/hg/oneShot/randomEst/randomEst.c
+++ src/hg/oneShot/randomEst/randomEst.c
@@ -1,82 +1,84 @@
 /* randomEst - Select random ESTs from database. */
 
 /* Copyright (C) 2013 The Regents of the University of California 
  * See kent/LICENSE or http://genome.ucsc.edu/license/ for licensing information. */
 #include "common.h"
 #include "linefile.h"
 #include "hash.h"
 #include "cheapcgi.h"
 #include "jksql.h"
 #include "fa.h"
 #include "hdb.h"
 
 
 void usage()
 /* Explain usage and exit. */
 {
 errAbort(
   "randomEst - Select random ESTs from database\n"
   "usage:\n"
   "   randomEst database count output.fa\n"
   "options:\n"
   "   -xxx=XXX\n"
   );
 }
 
 void randomEst(char *database, int count, char *output)
 /* randomEst - Select random ESTs from database. */
 {
 struct sqlConnection *conn = sqlConnect(database);
 struct sqlResult *sr;
 char **row;
 int i, elIx, okCount = 0;
 struct slName *list = NULL, *el;
 FILE *f = NULL;
 char **array = NULL;
 struct dnaSeq *seq;
 struct hash *uniqHash = newHash(0);
 
 hSetDb(database);
 printf("Scanning database\n");
-sr = sqlGetResult(conn, NOSQLINJ "select acc,type,direction from mrna");
+char query[1024];
+sqlSafef(query, sizeof query, "select acc,type,direction from mrna");
+sr = sqlGetResult(conn, query);
 while ((row = sqlNextRow(sr)) != NULL)
     {
     if (sameString(row[1], "EST") && sameString(row[2], "3"))
         {
 	el = newSlName(row[0]);
 	slAddHead(&list, el);
 	++okCount;
 	}
     }
 sqlFreeResult(&sr);
 printf("Got %d 3' ESTs\n", okCount);
 AllocArray(array, okCount);
 for (i=0, el = list; el != NULL; el = el->next, ++i)
     array[i] = el->name;
 
 printf("Selecting %d to put into %s\n", count, output);
 f = mustOpen(output, "w");
 for (i=0; i<count; ++i)
     {
     char *name;
     elIx = rand()%okCount;
     name = array[elIx];
     if (!hashLookup(uniqHash, name))
 	{
 	hashAdd(uniqHash, name, NULL);
 	seq = hRnaSeq(name);
 	faWriteNext(f, seq->name, seq->dna, seq->size);
 	freeDnaSeq(&seq);
 	}
     }
 }
 
 int main(int argc, char *argv[])
 /* Process command line. */
 {
 cgiSpoof(&argc, argv);
 if (argc != 4 || !isdigit(argv[2][0]))
     usage();
 randomEst(argv[1], atoi(argv[2]), argv[3]);
 return 0;
 }