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/hgLoadChromGraph/hgLoadChromGraph.c src/hg/makeDb/hgLoadChromGraph/hgLoadChromGraph.c
index 577c0e0..d6d8afc 100644
--- src/hg/makeDb/hgLoadChromGraph/hgLoadChromGraph.c
+++ src/hg/makeDb/hgLoadChromGraph/hgLoadChromGraph.c
@@ -30,49 +30,30 @@
   "                 instead of /gbdb/<database>/chromGraph for the\n"
   "                 .cgb file.\n\n"
   "*NOTE: remember to put .cgb file in a good place for the web server\n"
   "       and make a link to the file in /gbdb/<database>/chromGraph.\n"
   );
 }
 
 static struct optionSpec options[] = {
    {"noLoad", OPTION_BOOLEAN},
    {"idTable", OPTION_STRING},
    {"minusLog10", OPTION_BOOLEAN},
    {"pathPrefix", OPTION_STRING},
    {NULL, 0},
 };
 
-char *createString = 
-"CREATE TABLE %s (\n"
-"    chrom varchar(255) not null,       # Chromosome\n"
-"    chromStart int not null,   # Start coordinate\n"
-"    val double not null,        # Value at coordinate\n"
-"              #Indices\n"
-"    PRIMARY KEY(chrom(%d),chromStart)\n"
-");\n";
-
-char *metaCreateString = 
-NOSQLINJ "CREATE TABLE metaChromGraph (\n"
-"    name varchar(255) not null,        # Corresponds to chrom graph table name\n"
-"    minVal double not null,    # Minimum value observed\n"
-"    maxVal double not null,    # Maximum value observed\n"
-"    binaryFile varchar(255) not null,  # Location of binary data point file if any\n"
-"              #Indices\n"
-"    PRIMARY KEY(name(32))\n"
-");\n";
-
 void checkTableForFields(struct sqlConnection *conn, char *tableName)
 /* Do basic checks on the table to make sure it's kosher. i.e. */
 /* chrom, chromStart, name all exist along with the table itself. */
 {
 struct slName *fieldList;
 if (!sqlTableExists(conn, tableName))
     errAbort("table %s not found.", tableName);
 fieldList = sqlListFields(conn, tableName);
 if (!slNameInList(fieldList, "chrom"))
     errAbort("table %s doesn't have a chrom field. It must have chrom, chromStart, and name at the minimum.", tableName);
 if (!slNameInList(fieldList, "chromStart"))
     errAbort("table %s doesn't have a chrom field. It must have chrom, chromStart, and name at the minimum.", tableName);
 if (!slNameInList(fieldList, "name"))
     errAbort("table %s doesn't have a chrom field. It must have chrom, chromStart, and name at the minimum.", tableName);
 slNameFreeList(&fieldList);
@@ -172,40 +153,60 @@
 
 
 /* Sort and write out temp file. */
 slSort(&list, chromGraphCmp);
 f = hgCreateTabFile(tempDir, track);
 for (el = list; el != NULL; el = el->next)
     chromGraphTabOut(el, f);
 
 if (doLoad)
     {
     struct dyString *dy = dyStringNew(0);
     struct sqlConnection *conn;
 
     /* Set up connection to database and create main table. */
     conn = hAllocConn(db);
-    sqlDyStringPrintf(dy, createString, track, hGetMinIndexLength(db));
+    sqlDyStringPrintf(dy, 
+    "CREATE TABLE %s (\n"
+    "    chrom varchar(255) not null,       # Chromosome\n"
+    "    chromStart int not null,   # Start coordinate\n"
+    "    val double not null,        # Value at coordinate\n"
+    "              #Indices\n"
+    "    PRIMARY KEY(chrom(%d),chromStart)\n"
+    ");\n"
+    , track, hGetMinIndexLength(db));
     sqlRemakeTable(conn, track, dy->string);
 
     /* Load main table and clean up file handle. */
     hgLoadTabFile(conn, tempDir, track, &f);
     hgRemoveTabFile(tempDir, track);
 
     /* If need be create meta table.  If need be delete old row. */
     if (!sqlTableExists(conn, "metaChromGraph"))
-	sqlUpdate(conn, metaCreateString);
+	{
+	dyStringClear(dy);
+	sqlDyStringPrintf(dy,
+	"CREATE TABLE metaChromGraph (\n"
+	"    name varchar(255) not null,        # Corresponds to chrom graph table name\n"
+	"    minVal double not null,    # Minimum value observed\n"
+	"    maxVal double not null,    # Maximum value observed\n"
+	"    binaryFile varchar(255) not null,  # Location of binary data point file if any\n"
+	"              #Indices\n"
+	"    PRIMARY KEY(name(32))\n"
+	");\n");
+	sqlUpdate(conn, dy->string);
+	}
     else
         {
 	dyStringClear(dy);
 	sqlDyStringPrintf(dy, "delete from metaChromGraph where name = '%s'", 
 		track);
 	sqlUpdate(conn, dy->string);
 	}
 
     /* Make chrom graph file */
     safef(path, sizeof(path), "%s.cgb", track);
     chromGraphToBin(list, path);
     safef(path, sizeof(path), "/gbdb/%s/chromGraph", db);
     pathPrefix = optionVal("pathPrefix", path);
     safef(gbdbPath, sizeof(gbdbPath), "%s/%s.cgb", pathPrefix, track);