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/protein/kgXref2/kgXref2.c src/hg/protein/kgXref2/kgXref2.c index ab723ac..9d70811 100644 --- src/hg/protein/kgXref2/kgXref2.c +++ src/hg/protein/kgXref2/kgXref2.c @@ -62,117 +62,117 @@ emptyStr = strdup(""); sqlSafef(query2, sizeof query2, "select name, proteinID from %s.knownGene;", kgTempDb); sr2 = sqlMustGetResult(conn2, query2); row2 = sqlNextRow(sr2); while (row2 != NULL) { kgID = row2[0]; kgProteinID = row2[1]; refseqID = strdup(""); geneSymbol = strdup(""); desc = strdup(""); protAcc = strdup(""); - sqlSafefFrag(cond_str, sizeof cond_str, "displayID='%s'", kgProteinID); + sqlSafef(cond_str, sizeof cond_str, "displayID='%s'", kgProteinID); spID = sqlGetField(proteinsDb, "spXref3", "accession", cond_str); /* process variant splice proteins */ if (spID == NULL) { - sqlSafefFrag(cond_str, sizeof cond_str, "varAcc='%s'", kgProteinID); + sqlSafef(cond_str, sizeof cond_str, "varAcc='%s'", kgProteinID); spID = kgProteinID; parSpID = sqlGetField(proteinsDb, "splicProt", "parAcc", cond_str); if (parSpID != NULL) { - sqlSafefFrag(cond_str, sizeof cond_str, "accession='%s'", parSpID); + sqlSafef(cond_str, sizeof cond_str, "accession='%s'", parSpID); protDisplayId = sqlGetField(proteinsDb, "spXref3", "displayID", cond_str); } else { fprintf(stderr, "%s not found in kgXref3 nor in varProtein.\n", kgProteinID); exit(1); } } else { protDisplayId = kgProteinID; } /* use description for the protein as default, replace it with HUGO desc if available. */ - sqlSafefFrag(cond_str, sizeof cond_str, "displayID='%s'", protDisplayId); + sqlSafef(cond_str, sizeof cond_str, "displayID='%s'", protDisplayId); desc = sqlGetField(proteinsDb, "spXref3", "description", cond_str); if (strstr(kgID, "NM_") != NULL) { leg = 1; /* special processing for RefSeq DNA based genes */ - sqlSafefFrag(cond_str, sizeof cond_str, "mrnaAcc = '%s'", kgID); + sqlSafef(cond_str, sizeof cond_str, "mrnaAcc = '%s'", kgID); refSeqName = sqlGetField(ro_DB, "refLink", "name", cond_str); if (refSeqName != NULL) { geneSymbol = cloneString(refSeqName); refseqID = kgID; - sqlSafefFrag(cond_str, sizeof cond_str, "mrnaAcc = '%s'", kgID); + sqlSafef(cond_str, sizeof cond_str, "mrnaAcc = '%s'", kgID); desc = sqlGetField(ro_DB, "refLink", "product", cond_str); - sqlSafefFrag(cond_str, sizeof cond_str, "mrnaAcc='%s'", refseqID); + sqlSafef(cond_str, sizeof cond_str, "mrnaAcc='%s'", refseqID); answer = sqlGetField(ro_DB, "refLink", "protAcc", cond_str); if (answer != NULL) { protAcc = strdup(answer); } } } else { - sqlSafefFrag(cond_str, sizeof cond_str, "displayID = '%s'", protDisplayId); + sqlSafef(cond_str, sizeof cond_str, "displayID = '%s'", protDisplayId); hugoID = sqlGetField(proteinsDb, "spXref3", "hugoSymbol", cond_str); if (!((hugoID == NULL) || (*hugoID == '\0')) ) { leg = 21; geneSymbol = cloneString(hugoID); - sqlSafefFrag(cond_str, sizeof cond_str, "displayID = '%s'", protDisplayId); + sqlSafef(cond_str, sizeof cond_str, "displayID = '%s'", protDisplayId); desc = sqlGetField(proteinsDb, "spXref3", "hugoDesc", cond_str); if (desc == NULL) { printf("%s/%s don't have hugo desc ...\n", kgProteinID, protDisplayId); fflush(stdout); } } refseqID = emptyStr; protAcc = emptyStr; - sqlSafefFrag(cond_str, sizeof cond_str, "mrna = '%s'", kgID); + sqlSafef(cond_str, sizeof cond_str, "mrna = '%s'", kgID); answer = sqlGetField(ro_DB, "mrnaRefseq", "refseq", cond_str); if (answer != NULL) { refseqID = answer; } else { /*printf("%s does not have a related RefSeq.\n", kgID);fflush(stdout); */ } if (strlen(geneSymbol) == 0) { leg = 23; if (strlen(refseqID) != 0) { - sqlSafefFrag(cond_str, sizeof cond_str, "mrnaAcc = '%s'", refseqID); + sqlSafef(cond_str, sizeof cond_str, "mrnaAcc = '%s'", refseqID); answer = sqlGetField(ro_DB, "refLink", "name", cond_str); if (answer != NULL) { leg = 24; geneSymbol = strdup(answer); } } } } /* fix missing fields */ if (strlen(refseqID) == 0) { /* printf("%3d %s reseqID is empty.\n", leg, kgID); */ }