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/makeDb/schema/dbSnoop/dbSnoop.c src/hg/makeDb/schema/dbSnoop/dbSnoop.c index d1cc999..723bc4e 100644 --- src/hg/makeDb/schema/dbSnoop/dbSnoop.c +++ src/hg/makeDb/schema/dbSnoop/dbSnoop.c @@ -251,33 +251,33 @@ int len = strlen(chrom->name); if (table[len] == '_') return table + len + 1; } } return NULL; } void unsplitTables(struct sqlConnection *conn, struct tableInfo **pList) /* Merge together information on tables that are split by chromosome. */ { /* Note, this routine leaks a little memory, but in this context that's * that's ok. */ struct tableInfo *ti, *newList = NULL, *next; struct hash *splitHash = hashNew(0); - -chromList = sqlQuickList(conn, - NOSQLINJ "select chrom from chromInfo order by length(chrom) desc"); +char query[1024]; +sqlSafef(query, sizeof query, "select chrom from chromInfo order by length(chrom) desc"); +chromList = sqlQuickList(conn, query); if (chromList == NULL) errAbort("Can't use unsplit because database has no chromInfo table"); for (ti = *pList; ti != NULL; ti = next) { char *pastChrom = findPastChrom(ti->name, chromList); next = ti->next; if (pastChrom != NULL) { struct tableInfo *splitTi; if ((splitTi = hashFindVal(splitHash, pastChrom)) != NULL) { splitTi->status->rows += ti->status->rows; splitTi->status->dataLength += ti->status->dataLength; splitTi->status->indexLength += ti->status->indexLength; @@ -445,31 +445,33 @@ // TODO could add support for other connection params like port, socket, ssl-params if (host != NULL || user != NULL || password != NULL) return sqlConnectRemoteFull(settings, database); else return sqlConnectProfile(profile, database); } void dbSnoop(char *database, char *output) /* dbSnoop - Produce an overview of a database.. */ { FILE *f = mustOpen(output, "w"); struct sqlConnection *conn = dbConnect(database); struct sqlConnection *conn2 = dbConnect(database); int majorVersion = sqlMajorVersion(conn); int minorVersion = sqlMinorVersion(conn); -struct sqlResult *sr = sqlGetResult(conn, NOSQLINJ "show table status"); +char query[1024]; +sqlSafef(query, sizeof query, "show table status"); +struct sqlResult *sr = sqlGetResult(conn, query); char **row; struct hash *fieldHash = hashNew(0); struct hash *typeHash = hashNew(0); struct hash *tableHash = hashNew(0); struct fieldInfo *fiList = NULL, *fi; struct tableInfo *tiList = NULL, *ti; struct tableType *ttList = NULL, *tt; long long totalData = 0, totalIndex = 0, totalRows = 0; int totalFields = 0; /* Collect info from database. */ while ((row = sqlNextRow(sr)) != NULL) { struct tableStatus *status; if ((majorVersion > 4) || ((4 == majorVersion) && (minorVersion > 0)))