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/outside/hgFiberglass/hgFiberglass.c src/hg/makeDb/outside/hgFiberglass/hgFiberglass.c index 8dd5e1e..53d7cc1 100644 --- src/hg/makeDb/outside/hgFiberglass/hgFiberglass.c +++ src/hg/makeDb/outside/hgFiberglass/hgFiberglass.c @@ -9,42 +9,30 @@ #include "jksql.h" #include "dystring.h" #include "bed.h" void usage() /* Explain usage and exit. */ { errAbort( "hgFiberglass - Turn Fiberglass Annotations into a BED and load into database\n" "usage:\n" " hgFiberglass database file\n" ); } -char *createString = -NOSQLINJ "CREATE TABLE fiberMouse (\n" - "chrom varchar(255) not null, # Human chromosome or FPC contig\n" - "chromStart int unsigned not null, # Start position in chromosome\n" - "chromEnd int unsigned not null, # End position in chromosome\n" - "name varchar(255) not null, # Name of other sequence\n" - "#Indices\n" - "INDEX(chrom(8),chromStart),\n" - "INDEX(chrom(8),chromEnd),\n" - "INDEX(name(12))\n" -")\n"; - int bedCmp(const void *va, const void *vb); void hgFiberglass(char *database, char *fileName) /* hgFiberglass - Turn Fiberglass Annotations into a BED and load into database. */ { struct sqlConnection *conn = sqlConnect(database); struct lineFile *lf = lineFileOpen(fileName, TRUE); char *tabName = "fiberMouse.tab"; FILE *f = mustOpen(tabName, "w"); char *row[3]; struct bed *bedList = NULL, *bed; char *ti; char query[256]; while (lineFileRow(lf, row)) @@ -61,30 +49,43 @@ if (!startsWith("ti|", ti)) errAbort("Trace doesn't start with ti| line %d of %s", lf->lineIx, lf->fileName); bed->name = cloneString(ti); slAddHead(&bedList, bed); } lineFileClose(&lf); printf("Loaded %d ecores from %s\n", slCount(bedList), fileName); slSort(&bedList, bedCmp); /* Write out tab-separated file. */ for (bed = bedList; bed != NULL; bed = bed->next) fprintf(f, "%s\t%d\t%d\t%s\n", bed->chrom, bed->chromStart, bed->chromEnd, bed->name); carefulClose(&f); printf("Loading database\n"); -sqlMaybeMakeTable(conn, "fiberMouse", createString); + +sqlSafef(query, sizeof query, +"CREATE TABLE fiberMouse (\n" + "chrom varchar(255) not null, # Human chromosome or FPC contig\n" + "chromStart int unsigned not null, # Start position in chromosome\n" + "chromEnd int unsigned not null, # End position in chromosome\n" + "name varchar(255) not null, # Name of other sequence\n" + "#Indices\n" + "INDEX(chrom(8),chromStart),\n" + "INDEX(chrom(8),chromEnd),\n" + "INDEX(name(12))\n" +")\n"); + +sqlMaybeMakeTable(conn, "fiberMouse", query); sqlSafef(query, sizeof query, "LOAD data local infile '%s' into table %s", tabName, "fiberMouse"); sqlUpdate(conn, query); sqlDisconnect(&conn); } int main(int argc, char *argv[]) /* Process command line. */ { cgiSpoof(&argc, argv); if (argc != 3) usage(); hgFiberglass(argv[1], argv[2]); return 0; }