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/snp/snpLoad/snpContigLocusIdCondense.c src/hg/snp/snpLoad/snpContigLocusIdCondense.c
index 2ce96c4..0d2f201 100644
--- src/hg/snp/snpLoad/snpContigLocusIdCondense.c
+++ src/hg/snp/snpLoad/snpContigLocusIdCondense.c
@@ -1,160 +1,161 @@
 /* snpContigLocusIdCondense 
  * Condense the ContigLocusIdFilter table to contain one row per snp_id,
    with unique, comma separated fxn_class values. */
 /* This assumes that the SNPs are in order!!! 
    errAbort if this is violated. */
 
 /* Copyright (C) 2013 The Regents of the University of California 
  * See kent/LICENSE or http://genome.ucsc.edu/license/ for licensing information. */
 #include "common.h"
 
 #include "hdb.h"
 
 char *functionStrings[] = {
 /* From snpFixed.SnpFunctionCode. */
 "unknown",
 "locus",
 "coding", // not used
 "coding-synon",
 "coding-nonsynon",
 "untranslated",
 "intron",
 "splice-site",
 // "cds-reference", not useful
 };
 
 boolean functionFound[ArraySize(functionStrings)];
 
 
 static char *snpDb = NULL;
 
 void usage()
 /* Explain usage and exit. */
 {
 errAbort(
     "snpContigLocusIdCondense - condense the ContigLocusIdFilter table to contain one row per snp_id\n"
     "usage:\n"
     "    snpContigLocusIdCondense snpDb\n");
 }
 
 void initArray()
 {
 int i;
 for (i=0; i<ArraySize(functionStrings); i++)
     functionFound[i] = FALSE;
 }
 
 void printArray(FILE *f)
 {
 int i;
 boolean first = TRUE;
 /* special case for UTR/intron */
 // if (functionFound[5] && functionFound[6])
     // functionFound[5] = FALSE;
 for (i=0; i<ArraySize(functionStrings); i++)
     if (functionFound[i])
         {
         if (!first) fprintf(f, ",");
         fprintf(f, "%s", functionStrings[i]);
 	first = FALSE;
 	}
 fprintf(f, "\n");
 }
 
 void condenseFunctionValues()
 /* combine function values for single snp */
 {
 char query[512];
 struct sqlConnection *conn = hAllocConn();
 struct sqlResult *sr;
 char **row;
 FILE *f;
 char *currentSnpString = NULL;
 int currentSnpNum = 0;
 int functionIndex = 0;
 
 f = hgCreateTabFile(".", "ContigLocusIdCondense");
 
 sqlSafef(query, sizeof(query), "select snp_id, fxn_class from ContigLocusIdFilter");
 
 initArray();
 sr = sqlGetResult(conn, query);
 while ((row = sqlNextRow(sr)) != NULL)
     {
     functionIndex = sqlUnsigned(row[1]);
     if (functionIndex == 8) continue;
     if (functionIndex > 8) 
         {
 	verbose(1, "unexpected functionIndex %d\n", functionIndex);
 	continue;
 	}
     if (currentSnpString == NULL) 
         {
         currentSnpString = cloneString(row[0]);
 	currentSnpNum = sqlUnsigned(row[0]);
 	}
     if (!sameString(row[0], currentSnpString))
         {
 	fprintf(f, "%s\t", currentSnpString);
 	printArray(f);
 	initArray();
 	if (currentSnpNum > sqlUnsigned(row[0]))
 	    errAbort("snps out of order: %d before %s\n", currentSnpNum, row[0]);
 	currentSnpString = cloneString(row[0]);
 	currentSnpNum = sqlUnsigned(row[0]);
 	}
     functionFound[functionIndex] = TRUE;
     }
 fprintf(f, "%s\t", currentSnpString);
 printArray(f);
 sqlFreeResult(&sr);
 hFreeConn(&conn);
 carefulClose(&f);
 }
 
 
 void createTable()
 /* create a ContigLocusIdCondense table */
 {
 struct sqlConnection *conn = hAllocConn();
-char *createString =
-NOSQLINJ "CREATE TABLE ContigLocusIdCondense (\n"
+char query[1024];
+sqlSafef(query, sizeof query, 
+"CREATE TABLE ContigLocusIdCondense (\n"
 "    snp_id int(11) not null,       \n"
 "    fxn_class varchar(255) not null\n"
-");\n";
+");\n");
 
-sqlRemakeTable(conn, "ContigLocusIdCondense", createString);
+sqlRemakeTable(conn, "ContigLocusIdCondense", query);
 }
 
 
 void loadDatabase()
 {
 struct sqlConnection *conn = hAllocConn();
 FILE *f = mustOpen("ContigLocusIdCondense.tab", "r");
 hgLoadTabFile(conn, ".", "ContigLocusIdCondense", &f);
 hFreeConn(&conn);
 }
 
 
 
 int main(int argc, char *argv[])
 /* Condense ContigLocusIdFilter and write to ContigLocusIdCondense. */
 {
 
 if (argc != 2)
     usage();
 
 snpDb = argv[1];
 hSetDb(snpDb);
 
 /* check for needed tables */
 if(!hTableExistsDb(snpDb, "ContigLocusIdFilter"))
     errAbort("no ContigLocusIdFilter table in %s\n", snpDb);
 
 
 condenseFunctionValues();
 createTable();
 loadDatabase();
 
 return 0;
 }