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/lib/chromGraphFactory.c src/hg/lib/chromGraphFactory.c
index f2e214a..95c820a 100644
--- src/hg/lib/chromGraphFactory.c
+++ src/hg/lib/chromGraphFactory.c
@@ -460,31 +460,33 @@
     }
 for (i=dataStart, fileEl = fileList; i < colCount; i++, fileEl = fileEl->next)
     {
     char *label = row[i];
     if (!allWhite(label))
 	fileEl->label = cloneString(row[i]);
     }
 }
 
 static struct hash *chromInfoHash(struct sqlConnection *conn)
 /* Build up hash of chromInfo keyed by name */
 {
 struct sqlResult *sr;
 char **row;
 struct hash *hash = hashNew(0);
-sr = sqlGetResult(conn, NOSQLINJ "select * from chromInfo");
+char query[1024];
+sqlSafef(query, sizeof query, "select * from chromInfo");
+sr = sqlGetResult(conn, query);
 while ((row = sqlNextRow(sr)) != NULL)
     {
     struct chromInfo *ci = chromInfoLoad(row);
     hashAdd(hash, ci->chrom, ci);
     }
 sqlFreeResult(&sr);
 return hash;
 }
 
 static int cppNeedNum(struct customPp *cpp, char *words[], int wordIx)
 /* Make sure that words[wordIx] is an ascii integer, and return
  * binary representation of it. Conversion stops at first non-digit char. */
 {
 char *ascii = words[wordIx];
 char c = ascii[0];
@@ -644,42 +646,42 @@
     touppers(row[0]);
     touppers(row[1]);
     hashAdd(hash, row[0], lmCloneString(hash->lm, row[1]));
     }
 sqlFreeResult(&sr);
 return hash;
 }
 
 
 struct chromPos *snpIndexLookup(struct sqlConnection *conn, char *table, char *query, char *name)
 /* Lookup the marker name in the snp table using the name index.
  * As the snp table grew beyond 150 million rows, 
  * loading the entire table into a hash from the database became too slow. */
 {
 static struct chromPos pos;
-char fullQuery[256];
-sqlSafef(fullQuery, sizeof fullQuery, query, table);
-sqlSafefAppend(fullQuery, sizeof fullQuery, " where name='%s'", name);
+struct dyString *fullQuery = sqlDyStringCreate(query, table);
+sqlDyStringPrintf(fullQuery, " where name='%s'", name);
 struct sqlResult *sr;
 char **row;
-sr = sqlGetResult(conn, fullQuery);
+sr = sqlGetResult(conn, dyStringContents(fullQuery));
 if ((row = sqlNextRow(sr)) != NULL)
     {
     pos.chrom = cloneString(row[0]);
     pos.pos = sqlUnsigned(row[1]);
     }
 sqlFreeResult(&sr);
+dyStringFree(&fullQuery);
 if (!row)
     return NULL;
 return &pos;
 }
 
 void processDb(struct sqlConnection *conn,
 	struct customPp *cpp, int colCount, char *formatType, 
 	boolean firstLineLabels, struct labeledFile *fileList, 
 	char *table, char *query, char *aliasTable, char *aliasQuery,
 	boolean report, boolean isSnpTable)
 /* Process two column input file into chromGraph.  Treat first
  * column as a name to look up in bed-format table, which should
  * not be split. Return TRUE on success. */
 {
 struct hash *hash = NULL;