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/hgVai/hgVai.c src/hg/hgVai/hgVai.c
index 337ac33..7d18a19 100644
--- src/hg/hgVai/hgVai.c
+++ src/hg/hgVai/hgVai.c
@@ -797,32 +797,33 @@
 printf("Include <A HREF='https://www.ncbi.nlm.nih.gov/projects/SNP/' TARGET=_BLANK>dbSNP</A> "
        "rs# ID if one exists<BR>\n");
 puts("<BR>");
 endCollapsibleSection();
 }
 
 #define GENCODE_PREFIX "wgEncodeGencode"
 
 struct slName *getGencodeTagVersions()
 /* Return a list of version strings from the ends of wgEncodeGencodeTag% tables. */
 {
 static struct slName *tagVersions = NULL;
 if (tagVersions == NULL && !startsWith(hubTrackPrefix, database))
     {
     struct sqlConnection *conn = hAllocConn(database);
-    struct slName *tagTables = sqlQuickList(conn,
-                                            NOSQLINJ "show tables like '"GENCODE_PREFIX"Tag%'");
+    char query[1024];
+    sqlSafef(query, sizeof query, "show tables like '"GENCODE_PREFIX"Tag%%'");
+    struct slName *tagTables = sqlQuickList(conn, query);
     int offset = strlen(GENCODE_PREFIX"Tag");
     struct slName *tt;
     for (tt = tagTables;  tt != NULL;  tt = tt->next)
         slAddHead(&tagVersions, slNameNew(tt->name + offset));
     hFreeConn(&conn);
     }
 return slNameCloneList(tagVersions);
 }
 
 boolean knownGeneHasGencodeTags()
 /* Return TRUE if this database has knownToTag for knownGene. */
 {
 return hTableExists(database, "knownGene") && hTableExists(database, "knownToTag");
 }
 
@@ -2319,34 +2320,36 @@
     return;
 struct trackDb *tdb = hFindLatestSnpTrack(database, NULL, &fullTrackList);
 if (tdb == NULL)
     return;
 struct sqlConnection *conn = hAllocConn(assembly->name);
 // Build a 'name in (...)' query, and build a hash of IDs so we can test whether all were found
 struct dyString *dq = sqlDyStringCreate("select chrom, chromStart, chromEnd, name, strand, "
 					"refUCSC, observed, alleleFreqCount, alleles "
                                         "from %s where name in (",
 					tdb->table);
 struct hash *idHash = hashNew(0);
 struct slName *id;
 for (id = rsIds;  id != NULL;  id = id->next)
     {
     tolowers(id->name);
-    dyStringPrintf(dq, "%s'%s'", (id != rsIds ? "," : ""), id->name);
+    if (id != rsIds)
+	sqlDyStringPrintf(dq, ",");
+    sqlDyStringPrintf(dq, "'%s'", id->name);
     hashStoreName(idHash, id->name);
     }
-dyStringAppend(dq, ");");
+sqlDyStringPrintf(dq, ");");
 struct sqlResult *sr = sqlGetResult(conn, dq->string);
 // Construct a minimal VCF row to make a vcfRecord for each variant.
 char *vcfRow[9];
 vcfRow[5] = vcfRow[6] = vcfRow[7] = "."; // placeholder for qual, filter, info
 // It would be cool to someday add snpNNN's exceptions column to the filter or info.
 struct dyString *dyAltAlStr = dyStringNew(0);
 int i;
 char **row;
 while ((row = sqlNextRow(sr)) != NULL)
     {
     char *chrom = row[0];
     uint chromStart = atoll(row[1]);
     uint chromEnd = atoll(row[2]);
     char *name = row[3];
     char *strand = row[4];
@@ -2403,46 +2406,46 @@
     if (needLeftBase)
 	{
 	vcfStart--;
 	//#*** Need something more efficient, like a sequence cache inside assembly!
 	struct dnaSeq *seq = twoBitReadSeqFragLower(assembly->tbf, chrom,
 						    chromStart-1, chromStart);
 	toUpperN(seq->dna, 1);
 	char leftBase = seq->dna[0];
 	dnaSeqFree(&seq);
 	char refAlPlus[strlen(refAl)+2];
 	safef(refAlPlus, sizeof(refAlPlus), "%c%s", leftBase, refAl);
 	safecpy(refAl, sizeof(refAl), refAlPlus);
 	for (i = 0;  i < altAlCount;  i++)
 	    {
 	    if (i > 0)
-		dyStringAppendC(dyAltAlStr, ',');
-            dyStringPrintf(dyAltAlStr, "%c%s", leftBase, altAls[i]);
+		sqlDyStringPrintf(dyAltAlStr, ",");
+            sqlDyStringPrintf(dyAltAlStr, "%c%s", leftBase, altAls[i]);
 	    }
 	}
     else
 	{
 	// Not an indel, just make comma-sep string of alt alleles.
 	for (i = 0;  i < altAlCount;  i++)
 	    {
 	    if (i > 0)
-		dyStringAppendC(dyAltAlStr, ',');
-	    dyStringAppend(dyAltAlStr, altAls[i]);
+		sqlDyStringPrintf(dyAltAlStr, ",");
+	    sqlDyStringPrintf(dyAltAlStr, "%s", altAls[i]);
 	    }
 	}
     if (altAlCount == 0)
-        dyStringAppendC(dyAltAlStr, '.');
+        sqlDyStringPrintf(dyAltAlStr, ".");
     char vcfStartStr[64];
     safef(vcfStartStr, sizeof(vcfStartStr), "%d", vcfStart);
     vcfRow[0] = chrom;
     vcfRow[1] = vcfStartStr;
     vcfRow[2] = name;
     vcfRow[3] = refAl;
     vcfRow[4] = dyAltAlStr->string;
     struct vcfRecord *rec = vcfRecordFromRow(vcff, vcfRow);
     slAddHead(pRecList, rec);
     }
 dyStringFree(&dyAltAlStr);
 // Check for IDs not found
 struct slName *notFoundIds = hashListNames(idHash);
 if (notFoundIds != NULL)
     {