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/protein/lib/pbUtil.c src/hg/protein/lib/pbUtil.c
index f80976a..5c34fa3 100644
--- src/hg/protein/lib/pbUtil.c
+++ src/hg/protein/lib/pbUtil.c
@@ -543,34 +543,34 @@
 else
     {
     hPrintf("<A HREF=\"../cgi-bin/hgNear?near_search=%s&db=%s&org=%s%s\"",
     	    spAcc, database, organism, hgsidStr);
     hPrintf(" TARGET=_BLANK>%s</A>&nbsp</LI>\n", spAcc);
     }
 hPrintf("\n");
 }
 
 void doGeneDetailsLink(char *spAcc, char *mrnaID, char *hgsidStr)
 {
 char cond_str[128];
 char *hggChrom, *hggStart, *hggEnd;
 char *displayId;
 
-sqlSafefFrag(cond_str, sizeof(cond_str), "kgId='%s' and spID='%s'", mrnaID, spAcc);
+sqlSafef(cond_str, sizeof(cond_str), "kgId='%s' and spID='%s'", mrnaID, spAcc);
 displayId  = sqlGetField(database, "kgXref", "spDisplayID", cond_str);
 /* Feed hgGene with chrom, txStart, and txEnd data, otherwise it would use whatever are in the cart */
-sqlSafefFrag(cond_str, sizeof(cond_str), "name='%s'", mrnaID);
+sqlSafef(cond_str, sizeof(cond_str), "name='%s'", mrnaID);
 hggChrom = sqlGetField(database, "knownGene", "chrom", cond_str);
 hggStart = sqlGetField(database, "knownGene", "txStart", cond_str);
 hggEnd   = sqlGetField(database, "knownGene", "txEnd", cond_str);
 if (mrnaID != NULL)
     {
     hPrintf("\n<LI>Gene Details Page - ");
     hPrintf("<A HREF=\"../cgi-bin/hgGene?db=%s&hgg_gene=%s&hgg_prot=%s&hgg_chrom=%s"
             "&hgg_start=%s&hgg_end=%s\"",
     	    database, mrnaID, displayId, hggChrom, hggStart, hggEnd);
     hPrintf(" TARGET=_BLANK>%s</A></LI>\n", mrnaID);
     }
 }
 
 void doBlatLink(char *db, char *sciName, char *commonName, char *aaSeq)
 {
@@ -587,129 +587,129 @@
 {
 struct sqlConnection *conn  = hAllocConn(database);
 struct sqlConnection *conn2 = hAllocConn(database);
 struct sqlResult *sr;
 char **row;
 char query[256];
 char cond_str[128];
 char *mapID, *locusID, *mapDescription;
 char *geneID;
 char *geneSymbol;
 char *cgapID, *biocMapID;
 boolean hasPathway;
 
 if (hTableExists(database, "kgXref"))
     {
-    sqlSafefFrag(cond_str, sizeof(cond_str), "kgID='%s'", mrnaName);
+    sqlSafef(cond_str, sizeof(cond_str), "kgID='%s'", mrnaName);
     geneSymbol = sqlGetField(database, "kgXref", "geneSymbol", cond_str);
     if (geneSymbol == NULL)
         {
         geneSymbol = mrnaName;
         }
     }
 else
     {
     geneSymbol = mrnaName;
     }
 
 /* Show Pathway links if any exist */
 hasPathway = FALSE;
 cgapID     = NULL;
 
 /*Process BioCarta Pathway link data */
 if (sqlTableExists(conn, "cgapBiocPathway"))
     {
-    sqlSafefFrag(cond_str, sizeof(cond_str), "alias='%s'", geneSymbol);
+    sqlSafef(cond_str, sizeof(cond_str), "alias='%s'", geneSymbol);
     cgapID = sqlGetField(database, "cgapAlias", "cgapID", cond_str);
 
     if (cgapID != NULL)
 	{
     	sqlSafef(query, sizeof(query), "select mapID from %s.cgapBiocPathway where cgapID = '%s'", database, cgapID);
     	sr = sqlGetResult(conn, query);
     	row = sqlNextRow(sr);
     	if (row != NULL)
 	    {
 	    if (!hasPathway)
 	        {
 	        hPrintf("<B>Pathways:</B>\n<UL>");
 	        hasPathway = TRUE;
 	    	}
 	    }
     	while (row != NULL)
 	    {
 	    biocMapID = row[0];
 	    hPrintf("<LI>BioCarta - &nbsp");
-	    sqlSafefFrag(cond_str, sizeof(cond_str), "mapID=%c%s%c", '\'', biocMapID, '\'');
+	    sqlSafef(cond_str, sizeof(cond_str), "mapID=%c%s%c", '\'', biocMapID, '\'');
 	    mapDescription = sqlGetField(database, "cgapBiocDesc", "description",cond_str);
 	    hPrintf("<A HREF = \"");
 	    hPrintf("http://cgap.nci.nih.gov/Pathways/BioCarta/%s", biocMapID);
 	    hPrintf("\" TARGET=_blank>%s</A> - %s <BR>\n", biocMapID, mapDescription);
 	    row = sqlNextRow(sr);
 	    }
         sqlFreeResult(&sr);
 	}
     }
 
 /* Process KEGG Pathway link data */
 if (sqlTableExists(conn, "keggPathway"))
     {
     sqlSafef(query, sizeof(query),
 	  "select * from %s.keggPathway where kgID = '%s'", database, mrnaName);
     sr = sqlGetResult(conn, query);
     row = sqlNextRow(sr);
     if (row != NULL)
 	{
 	if (!hasPathway)
 	    {
 	    hPrintf("<B>Pathways:</B>\n<UL>");
 	    hasPathway = TRUE;
 	    }
         while (row != NULL)
             {
             locusID = row[1];
 	    mapID   = row[2];
 	    hPrintf("<LI>KEGG - &nbsp");
-	    sqlSafefFrag(cond_str, sizeof(cond_str), "mapID=%c%s%c", '\'', mapID, '\'');
+	    sqlSafef(cond_str, sizeof(cond_str), "mapID=%c%s%c", '\'', mapID, '\'');
 	    mapDescription = sqlGetField(database, "keggMapDesc", "description", cond_str);
 	    hPrintf("<A HREF = \"");
 	    hPrintf("http://www.genome.ad.jp/dbget-bin/show_pathway?%s+%s", mapID, locusID);
 	    hPrintf("\" TARGET=_blank>%s</A> - %s <BR>\n",mapID, mapDescription);
             row = sqlNextRow(sr);
 	    }
 	}
     sqlFreeResult(&sr);
     }
 
 /* Process SRI BioCyc link data */
 if (sqlTableExists(conn, "bioCycPathway"))
     {
     sqlSafef(query, sizeof(query), "select * from %s.bioCycPathway where kgID = '%s'", database, mrnaName);
     sr = sqlGetResult(conn, query);
     row = sqlNextRow(sr);
     if (row != NULL)
 	{
 	if (!hasPathway)
 	    {
 	    hPrintf("<BR><B>Pathways:</B>\n<UL>");
 	    hasPathway = TRUE;
 	    }
         while (row != NULL)
             {
             geneID  = row[1];
 	    mapID   = row[2];
 	    hPrintf("<LI>BioCyc - &nbsp");
-	    sqlSafefFrag(cond_str, sizeof(cond_str), "mapID=%c%s%c", '\'', mapID, '\'');
+	    sqlSafef(cond_str, sizeof(cond_str), "mapID=%c%s%c", '\'', mapID, '\'');
 	    mapDescription = sqlGetField(database, "bioCycMapDesc", "description", cond_str);
 	    hPrintf("<A HREF = \"");
 
 	    hPrintf("http://biocyc.org/HUMAN/new-image?type=PATHWAY&object=%s&detail-level=2",
 		   mapID);
 	    hPrintf("\" TARGET=_blank>%s</A> %s <BR>\n",mapID, mapDescription);
             row = sqlNextRow(sr);
 	    }
 	}
     sqlFreeResult(&sr);
     }
 
 if (hasPathway)
     {
     hPrintf("</UL>\n");
@@ -755,31 +755,31 @@
 char *answer;
 
 /* get all genome DBs that support PB */
 connCentral = hConnectCentral();
 sqlSafef(queryCentral, sizeof(queryCentral),
       "select defaultDb.name, dbDb.organism from dbDb,defaultDb where hgPbOk=1 and defaultDb.name=dbDb.name");
 srCentral = sqlMustGetResult(connCentral, queryCentral);
 row3 = sqlNextRow(srCentral);
 
 /* go through each valid genome database that has PB */
 while (row3 != NULL)
     {
     gDatabase = row3[0];
     org       = row3[1];
     conn = sqlConnect(gDatabase);
-    sqlSafefFrag(cond_str, sizeof(cond_str), "alias='%s'", queryID);
+    sqlSafef(cond_str, sizeof(cond_str), "alias='%s'", queryID);
     answer = sqlGetField(gDatabase, "kgSpAlias", "count(distinct spID)", cond_str);
     sqlDisconnect(&conn);
 
     if ((answer != NULL) && (!sameWord(answer, "0")))
     	{
 	/* increase the count only by one, because new addition of splice variants to kgSpAlias
 	   would give a count of 2 for both the parent and the variant, which caused the
 	   problem when rescale button is pressed */
 	if (atoi(answer) > 0) pbProteinCnt++;
 	*database = strdup(gDatabase);
 	}
     row3 = sqlNextRow(srCentral);
     }
 sqlFreeResult(&srCentral);
 hDisconnectCentral(&connCentral);
@@ -849,55 +849,55 @@
 /* go through each genome DB that supports PB */
 sqlSafef(queryCentral, sizeof(queryCentral),
       "select defaultDb.name, dbDb.organism, dbDb.scientificName from dbDb,defaultDb where hgPbOk=1 and defaultDb.name=dbDb.name");
 srCentral = sqlMustGetResult(connCentral, queryCentral);
 row3 = sqlNextRow(srCentral);
 while (row3 != NULL)
     {
     gDatabase = row3[0];
     org       = row3[1];
     orgSciName= row3[2];
 
     protDbName = hPdbFromGdb(gDatabase);
     proteinsConn = sqlConnect(protDbName);
 
     conn = sqlConnect(gDatabase);
-    sqlSafefFrag(cond_str, sizeof(cond_str), "alias='%s' and spID != ''", queryID);
+    sqlSafef(cond_str, sizeof(cond_str), "alias='%s' and spID != ''", queryID);
     answer = sqlGetField(gDatabase, "kgSpAlias", "count(distinct spID)", cond_str);
     if ((answer != NULL) && (!sameWord(answer, "0")))
 	{
 	/* display organism name */
 	hPrintf("<FONT SIZE=4><B>");
 	hPrintf("<A href=\"https://www.ncbi.nlm.nih.gov/Taxonomy/Browser/wwwtax.cgi?mode=Undef&name=%s&lvl=0&srchmode=1\" TARGET=_blank>%s</A>",
            cgiEncode(orgSciName), orgSciName);
 	hPrintf(" (%s):</B></FONT>\n", org);
 	hPrintf("<UL>");
 
        	sqlSafef(query, sizeof(query),
               "select distinct spID from %s.kgSpAlias where alias='%s' "
 	      "and spID != ''",
 	      gDatabase, queryID);
 
     	sr = sqlMustGetResult(conn, query);
     	row = sqlNextRow(sr);
 
     	while (row != NULL)
 	    {
    	    spID = row[0];
-    	    sqlSafefFrag(cond_str, sizeof(cond_str), "accession='%s'", spID);
+    	    sqlSafef(cond_str, sizeof(cond_str), "accession='%s'", spID);
     	    displayID = sqlGetField(protDbName, "spXref3", "displayID", cond_str);
-    	    sqlSafefFrag(cond_str, sizeof(cond_str), "accession='%s'", spID);
+    	    sqlSafef(cond_str, sizeof(cond_str), "accession='%s'", spID);
     	    desc = sqlGetField(protDbName, "spXref3", "description", cond_str);
 
 	    /* display a protein */
 	    hPrintf(
 		"<LI><A HREF=\"../cgi-bin/pbGlobal?proteinID=%s&db=%s\">",
 		displayID, gDatabase);
 	    if (sameWord(spID, displayID) || (strstr(displayID, spID) != NULL))
 		{
 		hPrintf("%s</A> %s\n", spID, desc);
 		}
 	    else
 		{
 	    	hPrintf("%s</A> (aka %s) %s\n", spID, displayID, desc);
 		}
 
@@ -962,31 +962,31 @@
 	    if (sameWord(pbOrgSciName[i], protOrg) && pbOrgPresented[i])
 	    	{
 	    	skipIt = TRUE;
 		}
 	    }
 
 	/* print organism name if organism changed */
 	if (!sameWord(protOrg, oldOrg))
 	    {
 	    if (!sameWord(oldOrg, ""))
 	        {
 	        hPrintf("</UL>\n");
 		}
 	    if (!skipIt)
 	    	{
-    		sqlSafefFrag(cond_str, sizeof(cond_str), "id=%s and nameType='genbank common name'", taxonId);
+    		sqlSafef(cond_str, sizeof(cond_str), "id=%s and nameType='genbank common name'", taxonId);
     		answer = sqlGetField(PROTEOME_DB_NAME, "taxonNames", "name", cond_str);
 		hPrintf("<FONT SIZE=3><B>");
 		hPrintf("<A href=\"https://www.ncbi.nlm.nih.gov/Taxonomy/Browser/wwwtax.cgi?mode=Undef&name=%s&lvl=0&srchmode=1\" TARGET=_blank>%s</A>",
            		cgiEncode(protOrg), protOrg);
 		if (answer != NULL)
 		    {
 		    hPrintf(" (%s)", answer);
 		    }
 		hPrintf(":</B></FONT>\n");
 		}
             hPrintf("<UL>\n");
 	    }
 
 	/* print protein entry, if it is not already displayed in the PB supported genome list */
 	if (!skipIt)