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/getFeatDna/getFeatDna.c src/hg/getFeatDna/getFeatDna.c
index 93da665..bcf84ab 100644
--- src/hg/getFeatDna/getFeatDna.c
+++ src/hg/getFeatDna/getFeatDna.c
@@ -103,31 +103,31 @@
         reverseComplement(seq->dna, size);
     if (faName == NULL)
         faName = makeFaStartLine(chrom, table, start, size);
     writeOut(f, chrom, start, size, strand, seq->dna, faName);
     freeDnaSeq(&seq);
     }
 }
 
 void chromFeatDna(char *table, char *chrom, FILE *f)
 /* chromFeatDna - Get dna for a type of feature on one chromosome. */
 {
 /* Get chromosome in lower case case.  If merging set bits that 
  * are part of feature of interest to upper case, and then scan 
  * for blocks of lower upper case and output them. If not merging
  * then just output dna for features directly. */
-struct dyString *query = newDyString(512);
+struct dyString *query = dyStringNew(512);
 struct sqlConnection *conn = hAllocConn(database);
 struct sqlResult *sr;
 char **row;
 char chromField[32], startField[32], endField[32];
 struct dnaSeq *seq = NULL;
 DNA *dna = NULL;
 int s=0,e,sz,i,size=0;
 boolean lastIsUpper = FALSE, isUpper = FALSE;
 char nibFileName[512];
 FILE *nibFile = NULL;
 int nibSize;
 
 if (!hFindChromStartEndFields(database, table, chromField, startField, endField))
     errAbort("Couldn't find chrom/start/end fields in table %s", table);
 
@@ -137,92 +137,92 @@
     dna = seq->dna;
     size = seq->size;
     }
 else
     {
     hNibForChrom(database, chrom, nibFileName);
     nibOpenVerify(nibFileName, &nibFile, &nibSize);
     }
 
 if (breakUp)
     {
     if (sameString(startField, "tStart"))
 	{
 	sqlDyStringPrintf(query, "select * from %s where tStart >= %d and tEnd < %d",
 	    table, chromStart, chromEnd);
-	dyStringPrintf(query, " and %s = '%s'", chromField, chrom);
+	sqlDyStringPrintf(query, " and %s = '%s'", chromField, chrom);
 	if (where != NULL)
-	    dyStringPrintf(query, " and %s", where);
+	    sqlDyStringPrintf(query, " and %-s", where);
 	sr = sqlGetResult(conn, query->string);
 	while ((row = sqlNextRow(sr)) != NULL)
 	    {
 	    struct psl *psl = pslLoad(row);
 	    if (psl->strand[1] == '-')	/* Minus strand on target */
 		{
 		int tSize = psl->tSize;
 		for (i=0; i<psl->blockCount; ++i)
 		     {
 		     sz = psl->blockSizes[i];
 		     s = tSize - (psl->tStarts[i] + sz);
 		     outputDna(f, chrom, table, 
 		         s, sz, dna, nibFileName, nibFile, nibSize, '-', NULL);
 		     }
 		}
 	    else
 		{
 		for (i=0; i<psl->blockCount; ++i)
 		     outputDna(f, chrom, table, psl->tStarts[i], psl->blockSizes[i], 
 			    dna, nibFileName, nibFile, nibSize, '+', NULL);
 		}
 	    pslFree(&psl);
 	    }
 	}
     else if (sameString(startField, "txStart"))
         {
 	sqlDyStringPrintf(query, "select * from %s where txStart >= %d and txEnd < %d",
 	    table, chromStart, chromEnd);
-	dyStringPrintf(query, " and %s = '%s'", chromField, chrom);
+	sqlDyStringPrintf(query, " and %s = '%s'", chromField, chrom);
 	if (where != NULL)
-	    dyStringPrintf(query, " and %s", where);
+	    sqlDyStringPrintf(query, " and %-s", where);
 	sr = sqlGetResult(conn, query->string);
 	while ((row = sqlNextRow(sr)) != NULL)
 	    {
 	    struct genePred *gp = genePredLoad(row);
 	    for (i=0; i<gp->exonCount; ++i)
 		 {
 		 s = gp->exonStarts[i];
 		 sz = gp->exonEnds[i] - s;
 		 outputDna(f, chrom, table, 
 		    s, sz, dna, nibFileName, nibFile, nibSize, 
 		    gp->strand[0], gp->name);
 		 }
 	    genePredFree(&gp);
 	    }
 	}
     else
         {
         errAbort("Can only use breakUp parameter with psl or genePred formatted tables");
 	}
     }
 else
     {
     sqlDyStringPrintf(query, "select %s,%s from %s where %s >= %d and %s < %d", 
 	    startField, endField, table,
 	    startField, chromStart, endField, chromEnd);
-    dyStringPrintf(query, " and %s = '%s'", chromField, chrom);
+    sqlDyStringPrintf(query, " and %s = '%s'", chromField, chrom);
     if (where != NULL)
-	dyStringPrintf(query, " and %s", where);
+	sqlDyStringPrintf(query, " and %-s", where);
     sr = sqlGetResult(conn, query->string);
     while ((row = sqlNextRow(sr)) != NULL)
 	{
 	s = sqlUnsigned(row[0]);
 	e = sqlUnsigned(row[1]);
 	sz = e - s;
 	if (seq != NULL && (sz < 0 || e >= size))
 	    errAbort("Coordinates out of range %d %d (%s size is %d)", s, e, chrom, size);
 	outputDna(f, chrom, table, s, sz, dna, nibFileName, nibFile, nibSize, 
 		'+', NULL);
 	}
     }
 
 /* Merge nearby upper case blocks. */
 if (merge > 0)
@@ -299,22 +299,31 @@
     chromFeatDna(chrTable, chrom, f);
     }
 carefulClose(&f);
 if (!toStdout)
     printf("%d features extracted to %s\n", blockIx, outName);
 }
 
 int main(int argc, char *argv[])
 /* Process command line. */
 {
 cgiSpoof(&argc, argv);
 if (argc != 5)
     usage();
 chromStart = cgiUsualInt("chromStart", chromStart);
 chromEnd = cgiUsualInt("chromEnd", chromEnd);
+
 where = cgiOptionalString("where");
+if (where)
+    {
+    char whereSafe[4096];
+    // TRUST command-line SQL where clause
+    sqlSafef(whereSafe, sizeof whereSafe, where, NULL);
+    where = whereSafe;
+    }
+
 breakUp = cgiBoolean("breakUp");
 merge = cgiUsualInt("merge", merge);
 database = argv[1];
 getFeatDna(argv[2], argv[3], argv[4]);
 return 0;
 }