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/poster/finPoster/finPoster.c src/hg/poster/finPoster/finPoster.c
index ccec09c..23a34d8 100644
--- src/hg/poster/finPoster/finPoster.c
+++ src/hg/poster/finPoster/finPoster.c
@@ -132,31 +132,31 @@
 /* A duplication that has been resolved by hand. */
     {
     struct resolvedDup *next;	/* Next in list. */
     char *name;			/* Name - allocated in hash. */
     char *trueChrom;		/* Real chromosome - allocated here. */
     int trueStart;		/* Start of real gene. */
     int trueEnd;		/* End of true gene. */
     boolean used;		/* True when used - can be used only once. */
     };
 
 void makeResolvedDupes(struct hash *hash, struct resolvedDup **retList)
 /* Read in list of dupes that Deanna resolved by hand. */
 {
 struct lineFile *lf = lineFileMayOpen(bestDupFile, TRUE);
 char *row[3], *parts[4];
-int wordCount, partCount;
+int partCount;
 struct resolvedDup *rd, *rdList = NULL;
 
 if (lf == NULL)
     {
     warn("Can't find %s", bestDupFile);
     return;
     }
 while (lineFileRow(lf, row))
     {
     AllocVar(rd);
     hashAddSaveName(hash, row[1], rd, &rd->name);
     partCount = chopString(row[2], ":-", parts, ArraySize(parts));
     if (partCount != 3)
         errAbort("Misformed chromosome location line %d of %s", lf->lineIx, lf->fileName);
     if (sameString(row[0], "Un"))
@@ -395,31 +395,30 @@
 	    s = stringIn("-pending", geneName);
 	    if (s != NULL)
 	       *s = 0;
 	    kg->name = cloneString(geneName);
 	    kg->acc = cloneString(gp->name);
 	    kg->chrom = hgOfficialChromName(chrom);
 	    kg->start = gp->cdsStart;
 	    kg->end = gp->cdsEnd;
 	    dlAddValTail(geneList, kg);
 	    }
 	}
     }
 
 for (node = geneList->head; !dlEnd(node); node = node->next)
     {
-    int len;
     char *name;
     kg = node->val;
 
     name = kg->name;
     if (hashLookup(diseaseHash, name))
 	{
 	if (hashLookup(orthoHash, name))
 	    col = &red;
 	else
 	    col = &lightRed;
 	}
     else
 	{
 	if (hashLookup(orthoHash, name))
 	    col = &blue;
@@ -474,31 +473,30 @@
     {
     cpgIslandStaticLoad(row + rowOffset, &el);
     printTab(f, cg, chrom, el.chromStart, el.chromEnd, 
 	    "cpgIsland", "tick", 0, 120, 0, ".");
     }
 sqlFreeResult(&sr);
 }
 
 
 void getRnaGenes(struct chromGaps *cg, char *chrom, struct sqlConnection *conn, FILE *f)
 /* Get RNA gene stuff. */
 {
 char **row;
 int rowOffset;
 struct sqlResult *sr = hChromQuery(conn, "rnaGene", chrom, NULL, &rowOffset);
-char query[256];
 struct rnaGene el;
 int r,g,b;
 
 while ((row = sqlNextRow(sr)) != NULL)
     {
     rnaGeneStaticLoad(row + rowOffset, &el);
     if (el.isPsuedo)
         {
 	r = 250;
 	g = 210;
 	b = 175;
 	}
     else
         {
 	r = 120;
@@ -565,31 +563,30 @@
 char **row;
 
 while ((row = sqlNextRow(sr)) != NULL)
      {
      na = netAlignLoad(row + rowOffset);
      binKeeperAdd(bk, na->tStart, na->tEnd, na);
      }
 sqlFreeResult(&sr);
 return bk;
 }
 
 void netBinKeeperFree(struct binKeeper **pBk)
 /* Get rid of net bin keeper. */
 {
 struct binKeeper *bk = *pBk;
-struct binKeeperCookie *pos;
 int bin;
 
 if (bk == NULL)
     return;
 for (bin=0; bin<bk->binCount; ++bin)
     {
     struct binElement *bel;
     for (bel = bk->binLists[bin]; bel != NULL; bel = bel->next)
         {
 	struct netAlign *na = bel->val;
 	netAlignFree(&na);
 	}
     }
 binKeeperFree(pBk);
 }
@@ -649,32 +646,32 @@
 
 return synFilter(deepestNa);
 }
 
 
 void getSyntenicPslTicks(char *track, char *netTrack, char *outputName, int r, int g, int b,
 	struct chromGaps *cg, char *chrom, int chromSize,
 	struct sqlConnection *conn, FILE *f)
 /* Get PSL track stuff. */
 {
 struct binKeeper *netBk = netBinKeeper(conn, netTrack, chrom, chromSize);
 int rowOffset;
 struct sqlResult *sr = hChromQuery(conn, track, chrom, NULL, &rowOffset);
 char **row;
 struct psl *psl;
-int lastEnd = -BIGNUM;
-int sepDistance = 20000;
+//int lastEnd = -BIGNUM;
+//int sepDistance = 20000;
 
 while ((row = sqlNextRow(sr)) != NULL)
     {
     psl = pslLoad(row + rowOffset);
     // if (psl->tStart > lastEnd + sepDistance)
 	{
 	if (isSyntenic(netBk, psl->tStart))
 	    printTab(f, cg, chrom, psl->tStart, psl->tStart+1, 
 		    outputName, "tick", r, g, b, ".");
 	lastEnd = psl->tStart;
 	}
     pslFree(&psl);
     }
 sqlFreeResult(&sr);
 netBinKeeperFree(&netBk);
@@ -1202,31 +1199,30 @@
 #ifdef NEVER
 sqlSafef(query, sizeof(query),
 	"select genoStart,genoEnd,repFamily from %s_rmsk "
 	"where repFamily = 'centr' or repFamily = 'telo'"
 	, chrom);
 #endif /* NEVER */
 sqlSafef(query, sizeof(query),
 	"select genoStart,genoEnd,repFamily from %s_rmsk "
 	"where repFamily = 'centr'"
 	, chrom);
 sr = sqlGetResult(conn, query);
 while ((row = sqlNextRow(sr)) != NULL)
     {
     int start = atoi(row[0]);
     int end = atoi(row[1]);
-    char *label = NULL;
     if (sameString(row[2], "telo"))
 	printTab(f, cg, chrom, start, end, 
 		    "repTelomere", "box", 50, 50, 250, ".");
     else
 	printTab(f, cg, chrom, start, end, 
 		    "repCentromere", "box", 0, 200, 0, ".");
     }
 sqlFreeResult(&sr);
 }
 
 void fakeRnaGenesFromRmsk(struct chromGaps *cg, char *chrom, 
 	struct sqlConnection *conn, FILE *f)
 /* Get RNA genes from RepeatMasker. */
 {
 struct sqlResult *sr;
@@ -1712,47 +1708,48 @@
 	fprintf(f, "\n");
 	++dupCount;
 	}
     }
 printf("%d unresolved dupes\n", dupCount);
 }
 
 struct hash *makeEnsKnownHash(struct sqlConnection *conn)
 /* Make hash containing names of all ensemble known genes that
  * are mapped to knwon genes, using knownToEnsemble table. */
 {
 struct hash *hash = newHash(16);
 struct sqlResult *sr;
 char **row;
 
-sr = sqlGetResult(conn, NOSQLINJ "select value from knownToEnsembl");
+char query[1024];
+sqlSafef(query, sizeof query, "select value from knownToEnsembl");
+sr = sqlGetResult(conn, query);
 while ((row = sqlNextRow(sr)) != NULL)
     {
     hashAdd(hash, row[0], NULL);
     }
 sqlFreeResult(&sr);
 return hash;
 }
 
 void finPoster(int chromCount, char *chromNames[])
 /* finPoster - Search database info for making foldout. */
 {
 int i;
 struct sqlConnection *conn = sqlConnect(database);
 struct hash *dupHash = newHash(0);
 struct hash *resolvedDupHash = newHash(8);
-struct resolvedDup *rdList = NULL;
 struct hash *diseaseHash = makeSecondColumnHash(diseaseFile);
 struct hash *orthoHash = makeFirstColumnHash(orthoFile);
 struct hash *weedHash = makeFirstColumnHash(weedFile);
 struct hash *muteHash = makeMuteHash(mutFile);
 struct hash *ensKnownHash = makeEnsKnownHash(conn);
 
 dupeFile = mustOpen(dupeFileName, "w");
 hSetDb(database);
 // makeResolvedDupes(resolvedDupHash, &rdList);
 for (i=0; i<chromCount; ++i)
     {
     char fileName[512];
     FILE *f;
     sprintf(fileName, "%s.tab", chromNames[i]);
     f = mustOpen(fileName, "w");