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/hgLoadOut/hgLoadOut.c src/hg/makeDb/hgLoadOut/hgLoadOut.c index 1f0c59b..fabd504 100644 --- src/hg/makeDb/hgLoadOut/hgLoadOut.c +++ src/hg/makeDb/hgLoadOut/hgLoadOut.c @@ -1,49 +1,30 @@ /* hgLoadOut - load RepeatMasker .out files into database. */ /* Copyright (C) 2014 The Regents of the University of California * See kent/LICENSE or http://genome.ucsc.edu/license/ for licensing information. */ #include "common.h" #include "linefile.h" #include "dystring.h" #include "options.h" #include "cheapcgi.h" #include "hCommon.h" #include "hdb.h" #include "jksql.h" #include "rmskOut.h" -char *createRmskOut = "CREATE TABLE %s (\n" -" bin smallint unsigned not null, # bin index field for range queries\n" -" swScore int unsigned not null, # Smith Waterman alignment score\n" -" milliDiv int unsigned not null, # Base mismatches in parts per thousand\n" -" milliDel int unsigned not null, # Bases deleted in parts per thousand\n" -" milliIns int unsigned not null, # Bases inserted in parts per thousand\n" -" genoName varchar(255) not null, # Genomic sequence name\n" -" genoStart int unsigned not null, # Start in genomic sequence\n" -" genoEnd int unsigned not null, # End in genomic sequence\n" -" genoLeft int not null, # -#bases after match in genomic sequence\n" -" strand char(1) not null, # Relative orientation + or -\n" -" repName varchar(255) not null, # Name of repeat\n" -" repClass varchar(255) not null, # Class of repeat\n" -" repFamily varchar(255) not null, # Family of repeat\n" -" repStart int not null, # Start (if strand is +) or -#bases after match (if strand is -) in repeat sequence\n" -" repEnd int not null, # End in repeat sequence\n" -" repLeft int not null, # -#bases after match (if strand is +) or start (if strand is -) in repeat sequence\n" -" id char(1) not null, # First digit of id field in RepeatMasker .out file. Best ignored.\n" -" #Indices\n"; boolean noBin = FALSE; boolean split = FALSE; boolean noSplit = FALSE; char *tabFileName = NULL; char *suffix = NULL; int badRepCnt = 0; /* command line option specifications */ static struct optionSpec optionSpecs[] = { {"tabFile", OPTION_STRING}, {"tabfile", OPTION_STRING}, {"nosplit", OPTION_BOOLEAN}, {"noSplit", OPTION_BOOLEAN}, {"split", OPTION_BOOLEAN}, @@ -246,47 +227,67 @@ r.repLeft = parenSignInt(words[13-wordOffset], lf); r.id[0] = ((wordCount > (14-wordOffset)) ? words[14-wordOffset][0] : ' '); if (checkRepeat(&r, lf)) { FILE *f = getFileForChrom(r.genoName); if (!noBin) fprintf(f, "%u\t", hFindBin(r.genoStart, r.genoEnd)); rmskOutTabOut(&r, f); } } } void loadOneTable(char *database, struct sqlConnection *conn, char *tempName, char *tableName) /* Load .tab file tempName into tableName and remove tempName. */ { -struct dyString *query = newDyString(1024); +struct dyString *query = dyStringNew(1024); verbose(1, "Loading up table %s\n", tableName); if (sqlTableExists(conn, tableName)) { sqlDyStringPrintf(query, "DROP table %s", tableName); sqlUpdate(conn, query->string); } /* Create first part of table definitions, the fields. */ dyStringClear(query); -sqlDyStringPrintf(query, createRmskOut, tableName); +sqlDyStringPrintf(query, +"CREATE TABLE %s (\n" +" bin smallint unsigned not null, # bin index field for range queries\n" +" swScore int unsigned not null, # Smith Waterman alignment score\n" +" milliDiv int unsigned not null, # Base mismatches in parts per thousand\n" +" milliDel int unsigned not null, # Bases deleted in parts per thousand\n" +" milliIns int unsigned not null, # Bases inserted in parts per thousand\n" +" genoName varchar(255) not null, # Genomic sequence name\n" +" genoStart int unsigned not null, # Start in genomic sequence\n" +" genoEnd int unsigned not null, # End in genomic sequence\n" +" genoLeft int not null, # -#bases after match in genomic sequence\n" +" strand char(1) not null, # Relative orientation + or -\n" +" repName varchar(255) not null, # Name of repeat\n" +" repClass varchar(255) not null, # Class of repeat\n" +" repFamily varchar(255) not null, # Family of repeat\n" +" repStart int not null, # Start (if strand is +) or -#bases after match (if strand is -) in repeat sequence\n" +" repEnd int not null, # End in repeat sequence\n" +" repLeft int not null, # -#bases after match (if strand is +) or start (if strand is -) in repeat sequence\n" +" id char(1) not null, # First digit of id field in RepeatMasker .out file. Best ignored.\n" +" #Indices\n" +, tableName); /* Create the indexes */ if (!noSplit) { - dyStringAppend(query, " INDEX(bin))\n"); + sqlDyStringPrintf(query, " INDEX(bin))\n"); } else { int indexLen = hGetMinIndexLength(database); sqlDyStringPrintf(query, " INDEX(genoName(%d),bin))\n", indexLen); } sqlUpdate(conn, query->string); /* Load database from tab-file. */ dyStringClear(query); sqlDyStringPrintf(query, "LOAD data local infile '%s' into table %s", tempName, tableName); sqlUpdate(conn, query->string); remove(tempName);