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/cirm/cdw/cdwFindSymlinkable/cdwFindSymlinkable.c src/hg/cirm/cdw/cdwFindSymlinkable/cdwFindSymlinkable.c
index 42172ce..eeec145 100644
--- src/hg/cirm/cdw/cdwFindSymlinkable/cdwFindSymlinkable.c
+++ src/hg/cirm/cdw/cdwFindSymlinkable/cdwFindSymlinkable.c
@@ -149,66 +149,66 @@
     return NULL;
     }
 if (symlinkLevels < 1)
     {
     warn("Too few symlinks followed: %d symlinks. Where is the symlink created by cdwSubmit?", symlinkLevels);
     return NULL;
     }
 *pPath = path;   // WE RETURN THIS EXTRA INFO
 return lastPath;
 }
 
 void cdwFindSymlinkable()
 /* Find files that have not been symlinked to cdw/ yet to save space. */
 {
 struct sqlConnection *conn = cdwConnectReadWrite();
-char query[512];
 
-sqlSafef(query, sizeof(query), "select id from cdwSubmit");
+struct dyString *query = sqlDyStringCreate("select id from cdwSubmit");
 
 // optional filtering
 if (idList && submitDirFilter)
     errAbort("You can use -idList or -submitDir, but not both at the same time.");
 
 if (idList)
     {
-    sqlSafefAppend(query, sizeof(query), " where id in (");
+    sqlDyStringPrintf(query, " where id in (");
     unsigned *idArray = NULL; 
     int idCount = 0;
     sqlUnsignedDynamicArray(idList, &idArray, &idCount);
     int i;
     for (i=0; i < idCount; ++i)
 	{
 	if (i > 0)
-	    sqlSafefAppend(query, sizeof(query), ",");
-	sqlSafefAppend(query, sizeof(query), "%d", idArray[i]);
+	    sqlDyStringPrintf(query, ",");
+	sqlDyStringPrintf(query, "%d", idArray[i]);
 	}
-    sqlSafefAppend(query, sizeof(query), ")");
+    sqlDyStringPrintf(query, ")");
     freeMem(idArray);
     }
 
 if (submitDirFilter)
     {
     char query2[512];
     sqlSafef(query2, sizeof(query2), "select id from cdwSubmitDir where url='%s'", submitDirFilter);
     int submitDirId = sqlQuickNum(conn, query2);
     if (submitDirId <= 0)
 	errAbort("%s not found", submitDirFilter);
-    sqlSafefAppend(query, sizeof(query), " where submitDirId = %d", submitDirId);
+    sqlDyStringPrintf(query, " where submitDirId = %d", submitDirId);
     }
 
-struct slInt *submitIdList = sqlQuickNumList(conn, query);
+struct slInt *submitIdList = sqlQuickNumList(conn, dyStringContents(query));
+dyStringFree(&query);
 verbose(1, "%d submits\n", slCount(submitIdList));
 
 if (inodeReport)
     {
     hash = newHash(12);
     }
 
 
 char *newPath = NULL;
 int filesMTimed = 0;
 int filesSymlinked = 0;
 off_t fileSpaceSaved = 0;
 struct stat sb;
 struct slInt *sel;
 for (sel = submitIdList; sel != NULL; sel = sel->next)
@@ -217,32 +217,33 @@
     verbose(1, "%d\n", sel->val); 
 
     struct cdwSubmit *es = cdwSubmitFromId(conn, sel->val);
     verbose(1, "url=%s submitDirId=%d manifestFileId=%d metaFileId=%d \n", 
 	es->url, es->submitDirId, es->manifestFileId, es->metaFileId);
     
     struct cdwSubmitDir *ed = cdwSubmitDirFromId(conn, es->submitDirId);
     if (!ed)
 	{
 	verbose(1, "submitDir record missing id=%d\n", es->submitDirId);
 	continue;
 	}
     verbose(1, "submitDir url=%s\n", ed->url);
 
     verbose(1, "files:\n");
-    sqlSafef(query, sizeof(query), "select id from cdwFile where submitId=%d", sel->val);
-    struct slInt *fileIdList = sqlQuickNumList(conn, query);
+    char query3[1024];
+    sqlSafef(query3, sizeof(query3), "select id from cdwFile where submitId=%d", sel->val);
+    struct slInt *fileIdList = sqlQuickNumList(conn, query3);
     verbose(1, "%d files in submission\n", slCount(fileIdList));
     struct slInt *el;
     for (el = fileIdList; el != NULL; el = el->next)
 	{
 	verbose(1, "fileId %d\n", el->val); 
 	struct cdwFile *ef = cdwFileFromId(conn, el->val);
 	if (!ef->cdwFileName)
 	    {
 	    verbose(1, "ef->cdwFileName is null for file id=%d\n", el->val);
 	    continue;
 	    }
 	char *path = cdwPathForFileId(conn, el->val);
 	verbose(1, "id=%u, submitFileName=%s path=%s cdwFileName=%s\n", 
 	    ef->id, ef->submitFileName, path, ef->cdwFileName);