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/visiGene/vgLoadMahoney/vgLoadMahoney.c src/hg/visiGene/vgLoadMahoney/vgLoadMahoney.c index 5a860a5..40e6e13 100644 --- src/hg/visiGene/vgLoadMahoney/vgLoadMahoney.c +++ src/hg/visiGene/vgLoadMahoney/vgLoadMahoney.c @@ -184,70 +184,68 @@ safef(path, sizeof(path), "%s/%s", inFull, in->name); jpegSize(path,&imageWidth,&imageHeight); fprintf(f, "%d\t", imageWidth); fprintf(f, "%d", imageHeight); fprintf(f, "\n"); } } } carefulClose(&f); } void processWholeMounts(struct hash *mahoneyHash, char *inFull, char *outDir) /* Output database files for whole mounts. */ { -struct slName *imageFileList = NULL, *imageFile; +struct slName *imageFileList = NULL; char outRa[PATH_LEN], outTab[PATH_LEN]; imageFileList = listDir(inFull, "*.jpg"); safef(outRa, sizeof(outRa), "%s/whole.ra", outDir); wholeMountRa(outRa); safef(outTab, sizeof(outRa), "%s/whole.tab", outDir); wholeMountTab(imageFileList, inFull, mahoneyHash, outTab); } void slicesTab(struct slName *inList, char *inFull, struct hash *mahoneyHash, char *outFile) /* Output tab-delimited info on each item in inList. */ { struct slName *in; FILE *f = mustOpen(outFile, "w"); -char lastStage = 0; fprintf(f, "#fileName\tsubmitId\tgene\tlocusLink\trefSeq\tgenbank\tfPrimer\trPrimer\tage\tminAge\tmaxAge\tsectionSet\tsectionIx\timageWidth\timageHeight\n"); for (in = inList; in != NULL; in = in->next) { char hex[8]; char mtf[5]; - char stage = '1', sliceNo, nextSliceNo = '1'; + char stage = '1'; int id; struct mahoney *m; int imageWidth = 0, imageHeight = 0; char path[PATH_LEN]; strncpy(mtf, in->name+1, 4); mtf[4] = 0; id = atoi(mtf); safef(hex, sizeof(hex), "%x", id); m = hashFindVal(mahoneyHash, hex); if (m == NULL) warn("Can't find %d (%s) in hash while processing %s", id, mtf, in->name); else { if (anyGeneId(m)) { stage = in->name[5]; - sliceNo = in->name[6]; fprintf(f, "%s\t%d\t", in->name, id); fprintf(f, "%s\t", m->geneName); if (startsWith("NM_", m->genbank)) { fprintf(f, "%s\t", m->locusId); fprintf(f, "%s\t%s\t", m->genbank, m->genbank); } else { fprintf(f, "\t"); fprintf(f, "\t%s\t", m->genbank); } fprintf(f, "%s\t", m->fPrimer); fprintf(f, "%s\t", m->rPrimer); if (stage == '1') @@ -262,31 +260,31 @@ fprintf(f, "%d\t", imageWidth); fprintf(f, "%d", imageHeight); fprintf(f, "\n"); } } } carefulClose(&f); } void processSlices(struct hash *mahoneyHash, char *inFull, char *outDir) /* Output database files for slices. */ { -struct slName *imageFileList = NULL, *imageFile; +struct slName *imageFileList = NULL; char outRa[PATH_LEN], outTab[PATH_LEN]; imageFileList = listDir(inFull, "*.jpg"); safef(outRa, sizeof(outRa), "%s/slices.ra", outDir); slicesRa(outRa); safef(outTab, sizeof(outRa), "%s/slices.tab", outDir); slicesTab(imageFileList, inFull, mahoneyHash, outTab); } void fixPrimers(struct mahoney *el) /* Filter out bogus looking primers. */ { if (!isDna(el->fPrimer, strlen(el->fPrimer))) { static struct hash *uniqHash = NULL; @@ -339,52 +337,56 @@ /* Selected info from refLink table. */ { char *refSeq; /* Not allocated here */ char *geneName; int locusLink; }; struct hash *refGeneToNameHash(char *database) /* Create hash keyed by refSeq accession with refSeqInfo as value */ { struct sqlConnection *conn = sqlConnect(database); struct sqlResult *sr; char **row; struct hash *hash = newHash(17); -sr = sqlGetResult(conn, NOSQLINJ "select mrnaAcc,name,locusLinkId from refLink where name != ''"); +char query[1024]; +sqlSafef(query, sizeof query, "select mrnaAcc,name,locusLinkId from refLink where name != ''"); +sr = sqlGetResult(conn, query); while ((row = sqlNextRow(sr)) != NULL) { struct refSeqInfo *rsi; AllocVar(rsi); rsi->geneName = cloneString(row[1]); rsi->locusLink = atoi(row[2]); hashAddSaveName(hash, row[0], rsi, &rsi->refSeq); } sqlDisconnect(&conn); return hash; } struct hash *locusLinkToRefSeq(char *database) /* Create hash keyed by locusLink with refSeqInfo values. */ { struct sqlConnection *conn = sqlConnect(database); struct sqlResult *sr; char **row; struct hash *hash = newHash(17); -sr = sqlGetResult(conn, NOSQLINJ "select mrnaAcc,name,locusLinkId from refLink where locusLinkId != 0"); +char query[1024]; +sqlSafef(query, sizeof query, "select mrnaAcc,name,locusLinkId from refLink where locusLinkId != 0"); +sr = sqlGetResult(conn, query); while ((row = sqlNextRow(sr)) != NULL) { struct refSeqInfo *rsi; AllocVar(rsi); rsi->refSeq = cloneString(row[0]); rsi->geneName = cloneString(row[1]); rsi->locusLink = atoi(row[2]); hashAdd(hash, row[2], rsi); } sqlDisconnect(&conn); return hash; } struct rnaList @@ -492,31 +494,30 @@ struct mahoney *m; struct hash *refHash = refGeneToNameHash(mouseDb); struct hash *llHash = locusLinkToRefSeq(mouseDb); struct hash *pcrHash = mtfToRnaHash(pcrBed); int refWinsByPcr = 0, llWinsByPcr = 0, noWin = 0, bothWin = 0, noWinNoPcr = 0; int refWinsByName = 0, llWinsByName = 0; int refWinsByDefault = 0, llWinsByDefault = 0, noWinByDefault = 0; int locusLinkFromPcr = 0, genbankFromPcr = 0; for (m = mahoneyList; m != NULL; m = m->next) { char mtf[8]; char buf[9]; safef(mtf, sizeof(mtf), "%d", m->mtf); struct rnaList *rl; - boolean gotConflict = FALSE; rl = hashFindVal(pcrHash, mtf); /* Look for conflicts between refSeq and locusLink representations. * Use PCR and names to try to resolve them. (Ultimately this proves to * be futile though). On unresolved ones axe the locusLink and refSeq * id's */ if (startsWith("NM_", m->genbank) && m->locusId[0] != 0) { struct refSeqInfo *rsRsi = hashFindVal(refHash, m->genbank); struct refSeqInfo *llRsi = hashFindVal(llHash, m->locusId); boolean resolved = FALSE; if (rsRsi != NULL && llRsi != NULL) { if (rsRsi->locusLink == llRsi->locusLink) {