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/dbLinkTest.c src/hg/autoSql/tests/dbLinkTest.c
index 8ed814a..ba10ab4 100644
--- src/hg/autoSql/tests/dbLinkTest.c
+++ src/hg/autoSql/tests/dbLinkTest.c
@@ -35,49 +35,52 @@
 char *hdbDatabase = cfgOption("db.database");
 if(hdbDatabase == NULL)
     hdbDatabase = hDefaultDb();
 if(hdbHost == NULL || hdbUser == NULL || hdbPassword == NULL)
     errAbort("Cannot read in connection setting from configuration file.");
 conn = sqlConnectRemote(hdbHost, hdbUser, hdbPassword, hdbDatabase);
 return conn;
 }
 
 void setupTable(struct sqlConnection *conn) 
 /* Set up the autoTest table for testing. Call dropTable() later
  to remove table. */
 {
 struct lineFile *lf = lineFileOpen("output/newTest.sql", TRUE);
 char *update = NULL;
+char query[4096];
 char *line = NULL;
-struct dyString *ds = newDyString(256);
+struct dyString *ds = dyStringNew(256);
 if (sqlTableExists(conn, testTableName))
     errAbort("dbLinkTest.c::setupTable() - Table %s.%s already exists. Can't create another.",
              sqlGetDatabase(conn), testTableName);
 while(lineFileNextReal(lf, &line)) 
     {
     char *tmp = strstr(line, "not null");
     if(tmp != NULL)
 	{
 	*tmp = ',';
 	tmp++;
 	*tmp = '\0';
 	}
     subChar(line, '\t', ' ');
-    sqlDyStringPrintf(ds, "%-s", line);
+    dyStringPrintf(ds, "%s", line);
     }
 update = replaceChars(ds->string, "PRIMARY KEY", "UNIQUE");
-sqlUpdate(conn, update);
+// TRUST sql data off disk
+sqlSafef(query, sizeof query, update, NULL);
+sqlUpdate(conn, query);
 freez(&update);
 dyStringFree(&ds);
 lineFileClose(&lf);
 }
 
 void dropTable(struct sqlConnection *conn)
 /* Drop the test table that was created in setupTable(). */
 {
 char update[256];
 sqlSafef(update, sizeof(update), "drop table %s", testTableName);
 sqlUpdate(conn, update);
 }
 
 int countWcDiff(char *fileName)
 /** Count how many lines counted are reported by 'wc'. */
@@ -86,43 +89,43 @@
 char *line;
 int lineSize;
 int numLineDiff=0;
 char *row[3];
 lineFileNeedNext(lf, &line, &lineSize);
 line = trimSpaces(line);
 chopString(line, " ", row, 3);
 numLineDiff=atoi(row[0]);
 lineFileClose(&lf);
 return numLineDiff;
 }
 
 boolean testingFileIdentical(char *file1, char *file2)
 /** Check to see if two files are the same. */
 {
-struct dyString *command = newDyString(2048);
+struct dyString *command = dyStringNew(2048);
 char *resultsFile = "_testingFileIdentical.tmp";
 int result = 0;
 boolean identical = FALSE;
 int numDiff = 0;
 dyStringPrintf(command,  "diff -b %s %s | wc > %s", file1, file2,resultsFile );
 result = system(command->string);
 if(result != 0)
     errAbort("testing::testingFileIdentical() - Failed running diff on %s and %s", file1, file2);
 numDiff = countWcDiff(resultsFile);
 if(numDiff ==0)
     identical = TRUE;
-freeDyString(&command);
+dyStringFree(&command);
 remove(resultsFile);
 return identical;
 }
 
 struct autoTest *newAutoTestSample()
 /* Create an autotest structure and return it. */
 {
 struct autoTest *at = NULL;
 AllocVar(at);
 at->id=1;
 snprintf(at->shortName, sizeof(at->shortName), "shortName");
 at->longName = cloneString("autoSql autoTest longName");
 at->aliases[0] = cloneString("autoSql autoTest aliases");
 at->aliases[1] = cloneString("autoSql autoTest aliases");
 at->aliases[2] = cloneString("autoSql autoTest aliases");