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/makeDb/outside/hgFlyBase/hgFlyBase.c src/hg/makeDb/outside/hgFlyBase/hgFlyBase.c
index c1bdda8..6745f8f 100644
--- src/hg/makeDb/outside/hgFlyBase/hgFlyBase.c
+++ src/hg/makeDb/outside/hgFlyBase/hgFlyBase.c
@@ -107,31 +107,31 @@
 };
 
 char *spelledGreek(char *abbr)
 /* Convert from 'agr;' to 'alpha' or return NULL */
 {
 int i;
 for (i=0; i<ArraySize(ungreekTable); i += 2)
     if (sameWord(ungreekTable[i], abbr))
         return ungreekTable[i+1];
 return NULL;
 }
 
 char *ungreek(char *s)
 /* Get rid of greek characters and unwanted tags. */
 {
-struct dyString *dy = newDyString(0);
+struct dyString *dy = dyStringNew(0);
 char *result;
 char c;
 while ((c = *s++) != 0)
     {
     boolean special = FALSE;
     if (c == '&')
         {
 	char *e = strchr(s, ';');
 	if (e != NULL)
 	    {
 	    int size = e - s;
 	    if (size <= 4)
 	        {
 		char abbr[5];
 		char *english;
@@ -198,124 +198,134 @@
     dyStringAppend(dy, line+2);
     }
 return dy;
 }
 
 
 struct ref
 /* Keep track of reference. */
     {
     int id;	/* Reference ID. */
     };
 
 void remakeTables(struct sqlConnection *conn)
 /* Remake all our tables. */
 {
-sqlRemakeTable(conn, "fbGene", 
+char query[1024];
+sqlSafef(query, sizeof query, 
 "#Links FlyBase IDs, gene symbols and gene names\n"
-NOSQLINJ "CREATE TABLE fbGene (\n"
+"CREATE TABLE fbGene (\n"
 "    geneId varchar(255) not null,	# FlyBase ID\n"
 "    geneSym varchar(255) not null,	# Short gene symbol\n"
 "    geneName varchar(255) not null,	# Gene name - up to a couple of words\n"
 "              #Indices\n"
 "    PRIMARY KEY(geneId(11)),\n"
 "    INDEX(geneSym(8)),\n"
 "    INDEX(geneName(12))\n"
 ")\n");
+sqlRemakeTable(conn, "fbGene", query); 
 
-sqlRemakeTable(conn, "fbTranscript", 
+sqlSafef(query, sizeof query, 
 "#Links FlyBase gene IDs and BDGP transcripts\n"
-NOSQLINJ "CREATE TABLE fbTranscript (\n"
+"CREATE TABLE fbTranscript (\n"
 "    geneId varchar(255) not null,	# FlyBase ID\n"
 "    transcriptId varchar(255) not null,	# BDGP Transcript ID\n"
 "              #Indices\n"
 "    PRIMARY KEY(transcriptId(11)),\n"
 "    INDEX(transcriptId(11))\n"
 ")\n");
+sqlRemakeTable(conn, "fbTranscript", query); 
 
-sqlRemakeTable(conn, "fbSynonym", 
+sqlSafef(query, sizeof query, 
 "#Links all the names we call a gene to it's flybase ID\n"
-NOSQLINJ "CREATE TABLE fbSynonym (\n"
+"CREATE TABLE fbSynonym (\n"
 "    geneId varchar(255) not null,	# FlyBase ID\n"
 "    name varchar(255) not null,	# A name (synonym or real\n"
 "              #Indices\n"
 "    INDEX(geneId(11)),\n"
 "    INDEX(name(12))\n"
 ")\n");
+sqlRemakeTable(conn, "fbSynonym", query);
 
-sqlRemakeTable(conn, "fbAllele", 
+sqlSafef(query, sizeof query, 
 "#The alleles of a gene\n"
-NOSQLINJ "CREATE TABLE fbAllele (\n"
+"CREATE TABLE fbAllele (\n"
 "    id int not null,	# Allele ID\n"
 "    geneId varchar(255) not null,	# Flybase ID of gene\n"
 "    name varchar(255) not null,	# Allele name\n"
 "              #Indices\n"
 "    PRIMARY KEY(id),\n"
 "    INDEX(geneId(11))\n"
 ")\n");
+sqlRemakeTable(conn, "fbAllele", query); 
 
-sqlRemakeTable(conn, "fbRef", 
+sqlSafef(query, sizeof query, 
 "#A literature or sometimes database reference\n"
-NOSQLINJ "CREATE TABLE fbRef (\n"
+"CREATE TABLE fbRef (\n"
 "    id int not null,	# Reference ID\n"
 "    text longblob not null,	# Usually begins with flybase ref ID, but not always\n"
 "              #Indices\n"
 "    PRIMARY KEY(id)\n"
 ")\n");
+sqlRemakeTable(conn, "fbRef", query);
 
-sqlRemakeTable(conn, "fbRole", 
+sqlSafef(query, sizeof query, 
 "#Role of gene in wildType\n"
-NOSQLINJ "CREATE TABLE fbRole (\n"
+"CREATE TABLE fbRole (\n"
 "    geneId varchar(255) not null,	# Flybase Gene ID\n"
 "    fbAllele int not null,	# ID in fbAllele table or 0 if not allele-specific\n"
 "    fbRef int not null,	# ID in fbRef table\n"
 "    text longblob not null,	# Descriptive text\n"
 "              #Indices\n"
 "    INDEX(geneId(11))\n"
 ")\n");
+sqlRemakeTable(conn, "fbRole", query);
 
-sqlRemakeTable(conn, "fbPhenotype", 
+sqlSafef(query, sizeof query, 
 "#Observed phenotype in mutant.  Sometimes contains gene function info\n"
-NOSQLINJ "CREATE TABLE fbPhenotype (\n"
+"CREATE TABLE fbPhenotype (\n"
 "    geneId varchar(255) not null,	# Flybase Gene ID\n"
 "    fbAllele int not null,	# ID in fbAllele table or 0 if not allele-specific\n"
 "    fbRef int not null,	# ID in fbRef table\n"
 "    text longblob not null,	# Descriptive text\n"
 "              #Indices\n"
 "    INDEX(geneId(11))\n"
 ")\n");
+sqlRemakeTable(conn, "fbPhenotype", query);
 
-sqlRemakeTable(conn, "fbGo", 
+sqlSafef(query, sizeof query, 
 "#Links FlyBase gene IDs and GO IDs/aspects\n"
-NOSQLINJ "CREATE TABLE fbGo (\n"
+"CREATE TABLE fbGo (\n"
 "    geneId varchar(255) not null,	# FlyBase ID\n"
 "    goId varchar(255) not null,	# GO ID\n"
 "    aspect varchar(255) not null,      # P (process), F (function) or C (cellular component)"
 "              #Indices\n"
 "    INDEX(geneId(11)),\n"
 "    INDEX(goId(10))\n"
 ")\n");
+sqlRemakeTable(conn, "fbGo", query);
 
-sqlRemakeTable(conn, "fbUniProt", 
+sqlSafef(query, sizeof query, 
 "#Links FlyBase gene IDs and UniProt IDs/aspects\n"
-NOSQLINJ "CREATE TABLE fbUniProt (\n"
+"CREATE TABLE fbUniProt (\n"
 "    geneId varchar(255) not null,	# FlyBase ID\n"
 "    uniProtId varchar(255) not null,	# UniProt ID\n"
 "              #Indices\n"
 "    INDEX(geneId(11)),\n"
 "    INDEX(uniProtId(6))\n"
 ")\n");
+sqlRemakeTable(conn, "fbUniProt", query);
 }
 
 struct geneAlt
 /* Gene and list of isoforms. */
     {
     struct geneAlt *next;
     char *fbName;		/* Flybase gene name. */
     char *bdgpName;		/* BDGP Gene name. */
     struct slName *isoformList;	/* List of BDGP isoforms. */
     };
 
 void getAllSplices(char *database, FILE *f)
 /* Write out table linking flybase genes with BDGP transcripts --
  * unfortunately bdgpGeneInfo lacks -R* transcript/isoform identifiers,
  * so strip those off of bdgpGene.name.