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/sqlUpdateRelated/sqlUpdateRelated.c src/hg/sqlUpdateRelated/sqlUpdateRelated.c
index e40a2cb..4fb5c03 100644
--- src/hg/sqlUpdateRelated/sqlUpdateRelated.c
+++ src/hg/sqlUpdateRelated/sqlUpdateRelated.c
@@ -356,67 +356,67 @@
 		char *endField = strchr(startField, '@');
 		// This is parsed out so we know it works until someone rearranged code
 		assert(endField != NULL);  
 		field = cloneStringZ(startField, endField-startField);
 		}
 	    }
 
 	// We already dealt with the question mark outside this loop
 	if (firstChar == '?')
 	    field += 1;
 
 
 	if (firstTime)
 	    firstTime = !firstTime;
 	else
-	    dyStringAppendC(sql, ',');
-	dyStringAppend(sql, field);
+	    sqlDyStringPrintf(sql, ",");
+	sqlDyStringPrintf(sql, "%s", field);
 	}
 
     /* Now generate the values bit */
-    dyStringAppend(sql, ") values (");
+    sqlDyStringPrintf(sql, ") values (");
     firstTime = TRUE;
     for (fieldIx=0; fieldIx < inTable->fieldCount; ++fieldIx)
         {
 	char *field = inFields[fieldIx];
 	char firstChar = field[0];
 
 	if (firstChar == '@')
 	    {
 	    if (field[1] == '@')  // multi field
 		{
 		continue;	// multi field output doesn't go into this table, just relationship
 		}
 	    else
 	        field += 1;	// We val with the foreign key here, just skip over '@'
 	    }
 	if (firstChar == '?')
 	    field += 1;
 	if (firstTime)
 	    firstTime = !firstTime;
 	else
-	    dyStringAppendC(sql, ',');
+	    sqlDyStringPrintf(sql, ",");
 	char *rawVal = row[fieldIx];
 	char *uncsvVal = rawVal;
 	if (uncsv)
 	    uncsvVal = csvParseNext(&rawVal, csvScratch);
 	char *val = emptyForNull(uncsvVal);
 	char *escaped = sqlEscapeString(val);
 	dyStringPrintf(sql, "\"%s\"",  escaped);
 	freez(&escaped);
 	}
-    dyStringAppendC(sql, ')');
+    sqlDyStringPrintf(sql, ")");
     verbose(2, "update sql: %s\n", sql->string);
     sqlUpdate(conn, sql->string);
     int mainTableId = sqlLastAutoId(conn);
 
     /* Handle multi-multi stuff */
     struct multiRef *mRef;
     for (mRef = multiRefList; mRef != NULL; mRef = mRef->next)
         {
 	addMultiRelation(conn, mRef, fr, mainTableId, tabFile, csvScratch);
 	}
 
     /* Clean up strings allocated for field references */
     for (fRef = foreignRefList; fRef != NULL; fRef = fRef->next)
 	freez(&fRef->foreignKey);
     }