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/utils/mysqlSecurityCheck/mysqlSecurityCheck.c src/hg/utils/mysqlSecurityCheck/mysqlSecurityCheck.c
index 61e789d..4982181 100644
--- src/hg/utils/mysqlSecurityCheck/mysqlSecurityCheck.c
+++ src/hg/utils/mysqlSecurityCheck/mysqlSecurityCheck.c
@@ -55,128 +55,133 @@
 /* get setting for specified config */
 {
 char temp[256];
 safef(temp, sizeof(temp), "%s.%s", config, setting);
 char *value = cfgOption(temp);
 if (!value)
     warn("setting %s not found!",temp);
 return value;
 }
 
 
 boolean mysqlCheckSecurityOfConfig(char *config)
 /* Can we connect? Can we access the mysql database? */
 {
 
+char query[1024];
 boolean problemFound = FALSE;
 
 if (
     sameString(config, "Xarchivecentral") ||
     sameString(config, "XcustomTracks")
     )
     {
     printf("Skipping %s for now.\n", config);
     }
 else
     {
     /* get connection info */
     database = getCfgOption(config, "db"      );
     host     = getCfgOption(config, "host"    );
     user     = getCfgOption(config, "user"    );
     password = getCfgOption(config, "password");
 
     //uglyf("database=%s\n", database);// DEBUG REMOVE
     //uglyf("host=%s\n", host);// DEBUG REMOVE
     //uglyf("user=%s\n", user);// DEBUG REMOVE
     //uglyf("password=%s\n", password);// DEBUG REMOVE
     // it seems to tolerate connecting to a NULL database?
 retry_it:
     conn = sqlMayConnectRemote(host, user, password, database);
 
     if (conn)
 	{
     	printf("Connected to %s.\n", config);
-	printf("select database() = [%s]\n", sqlQuickString(conn, NOSQLINJ "select database()"));
-	char *result = sqlQuickString(conn, NOSQLINJ "show databases like 'mysql'");
+	sqlSafef(query, sizeof query, "select database()");
+	printf("select database() = [%s]\n", sqlQuickString(conn, query));
+        sqlSafef(query, sizeof query, "show databases like 'mysql'");
+	char *result = sqlQuickString(conn, query);
 	printf("show databases like 'mysql' = [%s]\n", result);
 	if (result)
 	    problemFound = TRUE;
 	if (!problemFound)
 	    {
-	    char *result = sqlQuickString(conn, NOSQLINJ "SELECT table_name FROM INFORMATION_SCHEMA.TABLES WHERE table_schema = 'mysql'");
+	    sqlSafef(query, sizeof query, "SELECT table_name FROM INFORMATION_SCHEMA.TABLES WHERE table_schema = 'mysql'");
+	    char *result = sqlQuickString(conn, query);
 	    if (result)
 		{
 		problemFound = TRUE;
 		printf("INFORMATION_SCHEMA is allowing access to mysql db\n");
 		}
 	    else
 		{
 		printf("INFORMATION_SCHEMA is NOT allowing access to mysql db\n");
 		}
 	    }
 	/* Disabling this check. It actually shows information about mysql leaking out, but it does not give hackers access to passwords 
 	if (!problemFound)
 	    {
-	    char *result = sqlQuickString(conn, NOSQLINJ "SELECT table_name FROM INFORMATION_SCHEMA.TABLES WHERE table_name = 'user'");
+	    sqlSafef(query, sizeof query, "SELECT table_name FROM INFORMATION_SCHEMA.TABLES WHERE table_name = 'user'");
+	    char *result = sqlQuickString(conn, query);
 	    if (result)
 		{
 		problemFound = TRUE;
 		printf("INFORMATION_SCHEMA is allowing access to user table\n");
 		}
 	    else
 		{
 		printf("INFORMATION_SCHEMA is NOT allowing access to user table\n");
 		}
 	    }
 	*/
 	if (!problemFound)
 	    {
-	    char *query = NOSQLINJ "desc mysql.user";
+	    sqlSafef(query, sizeof query, "desc mysql.user");
 	    unsigned int errNo = 0;
 	    char *errMsg = NULL;
 	    struct sqlResult *rs = sqlGetResultExt(conn, query, &errNo, &errMsg);
 	    if (rs)
 		{
 		sqlFreeResult(&rs);
 		problemFound = TRUE;
 		printf("desc command is leaking access to mysql.user\n");
 		}
 	    else
 		{
 		printf("desc mysql.user returned errNo=%d errMsg=[%s]\n", errNo, errMsg);
 		}
 	    }
 	if (!problemFound)
 	    {
-	    char *query = NOSQLINJ "select * from mysql.user";
+	    sqlSafef(query, sizeof query, "select * from mysql.user");
 	    unsigned int errNo = 0;
 	    char *errMsg = NULL;
 	    struct sqlResult *rs = sqlGetResultExt(conn, query, &errNo, &errMsg);
 	    if (rs)
 		{
 		sqlFreeResult(&rs);
 		problemFound = TRUE;
 		printf("select * from mysql.user is leaking access to mysql database\n");
 		}
 	    else
 		{
 		printf("select * from mysql.user returned errNo=%d errMsg=[%s]\n", errNo, errMsg);
 		}
 	    }
 	if (!problemFound)
 	    {
-	    char *query = NOSQLINJ "use mysql";
+	    sqlSafef(query, sizeof query, "use mysql");
 	    unsigned int errNo = 0;
 	    char *errMsg = NULL;
 	    struct sqlResult *rs = sqlGetResultExt(conn, query, &errNo, &errMsg);
 	    if (errNo == 0)
 		{
 		sqlFreeResult(&rs);
 		problemFound = TRUE;
 		printf("use mysql is leaking access to mysql database\n");
 		}
 	    else
 		{
 		printf("use mysql returned errNo=%d errMsg=[%s]\n", errNo, errMsg);
 		}
 	    }
 	}