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/lib/genePred.c src/hg/lib/genePred.c
index 7bc3415..04525f0 100644
--- src/hg/lib/genePred.c
+++ src/hg/lib/genePred.c
@@ -5,67 +5,30 @@
 /* Copyright (C) 2014 The Regents of the University of California 
  * See kent/LICENSE or http://genome.ucsc.edu/license/ for licensing information. */
 
 #include "common.h"
 #include "gff.h"
 #include "jksql.h"
 #include "psl.h"
 #include "linefile.h"
 #include "genePred.h"
 #include "genbank.h"
 #include "rangeTree.h"
 #include "hdb.h"
 #include "chromInfo.h"
 
 
-/* SQL to create a genePred table */
-static char *createSql = 
-"CREATE TABLE %s ("
-"   %-s"                                 /* bin column goes here */
-"   name varchar(255) not null,"	/* mrna accession of gene */
-"   chrom varchar(255) not null,"	/* Chromosome name */
-"   strand char(1) not null,"		/* + or - for strand */
-"   txStart int unsigned not null,"	/* Transcription start position */
-"   txEnd int unsigned not null,"	/* Transcription end position */
-"   cdsStart int unsigned not null,"	/* Coding region start */
-"   cdsEnd int unsigned not null,"	/* Coding region end */
-"   exonCount int unsigned not null,"	/* Number of exons */
-"   exonStarts longblob not null,"	/* Exon start positions */
-"   exonEnds longblob not null,"	/* Exon end positions */
-"   INDEX(name)";
-
-static char *binFieldSql = 
-"    bin smallint unsigned not null,"
-"    INDEX(chrom,bin),";
-
-static char *noBinIndexSql = 
-"   INDEX(chrom,txStart),";
-
-static char *scoreFieldSql = 
-"   ,score int";
-
-static char *name2FieldSql = 
-"  ,name2 varchar(255) not null,"    /* Secondary name. (e.g. name of gene) or NULL if not available */
-"   INDEX(name2)";
-
-static char *cdsStatFieldSql = 
-"  ,cdsStartStat enum('none', 'unk', 'incmpl', 'cmpl') not null,"    /* Status of cdsStart annotation */
-"   cdsEndStat enum('none', 'unk', 'incmpl', 'cmpl') not null";     /* Status of cdsEnd annotation */
-
-static char *exonFramesFieldSql = 
-"   ,exonFrames longblob not null";    /* List of frame for each exon, or -1 if no frame or not known. NULL if not available. */
-
 struct genePred *genePredLoad(char **row)
 /* Load a genePred from row fetched with select * from genePred
  * from database.  Dispose of this with genePredFree(). 
  * NOTE: cannabalizes the row argument */
 {
 struct genePred *ret;
 int sizeOne;
 
 AllocVar(ret);
 ret->exonCount = sqlUnsigned(row[7]);
 ret->name = cloneString(row[0]);
 ret->chrom = cloneString(row[1]);
 strcpy(ret->strand, row[2]);
 ret->txStart = sqlUnsigned(row[3]);
 ret->txEnd = sqlUnsigned(row[4]);
@@ -1269,44 +1232,77 @@
 ZeroVar(&cds);
 cds.start = cdsStart;
 cds.end = cdsEnd;
 return genePredFromPsl3(psl, &cds, 0, genePredPslDefaults, insertMergeSize, insertMergeSize);
 }
 
 char* genePredGetCreateSql(char* table, unsigned optFields, unsigned options,
                            int chromIndexLen)
 /* Get SQL required to create a genePred table. optFields is a bit set
  * consisting of the genePredFields values. Options are a bit set of
  * genePredCreateOpts. Returned string should be freed.  This will create all
  * optional fields that preceed the highest optFields column.  chromIndexLen
  * is now ignored.. */
 {
 /* the >= is used so that we create preceeding fields. */
-char sqlCmd[1024];
+struct dyString *dy = sqlDyStringCreate(
+"CREATE TABLE %s (", table);
+
+/* bin column goes here */
+if (options & genePredWithBin)
+    sqlDyStringPrintf(dy,
+"    bin smallint unsigned not null,"
+"    INDEX(chrom,bin),");
+else
+    sqlDyStringPrintf(dy,
+"   INDEX(chrom,txStart),");
+
+sqlDyStringPrintf(dy,
+"   name varchar(255) not null,"	/* mrna accession of gene */
+"   chrom varchar(255) not null,"	/* Chromosome name */
+"   strand char(1) not null,"		/* + or - for strand */
+"   txStart int unsigned not null,"	/* Transcription start position */
+"   txEnd int unsigned not null,"	/* Transcription end position */
+"   cdsStart int unsigned not null,"	/* Coding region start */
+"   cdsEnd int unsigned not null,"	/* Coding region end */
+"   exonCount int unsigned not null,"	/* Number of exons */
+"   exonStarts longblob not null,"	/* Exon start positions */
+"   exonEnds longblob not null,"	/* Exon end positions */
+"   INDEX(name)"
+);
 
-sqlSafef(sqlCmd, sizeof(sqlCmd), createSql, table,
-      ((options & genePredWithBin) ? binFieldSql : noBinIndexSql));
 if (optFields >= genePredScoreFld)
-    safecat(sqlCmd, sizeof(sqlCmd), scoreFieldSql);
+    sqlDyStringPrintf(dy,
+"   ,score int");
+
 if (optFields >= genePredName2Fld)
-    safecat(sqlCmd, sizeof(sqlCmd), name2FieldSql);
+    sqlDyStringPrintf(dy,
+"  ,name2 varchar(255) not null,"    /* Secondary name. (e.g. name of gene) or NULL if not available */
+"   INDEX(name2)");
+
 if (optFields >= genePredCdsStatFld)
-    safecat(sqlCmd, sizeof(sqlCmd), cdsStatFieldSql);
+    sqlDyStringPrintf(dy,
+"  ,cdsStartStat enum('none', 'unk', 'incmpl', 'cmpl') not null,"    /* Status of cdsStart annotation */
+"   cdsEndStat enum('none', 'unk', 'incmpl', 'cmpl') not null");     /* Status of cdsEnd annotation */
+
 if (optFields >= genePredExonFramesFld)
-    safecat(sqlCmd, sizeof(sqlCmd), exonFramesFieldSql);
-safecat(sqlCmd, sizeof(sqlCmd), ")");
-return cloneString(sqlCmd);
+    sqlDyStringPrintf(dy,
+"   ,exonFrames longblob not null");    /* List of frame for each exon, or -1 if no frame or not known. NULL if not available. */
+
+sqlDyStringPrintf(dy, ")");
+
+return cloneString(dyStringCannibalize(&dy));
 }
 
 // FIXME: this really doesn't belong in this module
 struct genePred *getOverlappingGene(char *db, struct genePred **list, char *table, char *chrom, int cStart, int cEnd, char *name, int *retOverlap)
 {
 /* read all genes from a table find the gene with the biggest overlap. 
    Cache the list of genes to so we only read it once */
 
 char query[256];
 struct genePred *gene;
 struct sqlConnection *conn;
 struct sqlResult *sr;
 char **row;
 struct genePred *el = NULL, *bestMatch = NULL, *gp = NULL;
 int overlap = 0 , bestOverlap = 0, i;