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/hgClonePos/hgClonePos.c src/hg/makeDb/hgClonePos/hgClonePos.c index cf282e6..0ad0553 100644 --- src/hg/makeDb/hgClonePos/hgClonePos.c +++ src/hg/makeDb/hgClonePos/hgClonePos.c @@ -24,45 +24,30 @@ " hgClonePos [options] database ooDir ffa/sequence.inf gsDir\n" "options:\n" " -chromLst=chrom.lst - chromosomes subdirs are named in chrom.lst (1, 2, ...)\n" " -maxErr=N set maximum allowed errors before aborting (default 0)\n" " -maxWarn=N set maximum number of warnings to print (default 10)\n" " -verbose=N set verbose level to N\n" "example:\n" " cd /cluster/store5/gs.18/build35\n" " hgClonePos -chromLst=chrom.lst hg17 build35 ./sequence.inf \\\n" " /cluster/store5/gs.18 -maxErr=3 -maxWarn=2000"); } int maxErr = 0; int maxWarn = 10; -char *createClonePos = -NOSQLINJ "CREATE TABLE clonePos (\n" -" name varchar(255) not null, # Name of clone including version\n" -" seqSize int unsigned not null, # base count not including gaps\n" -" phase tinyint unsigned not null, # htg phase\n" -" chrom varchar(255) not null, # Chromosome name\n" -" chromStart int unsigned not null, # Start in chromosome\n" -" chromEnd int unsigned not null, # End in chromosome\n" -" stage char(1) not null, # F/D/P for finished/draft/predraft\n" -" faFile varchar(255) not null, # File with sequence.\n" -" #Indices\n" -" INDEX(name(12)),\n" -" INDEX(chrom(12),chromStart)\n" -")\n"; - void addCloneInfo(char *glFileName, struct hash *cloneHash, struct clonePos **pCloneList) /* Add in clone info from one .gl file. */ { char dir[256], chrom[128], ext[64]; struct gl gl; struct lineFile *lf = lineFileOpen(glFileName, TRUE); struct clonePos *clone; char *line, *words[8]; int lineSize, wordCount; char cloneVerName[128]; char cloneName[128]; printf("Processing %s\n", glFileName); splitPath(glFileName, dir, chrom, ext); while (lineFileNext(lf, &line, &lineSize)) @@ -290,45 +275,64 @@ } } if (errCount > maxErr) { errAbort("Aborting with %d errors\n", errCount); } } void saveClonePos(struct clonePos *cloneList, char *database) /* Save sorted clone position list to database. */ { struct sqlConnection *conn = sqlConnect(database); struct clonePos *clone; struct tempName tn; FILE *f; -struct dyString *ds = newDyString(2048); +struct dyString *ds = dyStringNew(2048); /* Create tab file from clone list. */ printf("Creating tab file\n"); makeTempName(&tn, "hgCP", ".tab"); f = mustOpen(tn.forCgi, "w"); for (clone = cloneList; clone != NULL; clone = clone->next) clonePosTabOut(clone, f); fclose(f); /* Create table if it doesn't exist, delete whatever is * already in it, and fill it up from tab file. */ printf("Loading clonePos table\n"); -sqlMaybeMakeTable(conn, "clonePos", createClonePos); -sqlUpdate(conn, NOSQLINJ "DELETE from clonePos"); + +char query[1024]; +sqlSafef(query, sizeof query, +"CREATE TABLE clonePos (\n" +" name varchar(255) not null, # Name of clone including version\n" +" seqSize int unsigned not null, # base count not including gaps\n" +" phase tinyint unsigned not null, # htg phase\n" +" chrom varchar(255) not null, # Chromosome name\n" +" chromStart int unsigned not null, # Start in chromosome\n" +" chromEnd int unsigned not null, # End in chromosome\n" +" stage char(1) not null, # F/D/P for finished/draft/predraft\n" +" faFile varchar(255) not null, # File with sequence.\n" +" #Indices\n" +" INDEX(name(12)),\n" +" INDEX(chrom(12),chromStart)\n" +")\n"); + +sqlMaybeMakeTable(conn, "clonePos", query); + +sqlSafef(query, sizeof query, "DELETE from clonePos"); +sqlUpdate(conn, query); sqlDyStringPrintf(ds, "LOAD data local infile '%s' into table clonePos", tn.forCgi); sqlUpdate(conn, ds->string); /* Clean up. */ remove(tn.forCgi); sqlDisconnect(&conn); } void hgClonePos(char *database, char *ooDir, char *seqInfoName, char *gsDir) /* hgClonePos - create clonePos table in browser database. */ { struct hash *cloneHash = newHash(16); struct clonePos *cloneList = NULL;