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/hgKgMrna/hgKgMrna.c src/hg/makeDb/hgKgMrna/hgKgMrna.c index b7154c1..f7f5e88 100644 --- src/hg/makeDb/hgKgMrna/hgKgMrna.c +++ src/hg/makeDb/hgKgMrna/hgKgMrna.c @@ -21,84 +21,30 @@ boolean clTest = FALSE; boolean clDots = 100; char *proteinDB; void usage() /* Explain usage and exit. */ { errAbort( "hgKgMrna - Load mRNA alignments and other info into refGene tables\n" " into a TEMPORARY database to build Known Genes track.\n" "usage:\n" " hgKgMrna rn3Temp mrna.fa mrna.ra tight_mrna.psl loc2ref mrnaPep.fa mim2loc proteins070403\n"); } -char *refLinkTableDef = -NOSQLINJ "CREATE TABLE refLink (\n" -" name varchar(255) not null, # Name displayed in UI\n" -" product varchar(255) not null, # Name of protein product\n" -" mrnaAcc varchar(255) not null, # mRNA accession\n" -" protAcc varchar(255) not null, # protein accession\n" -" geneName int unsigned not null, # pointer to geneName table\n" -" prodName int unsigned not null, # pointer to product name table\n" -" locusLinkId int unsigned not null, # Locus Link ID\n" -" omimId int unsigned not null, # Locus Link ID\n" -" #Indices\n" -" PRIMARY KEY(mrnaAcc(12)),\n" -" index(name(10)),\n" -" index(protAcc(10)),\n" -" index(locusLinkId),\n" -" index(omimId),\n" -" index(prodName),\n" -" index(geneName)\n" -")"; - -char *refGeneTableDef = -NOSQLINJ "CREATE TABLE refGene ( \n" -" name varchar(255) not null, # mrna accession of gene \n" -" chrom varchar(255) not null, # Chromosome name \n" -" strand char(1) not null, # + or - for strand \n" -" txStart int unsigned not null, # Transcription start position \n" -" txEnd int unsigned not null, # Transcription end position \n" -" cdsStart int unsigned not null, # Coding region start \n" -" cdsEnd int unsigned not null, # Coding region end \n" -" exonCount int unsigned not null, # Number of exons \n" -" exonStarts longblob not null, # Exon start positions \n" -" exonEnds longblob not null, # Exon end positions \n" - " #Indices \n" -" INDEX(name(10)), \n" -" INDEX(chrom(12),txStart), \n" -" INDEX(chrom(12),txEnd) \n" -")"; - -char *refPepTableDef = -NOSQLINJ "CREATE TABLE refPep (\n" -" name varchar(255) not null, # Accession of sequence\n" -" seq longblob not null, # Peptide sequence\n" -" #Indices\n" -" PRIMARY KEY(name(32))\n" -")\n"; - -char *refMrnaTableDef = -NOSQLINJ "CREATE TABLE refMrna (\n" -" name varchar(255) not null, # Accession of sequence\n" -" seq longblob not null, # Nucleotide sequence\n" -" #Indices\n" -" PRIMARY KEY(name(32))\n" -")\n"; - struct hash *loadNameTable(struct sqlConnection *conn, char *tableName, int hashSize) /* Create a hash and load it up from table. */ { char query[128]; struct sqlResult *sr; char **row; struct hash *hash = newHash(hashSize); sqlSafef(query, sizeof query, "select id,name from %s", tableName); sr = sqlGetResult(conn, query); while ((row = sqlNextRow(sr)) != NULL) { hashAdd(hash, row[1], intToPt(sqlUnsigned(row[0]))); } @@ -339,40 +285,98 @@ struct sqlConnection *conn = hgStartUpdate(database); struct hash *productHash = loadNameTable(conn, "productName", 16); struct hash *geneHash = loadNameTable(conn, "geneName", 16); char *kgName = "refGene"; FILE *kgTab = hgCreateTabFile(".", kgName); FILE *productTab = hgCreateTabFile(".", "productName"); FILE *geneTab = hgCreateTabFile(".", "geneName"); FILE *refLinkTab = hgCreateTabFile(".", "refLink"); FILE *refPepTab = hgCreateTabFile(".", "refPep"); FILE *refMrnaTab = hgCreateTabFile(".", "refMrna"); struct exon *exonList = NULL, *exon; char *answer; char cond_str[200]; +char query[1024]; +sqlSafef(query, sizeof query, +"CREATE TABLE refLink (\n" +" name varchar(255) not null, # Name displayed in UI\n" +" product varchar(255) not null, # Name of protein product\n" +" mrnaAcc varchar(255) not null, # mRNA accession\n" +" protAcc varchar(255) not null, # protein accession\n" +" geneName int unsigned not null, # pointer to geneName table\n" +" prodName int unsigned not null, # pointer to product name table\n" +" locusLinkId int unsigned not null, # Locus Link ID\n" +" omimId int unsigned not null, # Locus Link ID\n" +" #Indices\n" +" PRIMARY KEY(mrnaAcc(12)),\n" +" index(name(10)),\n" +" index(protAcc(10)),\n" +" index(locusLinkId),\n" +" index(omimId),\n" +" index(prodName),\n" +" index(geneName)\n" +")"); /* Make refLink and other tables table if they don't exist already. */ -sqlMaybeMakeTable(conn, "refLink", refLinkTableDef); -sqlUpdate(conn, NOSQLINJ "delete from refLink"); -sqlMaybeMakeTable(conn, "refGene", refGeneTableDef); -sqlUpdate(conn, NOSQLINJ "delete from refGene"); -sqlMaybeMakeTable(conn, "refPep", refPepTableDef); -sqlUpdate(conn, NOSQLINJ "delete from refPep"); -sqlMaybeMakeTable(conn, "refMrna", refMrnaTableDef); -sqlUpdate(conn, NOSQLINJ "delete from refMrna"); +sqlMaybeMakeTable(conn, "refLink", query); +sqlSafef(query, sizeof query, "delete from refLink"); +sqlUpdate(conn, query); + +sqlSafef(query, sizeof query, +"CREATE TABLE refGene ( \n" +" name varchar(255) not null, # mrna accession of gene \n" +" chrom varchar(255) not null, # Chromosome name \n" +" strand char(1) not null, # + or - for strand \n" +" txStart int unsigned not null, # Transcription start position \n" +" txEnd int unsigned not null, # Transcription end position \n" +" cdsStart int unsigned not null, # Coding region start \n" +" cdsEnd int unsigned not null, # Coding region end \n" +" exonCount int unsigned not null, # Number of exons \n" +" exonStarts longblob not null, # Exon start positions \n" +" exonEnds longblob not null, # Exon end positions \n" + " #Indices \n" +" INDEX(name(10)), \n" +" INDEX(chrom(12),txStart), \n" +" INDEX(chrom(12),txEnd) \n" +")"); +sqlMaybeMakeTable(conn, "refGene", query); +sqlSafef(query, sizeof query, "delete from refGene"); +sqlUpdate(conn, query); + +sqlSafef(query, sizeof query, +"CREATE TABLE refPep (\n" +" name varchar(255) not null, # Accession of sequence\n" +" seq longblob not null, # Peptide sequence\n" +" #Indices\n" +" PRIMARY KEY(name(32))\n" +")\n"); +sqlMaybeMakeTable(conn, "refPep", query); +sqlSafef(query, sizeof query, "delete from refPep"); +sqlUpdate(conn, query); + +sqlSafef(query, sizeof query, +"CREATE TABLE refMrna (\n" +" name varchar(255) not null, # Accession of sequence\n" +" seq longblob not null, # Nucleotide sequence\n" +" #Indices\n" +" PRIMARY KEY(name(32))\n" +")\n"); +sqlMaybeMakeTable(conn, "refMrna", query); +sqlSafef(query, sizeof query, "delete from refMrna"); +sqlUpdate(conn, query); /* Scan through locus link to omim ID file and put in hash. */ { char *row[2]; printf("Scanning %s\n", mim2locFile); lf = lineFileOpen(mim2locFile, TRUE); while (lineFileRow(lf, row)) { hashAdd(loc2mimHash, row[1], intToPt(atoi(row[0]))); } lineFileClose(&lf); } /* Scan through .ra file and make up start of refSeqInfo @@ -469,31 +473,31 @@ printf("Missing protein accessions for %d of %d\n", noProtCount, rsiCount); /* Process alignments and write them out as genes. */ lf = pslFileOpen(pslFile); dotMod = 0; while ((psl = pslNext(lf)) != NULL) { if (hashFindVal(rsiHash, psl->qName) != NULL) { if (clDots > 0 && ++dotMod == clDots ) { dotMod = 0; dotOut(); } - sqlSafefFrag(cond_str, sizeof cond_str, "extAC='%s'", psl->qName); + sqlSafef(cond_str, sizeof cond_str, "extAC='%s'", psl->qName); answer = sqlGetField(proteinDB, "spXref2", "displayID", cond_str); if (answer == NULL) { fprintf(stderr, "%s NOT FOUND.\n", psl->qName); fflush(stderr); } if (answer != NULL) { struct genePred *gp = NULL; exonList = pslToExonList(psl); fprintf(kgTab, "%s\t%s\t%c\t%d\t%d\t", psl->qName, psl->tName, psl->strand[0], psl->tStart, psl->tEnd); rsi = hashMustFindVal(rsiHash, psl->qName);