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/hgGene/gencodeSection.c src/hg/hgGene/gencodeSection.c
index cbb91fa..f2a9387 100644
--- src/hg/hgGene/gencodeSection.c
+++ src/hg/hgGene/gencodeSection.c
@@ -43,31 +43,31 @@
 fprintf(f, entrezUidFormat, "gene", geneid, "Graphics");
 }
 
 static void printCcdsExtUrl(char *ccdsId)
 /* Print out URL to link to CCDS database at NCBI */
 {
 printf("https://www.ncbi.nlm.nih.gov/CCDS/CcdsBrowse.cgi?REQUEST=CCDS&BUILDS=ALLBUILDS&DATA=%s", ccdsId);
 }
 
 static char *hgTracksPathAndSettings()
 /* Return path with hgTracks CGI path and session state variable. */
 {
 static struct dyString *dy = NULL;
 if (dy == NULL)
     {
-    dy = newDyString(128);
+    dy = dyStringNew(128);
     dyStringPrintf(dy, "%s?%s", hgTracksName(), cartSidUrlString(cart));
     }
 return dy->string;
 }
 
 /*
  * General notes:
  *  - this will be integrated into hgGene at some point, however this was
  *    done as part of hgc for timing reasons and to allow more time to design
  *    the hgGene part.
  *  - Tables below will output at least one row even if no data is available.
  *    
  */
 
 /* Various URLs and URL templates.  At one time, these were in the ra file,
@@ -160,61 +160,61 @@
     return strcmp(a->name, b->name);
 }
 
 static bool isProteinCodingTrans(struct gencodeAttrs *transAttrs)
 /* is a transcript protein coding? */
 {
 return sameString(transAttrs->transcriptClass, "coding");
 }
 
 static struct genePred *transAnnoLoad(struct sqlConnection *conn, struct trackDb *tdb, char *gencodeId)
 /* load the gencode annotations and sort the one corresponding to the one that was clicked on is
  * first.  Should only have one or two. */
 {
 // must check chrom due to PAR
 char where[256];
-sqlSafefFrag(where, sizeof(where), "(chrom = \"%s\") and (name = \"%s\")", curGeneChrom, gencodeId);
+sqlSafef(where, sizeof(where), "(chrom = \"%s\") and (name = \"%s\")", curGeneChrom, gencodeId);
 struct genePred *transAnno = genePredReaderLoadQuery(conn, tdb->table, where);
 slSort(&transAnno, transAnnoCmp);
 return transAnno;
 }
 
 static struct gencodeAttrs *transAttrsLoad(struct trackDb *tdb, struct sqlConnection *conn, char *gencodeId)
 /* load the gencode attributes */
 {
 char query[1024];
 sqlSafef(query, sizeof(query), "select * from %s where transcriptId = \"%s\"",
          getGencodeTable(tdb, "gencodeAttrs"), gencodeId);
 struct sqlResult *sr = sqlGetResult(conn, query);
 char **row = sqlNextRow(sr);
 if (row == NULL)
     errAbort("gencode transcript %s not found in %s", gencodeId,
              getGencodeTable(tdb, "gencodeAttrs"));
 // older version don't have proteinId column.
 //struct gencodeAttrs *transAttrs = gencodeAttrsLoad(row, sqlCountColumns(sr));
 struct gencodeAttrs *transAttrs = gencodeAttrsLoad(row);
 sqlFreeResult(&sr);
 return transAttrs;
 }
 
 static void getGeneBounds(struct trackDb *tdb, struct sqlConnection *conn, struct genePred *transAnno,
                           int *geneChromStart, int *geneChromEnd)
 /* find bounds for the gene */
 {
 // must check chrom due to PAR
 char where[256];
-sqlSafefFrag(where, sizeof(where), "(chrom = \"%s\") and (name2 = \"%s\")", curGeneChrom, transAnno->name2);
+sqlSafef(where, sizeof(where), "(chrom = \"%s\") and (name2 = \"%s\")", curGeneChrom, transAnno->name2);
 struct genePred *geneAnnos = genePredReaderLoadQuery(conn, tdb->table, where);
 struct genePred *geneAnno;
 *geneChromStart = transAnno->txStart;
 *geneChromEnd = transAnno->txEnd;
 for (geneAnno = geneAnnos; geneAnno != NULL; geneAnno = geneAnno->next)
     {
     *geneChromStart = min(*geneChromStart, geneAnno->txStart);
     *geneChromEnd = max(*geneChromEnd, transAnno->txEnd);
     }
 genePredFreeList(&geneAnnos);
 }
 
 static void *metaDataLoad(struct trackDb *tdb, struct sqlConnection *conn, char *gencodeId, char *tableBase, char *keyCol, unsigned queryOpts, sqlLoadFunc loadFunc)
 /* load autoSql objects for gencode meta data. */
 {
@@ -347,31 +347,31 @@
 freeMem(speciesArg);
 }
 
 static void writePosLink(char *chrom, int chromStart, int chromEnd)
 /* write link to a genomic position */
 {
 printf("<a href=\"%s&db=%s&position=%s%%3A%d-%d\">%s:%d-%d</A>",
        hgTracksPathAndSettings(), database,
        chrom, chromStart, chromEnd, chrom, chromStart+1, chromEnd);
 }
 
 static bool geneHasApprisTranscripts(struct trackDb *tdb, struct sqlConnection *conn, struct gencodeAttrs *transAttrs)
 /* check if any transcript in a gene has an APPRIS tags */
 {
 char query[1024];
-sqlSafefFrag(query, sizeof(query),
+sqlSafef(query, sizeof(query),
       "%s tag where tag.tag like 'appris%%' and transcriptId in "
       "(select transcriptId from %s where geneId='%s')",
       getGencodeTable(tdb, "gencodeTag"),
       getGencodeTable(tdb, "gencodeAttrs"),
       transAttrs->geneId);
 return sqlRowCount(conn, query) > 0;
 }
 
 static char* findApprisTag(struct gencodeTag *tags)
 /* search list for APPRIS tag or NULL */
 {
 struct gencodeTag *tag;
 for (tag = tags; tag != NULL; tag = tag->next)
     {
     if (startsWith("appris_", tag->tag))