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/lib/customTrack.c src/hg/lib/customTrack.c index bdab661..d0c57b4 100644 --- src/hg/lib/customTrack.c +++ src/hg/lib/customTrack.c @@ -53,31 +53,31 @@ tdb->shortLabel = cloneString(CT_DEFAULT_TRACK_NAME); tdb->longLabel = cloneString(CT_DEFAULT_TRACK_DESCR); tdb->table = customTrackTableFromLabel(tdb->shortLabel); tdb->track = cloneString(tdb->table); tdb->visibility = tvDense; tdb->grp = cloneString("user"); tdb->type = (char *)NULL; tdb->canPack = 2; /* unknown -- fill in later when type is known */ return tdb; } static void createMetaInfo(struct sqlConnection *conn) /* create the metaInfo table in customTrash db */ { -struct dyString *dy = newDyString(1024); +struct dyString *dy = dyStringNew(1024); sqlDyStringPrintf(dy, "CREATE TABLE %s (\n" "name varchar(255) not null,\n" "useCount int not null,\n" "lastUse datetime not null,\n" "PRIMARY KEY(name)\n" ")\n", CT_META_INFO); sqlUpdate(conn,dy->string); dyStringFree(&dy); } void ctTouchLastUse(struct sqlConnection *conn, char *table, boolean status) /* for status==TRUE - update metaInfo information for table * for status==FALSE - delete entry for table from metaInfo table @@ -244,31 +244,33 @@ } track->needsLift = FALSE; } } void customTrackHandleLift(char *db, struct customTrack *ctList) /* lift any tracks with contig coords */ { if (!customTrackNeedsLift(ctList)) return; /* Load up hash of contigs and lift up tracks. */ struct hash *ctgHash = newHash(0); struct ctgPos *ctg, *ctgList = NULL; struct sqlConnection *conn = hAllocConn(db); -struct sqlResult *sr = sqlGetResult(conn, NOSQLINJ "select * from ctgPos"); +char query[1024]; +sqlSafef(query, sizeof query, "select * from ctgPos"); +struct sqlResult *sr = sqlGetResult(conn, query); char **row; while ((row = sqlNextRow(sr)) != NULL) { ctg = ctgPosLoad(row); slAddHead(&ctgList, ctg); hashAdd(ctgHash, ctg->contig, ctg); } customTrackLift(ctList, ctgHash); ctgPosFreeList(&ctgList); hashFree(&ctgHash); sqlFreeResult(&sr); hFreeConn(&conn); } static int ct_nextDefaultTrackNum = 1;