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/hgc/peakClusters.c src/hg/hgc/peakClusters.c
index 3d72c74..7d75484 100644
--- src/hg/hgc/peakClusters.c
+++ src/hg/hgc/peakClusters.c
@@ -117,40 +117,47 @@
 else if (!compositeMetadataToggle(database, tdb, "metadata", TRUE, FALSE))
     {
     /* no metadata, but there is a track table to point TB at */
     struct trackDb *parent = trackDbTopLevelSelfOrParent(tdb);
     printf("<A target='_blank' title='browse table' "
                 "href='%s?db=%s&hgta_table=%s&hgta_group=%s&hgta_track=%s'>%s</A>",
                     hgTablesName(), database, table, parent->grp, parent->track, table);
     }
 webPrintLinkCellEnd();
 }
 
 static void queryInputTrackTable(struct dyString *query, char *inputTrackTable,
                                 struct slName *fieldList)
 /* Construct query in dyString to return contents of inputTrackTable ordered appropriately */
 {
-struct dyString *fields = dyStringNew(0);
-struct slName *field;
 sqlDyStringPrintf(query, "select tableName ");
+struct slName *field;
 for (field = fieldList; field != NULL; field = field->next)
-    sqlDyStringPrintfFrag(fields, ",%s", field->name);
-sqlDyStringPrintf(query, "%-s from %s", fields->string, inputTrackTable);
+    {
+    sqlDyStringPrintf(query, ",%s", field->name);
+    }
+sqlDyStringPrintf(query, " from %s", inputTrackTable);
 if (fieldList != NULL)
-    // skip leading comma
-    dyStringPrintf(query, " order by %s", fields->string+1);
-dyStringFree(&fields);
+    {
+    sqlDyStringPrintf(query, " order by ");
+    for (field = fieldList; field != NULL; field = field->next)
+	{
+	sqlDyStringPrintf(query, "%s", field->name);
+	if (field->next) 
+	    sqlDyStringPrintf(query, ",");
+	}
+    }
 }
 
 static struct hash *getVocabHash(char *fileName)
 /* Get vocabulary term hash */
 {
 struct hash *hash = raTagVals(fileName, "type");
 hashAdd(hash, "cellType", NULL);	/* Will the kludge never end, no, never! */
 return hash;
 }
 
 static void getVocab(struct trackDb *tdb, struct cart *cart, 
                         char **vocabFile, struct hash **vocabHash)
 /* Get vocabulary info from trackDb settings (CV or vocab tables) */
 {
 
@@ -369,34 +376,36 @@
 	table, item, seqName, start);
 sr = sqlGetResult(conn, query);
 row = sqlNextRow(sr);
 if (row != NULL)
     cluster = bedLoadN(row+rowOffset, 5);
 sqlFreeResult(&sr);
 
 if (cluster != NULL)
     {
     /* Get list of subgroups to display */
     char *inputTableFieldDisplay = trackDbSetting(tdb, "inputTableFieldDisplay");
     if (inputTableFieldDisplay != NULL)
         {
 	struct slName *fieldList = stringToSlNames(inputTableFieldDisplay);
 	char *inputTrackTable = trackDbRequiredSetting(tdb, "inputTrackTable");
+	char queryTblSafe[1024];
+	sqlSafef(queryTblSafe, sizeof queryTblSafe, "%s", inputTrackTable);
 
 	/* Print out some information about the cluster overall. */
 	printf("<B>Items in Cluster:</B> %s of %d<BR>\n", cluster->name, 
-	    sqlRowCount(conn, sqlCheckIdentifier(inputTrackTable)));
+	    sqlRowCount(conn, queryTblSafe));
 	printf("<B>Cluster Score (out of 1000):</B> %d<BR>\n", cluster->score);
 	printPos(cluster->chrom, cluster->chromStart, cluster->chromEnd, NULL, TRUE, NULL);
 
 	/* In a new section put up list of hits. */
 	webNewSection("List of Items in Cluster");
 	webPrintLinkTableStart();
 	printClusterTableHeader(fieldList, FALSE, FALSE, TRUE);
 	printPeakClusterInfo(tdb, cart, conn, inputTrackTable, fieldList, cluster);
 	}
     else
 	errAbort("Missing required trackDb setting %s for track %s",
 	    "inputTableFieldDisplay", tdb->track);
     webPrintLinkTableEnd();
     }
 printf("<A HREF=\"%s&g=htcListItemsAssayed&table=%s\" TARGET_blank>", hgcPathAndSettings(),
@@ -435,31 +444,31 @@
     else
         {
         sqlSafef(query, sizeof(query),
                 "select motif from %s where target = '%s'", motifMapTable, cluster->name);
         char *ret = sqlQuickString(conn, query);
         if (ret == NULL)
             {
             // missing target from table -- no canonical motif
             webNewEmptySection();
             return;
             }
         motifNames = slNameListFromString(ret, ',');
         }
     for (mn = motifNames; mn != NULL; mn = mn->next)
         {
-        sqlSafefFrag(where, sizeof(where), "name='%s' order by score desc limit 1", mn->name);
+        sqlSafef(where, sizeof(where), "name='%s' order by score desc limit 1", mn->name);
         sr = hRangeQuery(conn, motifTable, cluster->chrom, cluster->chromStart,
                      cluster->chromEnd, where, &rowOffset);
         if ((row = sqlNextRow(sr)) != NULL)
             {
             hit = bed6FloatScoreLoad(row + rowOffset);
             if (maxHit == NULL || maxHit->score < hit->score)
                 maxHit = hit;
             }
         sqlFreeResult(&sr);
         }
     }
 if (maxHit == NULL)
     {
     // Maintain table layout
     webNewEmptySection();
@@ -485,31 +494,31 @@
 if (hit->strand[0] == '-')
     reverseComplement(seq->dna, seq->size);
 if (motifPwmTable != NULL && sqlTableExists(conn, motifPwmTable))
     {
     motif = loadDnaMotif(hit->name, motifPwmTable);
     if (motif == NULL)
         return;
     motifLogoAndMatrix(&seq, 1, motif);
     }
 }
 
 void doFactorSource(struct sqlConnection *conn, struct trackDb *tdb, char *item, int start, int end)
 /* Display detailed info about a cluster of TFBS peaks from other tracks. */
 {
 char extraWhere[256];
-safef(extraWhere, sizeof extraWhere, "name='%s'", item);
+sqlSafef(extraWhere, sizeof extraWhere, "name='%s'", item);
 int rowOffset;
 struct sqlResult *sr = hRangeQuery(conn, tdb->table, seqName, start, end, extraWhere, &rowOffset);
 char **row = sqlNextRow(sr);
 struct factorSource *cluster = NULL;
 if (row != NULL)
     cluster = factorSourceLoad(row + rowOffset);
 sqlFreeResult(&sr);
 
 if (cluster == NULL)
     errAbort("Error loading cluster from track %s", tdb->track);
 
 char *sourceTable = trackDbRequiredSetting(tdb, "sourceTable");
 
 char *factorLink = cluster->name;
 char *vocab = trackDbSetting(tdb, "controlledVocabulary");