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/hgCutters/hgCutters.c src/hg/makeDb/hgCutters/hgCutters.c index c05b10f..82b2a3d 100644 --- src/hg/makeDb/hgCutters/hgCutters.c +++ src/hg/makeDb/hgCutters/hgCutters.c @@ -1,122 +1,122 @@ /* Load up a GCG file. */ /* Copyright (C) 2013 The Regents of the University of California * See kent/LICENSE or http://genome.ucsc.edu/license/ for licensing information. */ #include "common.h" #include "dystring.h" #include "hash.h" #include "dnaseq.h" #include "dnautil.h" #include "dnaLoad.h" #include "options.h" #include "linefile.h" #include "bed.h" #include "cutter.h" char *tableName = "cutters"; struct slName *includes = NULL; struct slName *excludes = NULL; static struct optionSpec options[] = { {"tableName", OPTION_STRING}, {"overrides", OPTION_STRING}, {"justPrimary", OPTION_BOOLEAN}, {"createOnly", OPTION_BOOLEAN}, {NULL, 0} }; void usage() { errAbort("hgCutters - Load REBASE restriction enzymes GCG file into database.\n" "usage:\n" " hgCutters [options] database file.gcg\n" "options:\n" " -createOnly Just make the table without any rows.\n" " -justPrimary Just use the ones without semicolons in front (the primary ones).\n" " -overrides=file Include a file listing overrides to the set behavior. The file\n" " is formatted like:\n" " include enzyme\n" " exclude enzyme\n" " etc.\n" " -tableName=name Choose a different name for the loaded table. Default is \"cutters\"."); } void cutterTableCreate(struct sqlConnection *conn) /* Create a table. */ { static char *createString = "CREATE TABLE %s (\n" " name varchar(255) not null, # Name of Enzyme\n" " size int unsigned not null, # Size of recognition sequence\n" " matchSize int unsigned not null, # size without Ns\n" " seq varchar(255) not null, # Recognition sequence\n" " cut int unsigned not null, # Cut site on the plus strand\n" " overhang int not null, # Overhang\n" " palindromic tinyint unsigned not null, # 1 if it's panlidromic, 0 if not.\n" " semicolon tinyint unsigned not null, # 1 if it's from a REBASE record that has a semicolon in front, 0 if not.\n" " numSciz int unsigned not null, # Number of isoscizomers\n" " scizs longblob not null, # Names of isosizomers\n" " numCompanies int unsigned not null, # Number of companies selling this enzyme\n" " companies longblob not null, # Company letters\n" " numRefs int unsigned not null, # Number of references\n" " refs longblob not null # Reference numbers\n" ")\n"; -struct dyString *dy = newDyString(1024); +struct dyString *dy = dyStringNew(1024); sqlDyStringPrintf(dy, createString, tableName); sqlRemakeTable(conn, tableName, dy->string); dyStringFree(&dy); } void loadOverrides(char *overrides) /* Load overrides from file into the two global slName lists. */ { struct lineFile *lf = lineFileOpen(overrides,TRUE); char *words[2]; while (lineFileChop(lf,words)) { if (sameWord(words[0],"include")) slNameStore(&includes, words[1]); else if (sameWord(words[0],"exclude")) slNameStore(&excludes, words[1]); else errAbort("There's a problem with the overrides file."); } } void hgLoadCutters(char *database, struct cutter *cutters) /* Load table into database. */ { struct cutter *one; struct sqlConnection *conn = sqlConnect(database); cutterTableCreate(conn); if (!optionExists("createOnly")) for (one = cutters; one != NULL; one = one->next) { cutterSaveToDb(conn, one, tableName, 1024); } sqlDisconnect(&conn); } int main(int argc, char *argv[]) /* The program */ { struct cutter *cutters = NULL; char *overrides = NULL; optionInit(&argc, argv, options); if (argc != 3) usage(); cutters = readGcg(argv[2]); overrides = optionVal("overrides", NULL); if (overrides) loadOverrides(overrides); cullCutters(&cutters, !optionExists("justPrimary"), includes, 0); hgLoadCutters(argv[1], cutters); cutterFreeList(&cutters); slFreeList(&includes); slFreeList(&excludes); return 0; }