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/autoSql/tests/testSymCols.c src/hg/autoSql/tests/testSymCols.c
index 7c59b5b..ea63f7f 100644
--- src/hg/autoSql/tests/testSymCols.c
+++ src/hg/autoSql/tests/testSymCols.c
@@ -1,117 +1,121 @@
 /* Program to test symbolic columns (enum and set) in autoSql.  Requires
  * a writable database. */
 #include "common.h"
 #include "obscure.h"
 #include "linefile.h"
 #include "dystring.h"
 #include "jksql.h"
 #include "output/symTest.h"
 
 
 void usage()
 {
 errAbort("dbLinkTest - test symbolic columns (enum and set) in autoSql\n");
 }
 
 void writeTabFile(struct symTest *rows, char *outFile)
 /* write out rows to tab file */
 {
 struct symTest *row;
 FILE *fh = mustOpen(outFile, "w");
 for (row = rows; row != NULL; row = row->next)
     symTestTabOut(row, fh);
 carefulClose(&fh);
 }
 
 void testTabFile(char *inFile, char *outFile)
 /* test reading and writing a tab-separated file */
 {
 struct symTest *rows = symTestLoadAllByTab(inFile);
 writeTabFile(rows, outFile);
 symTestFreeList(&rows);
 }
 
 void testTabToCommaFile(char *inFile, char *outFile)
 /* create a comma file from a tab file */
 {
 struct symTest *rows = symTestLoadAllByTab(inFile);
 struct symTest *row;
 FILE *fh = mustOpen(outFile, "w");
 for (row = rows; row != NULL; row = row->next)
     symTestCommaOut(row, fh);
 carefulClose(&fh);
 symTestFreeList(&rows);
 }
 
 void testCommaToTabFile(char *inFile, char *outFile)
 /* create a tab file from a comma file */
 {
 char *buf;
 readInGulp(inFile, &buf, NULL);
 
 struct symTest *rows = NULL;
 char *recPtr = buf;
 while (*recPtr != '\0')
     slSafeAddHead(&rows, symTestCommaIn(&recPtr, NULL));
 freeMem(buf);
 
 slReverse(&rows);
 writeTabFile(rows, outFile);
 symTestFreeList(&rows);
 }
 
 char *loadSql(char *sqlFile)
 /* load sql from file, removing comments */
 {
 struct lineFile *lf = lineFileOpen(sqlFile, TRUE);
 struct dyString *sql = dyStringNew(0);
 char *line;
 while (lineFileNextReal(lf, &line)) 
     {
     char *h = strchr(line, '#');
     if (h != NULL)
         *h = '\0';
-    sqlDyStringPrintf(sql, "%-s", line); // trusting the input
+    dyStringPrintf(sql, "%s", line);
     }
 lineFileClose(&lf);
-return dyStringCannibalize(&sql);
+// TRUST input off disk file
+char query[4096];
+sqlSafef(query, sizeof query, sql->string, NULL);
+dyStringFree(&sql);
+return cloneString(query);
 }
 
 void testDb(char *testDb, char *testTbl, char* sqlFile,
             char *inFile, char *outFile)
 /* test loading and querying a database */
 {
 char *createSql = loadSql(sqlFile);
 struct sqlConnection *conn = sqlConnect(testDb);
 
 /* create a load */
 sqlRemakeTable(conn, testTbl, createSql);
 freeMem(createSql);
 sqlLoadTabFile(conn, inFile, testTbl, 0);
 
 /* query and save to file */
 struct symTest *rows = sqlQueryObjs(conn, (sqlLoadFunc)symTestLoad,
                                     sqlQueryMust|sqlQueryMulti,
                                     "select * from %s", testTbl);
 writeTabFile(rows, outFile);
 symTestFreeList(&rows);
 sqlDisconnect(&conn);
 }
 
 void doTests()
 /* test symbolic columns (enum and set) in autoSql */
 {
 testTabFile("input/symColsTest.tab", "output/symColsTest.tab");
 testTabToCommaFile("output/symColsTest.tab",  "output/symColsTest.comma");
 testCommaToTabFile("output/symColsTest.comma",  "output/symColsTestComma.tab");
 testDb("test", "symTest", "output/symTest.sql", "input/symColsTest.tab",
        "output/symColsTestDb.tab");
 }
 
 int main(int argc, char *argv[])
 {
 if(argc != 1)
     usage();
 doTests();
 return 0;
 }