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/hgLoadNet/hgLoadNet.c src/hg/makeDb/hgLoadNet/hgLoadNet.c
index 673d9b9..6130b2a 100644
--- src/hg/makeDb/hgLoadNet/hgLoadNet.c
+++ src/hg/makeDb/hgLoadNet/hgLoadNet.c
@@ -44,82 +44,84 @@
   "options:\n"
   "   -noBin   suppress bin field\n"
   "   -oldTable add to existing table\n"
   "   -sqlTable=table.sql Create table from .sql file\n"
   "   -qPrefix=xxx prepend \"xxx-\" to query name\n"
   "   -warn load even with missing fields\n"
   "   -test suppress loading table\n"
   );
 }
 
 
 void loadDatabase(char *database, char *tab, char *track)
 /* Load database from tab file. */
 {
 struct sqlConnection *conn = sqlConnect(database);
-struct dyString *dy = newDyString(1024);
+struct dyString *dy = dyStringNew(1024);
 /* First make table definition. */
 if (sqlTable != NULL)
     {
     /* Read from file. */
     char *sql, *s;
     readInGulp(sqlTable, &sql, NULL);
 
     /* Chop of end-of-statement semicolon if need be. */
     s = strchr(sql, ';');
     if (s != NULL) *s = 0;
     
     sqlRemakeTable(conn, track, sql);
     freez(&sql);
     }
 else if (!oldTable)
     {
     /* Create definition statement. */
     verbose(1, "Creating table definition for %s\n", track);
     sqlDyStringPrintf(dy, "CREATE TABLE %s (\n", track);
     if (!noBin)
-	dyStringAppend(dy, "  bin smallint unsigned not null,\n");
-    dyStringAppend(dy, "  level int unsigned not null,\n");
-    dyStringAppend(dy, "  tName varchar(255) not null,\n");
-    dyStringAppend(dy, "  tStart int unsigned not null,\n");
-    dyStringAppend(dy, "  tEnd int unsigned not null,\n");
-    dyStringAppend(dy, "  strand char(1) not null,\n");
-    dyStringAppend(dy, "  qName varchar(255) not null,\n");
-    dyStringAppend(dy, "  qStart int unsigned not null,\n");
-    dyStringAppend(dy, "  qEnd int unsigned not null,\n");
-    dyStringAppend(dy, "  chainId int unsigned not null,\n");
-    dyStringAppend(dy, "  ali int unsigned not null,\n");
-    dyStringAppend(dy, "  score double not null,\n");
-    dyStringAppend(dy, "  qOver int not null, \n");
-    dyStringAppend(dy, "  qFar int not null, \n");
-    dyStringAppend(dy, "  qDup int not null, \n");
-    dyStringAppend(dy, "  type varchar(255) not null,\n");
-    dyStringAppend(dy, "  tN int not null, \n");
-    dyStringAppend(dy, "  qN int not null, \n");
-    dyStringAppend(dy, "  tR int not null, \n");
-    dyStringAppend(dy, "  qR int not null, \n");
-    dyStringAppend(dy, "  tNewR int not null, \n");
-    dyStringAppend(dy, "  qNewR int not null, \n");
-    dyStringAppend(dy, "  tOldR int not null, \n");
-    dyStringAppend(dy, "  qOldR int not null, \n");
-    dyStringAppend(dy, "  tTrf int not null, \n");
-    dyStringAppend(dy, "  qTrf int not null, \n");
-    dyStringAppend(dy, "#Indices\n");
+	sqlDyStringPrintf(dy, "  bin smallint unsigned not null,\n");
+    sqlDyStringPrintf(dy,
+    "  level int unsigned not null,\n"
+    "  tName varchar(255) not null,\n"
+    "  tStart int unsigned not null,\n"
+    "  tEnd int unsigned not null,\n"
+    "  strand char(1) not null,\n"
+    "  qName varchar(255) not null,\n"
+    "  qStart int unsigned not null,\n"
+    "  qEnd int unsigned not null,\n"
+    "  chainId int unsigned not null,\n"
+    "  ali int unsigned not null,\n"
+    "  score double not null,\n"
+    "  qOver int not null, \n"
+    "  qFar int not null, \n"
+    "  qDup int not null, \n"
+    "  type varchar(255) not null,\n"
+    "  tN int not null, \n"
+    "  qN int not null, \n"
+    "  tR int not null, \n"
+    "  qR int not null, \n"
+    "  tNewR int not null, \n"
+    "  qNewR int not null, \n"
+    "  tOldR int not null, \n"
+    "  qOldR int not null, \n"
+    "  tTrf int not null, \n"
+    "  qTrf int not null, \n"
+    "#Indices\n");
     if (!noBin)
-	dyStringAppend(dy, "  INDEX(tName(16),bin),\n");
-    dyStringAppend(dy, "  INDEX(tName(16),tStart)\n");
-    dyStringAppend(dy, ")\n");
+	sqlDyStringPrintf(dy, "  INDEX(tName(16),bin),\n");
+    sqlDyStringPrintf(dy,
+    "  INDEX(tName(16),tStart)\n"
+    ")\n");
     sqlRemakeTable(conn, track, dy->string);
     }
 
 dyStringClear(dy);
 sqlDyStringPrintf(dy, "load data local infile '%s' into table %s", tab, track);
 verbose(1, "Loading %s into %s\n", track, database);
 sqlUpdate(conn, dy->string);
 /* add a comment to the history table and finish up connection */
 hgHistoryComment(conn, "Loaded net table %s", track);
 sqlDisconnect(&conn);
 }
 
 void cnWriteTables(char *chrom, struct cnFill *fillList, FILE *f, int depth)
 /* Recursively write out fill and gap lists. */
 {