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/visiGeneSearch/visiGeneSearch.c src/hg/visiGene/visiGeneSearch/visiGeneSearch.c index 2891ef7..1f7d020 100644 --- src/hg/visiGene/visiGeneSearch/visiGeneSearch.c +++ src/hg/visiGene/visiGeneSearch/visiGeneSearch.c @@ -67,31 +67,31 @@ { struct visiSearcher *next; /* Next search */ struct visiMatch *matchList; /* List of matching images. */ struct rbTree *tree; /* Tree for near random access. */ }; static struct visiSearcher *visiSearcherNew() /* Create a new, empty search structure. */ { struct visiSearcher *searcher; AllocVar(searcher); searcher->tree = rbTreeNew(visiMatchCmpImageId); return searcher; } -static void visiSearcherFree(struct visiSearcher **pSearcher) +void visiSearcherFree(struct visiSearcher **pSearcher) /* Free up memory associated with *pSearcher */ { struct visiSearcher *searcher = *pSearcher; if (searcher != NULL) { slFreeList(&searcher->matchList); rbTreeFree(&searcher->tree); freez(pSearcher); } } static struct visiMatch *visiSearcherAdd(struct visiSearcher *searcher, int imageId, double weight) /* Add given weight to match involving imageId, creating * a fresh match if necessary for imageId. */ @@ -194,41 +194,37 @@ /* Typedef of function to add things that are exact matches of * some type to searcher. This is broken out separately * so that the visiGeneMatchMultiWord function below can be * used in many contexts. */ static void visiGeneMatchMultiWord(struct visiSearcher *searcher, struct sqlConnection *conn, struct slName *wordList, char *table, AdderFunc adder) /* This helps cope with matches that may involve more than * one word. It will preferentially match as many words * as possible, and if there is a multiple-word match it * will take that over a single-word match. */ { struct slName *word; struct dyString *query = dyStringNew(0); -struct sqlResult *sr; -char **row; for (word = wordList; word != NULL; ) { struct slName *nameList, *name; int maxWordsUsed = 0; dyStringClear(query); - sqlDyStringPrintf(query, "select name from %s where name like \"", table); - dyStringAppend(query, word->name); - dyStringAppend(query, "%\""); + sqlDyStringPrintf(query, "select name from %s where name like '%s%%'", table, word->name); nameList = sqlQuickList(conn, query->string); if (nameList != NULL) { for (name = nameList; name != NULL; name = name->next) { int wordsUsed = countWordsUsedInPhrase(name->name, word); if (wordsUsed > maxWordsUsed) maxWordsUsed = wordsUsed; } } if (maxWordsUsed > 0) { for (name = nameList; name != NULL; name = name->next) { if (countWordsUsedInPhrase(name->name, word) == maxWordsUsed) @@ -250,31 +246,30 @@ { struct sqlResult *sr; char **row; sr = sqlGetResult(conn, query); while ((row = sqlNextRow(sr)) != NULL) visiSearcherAdd(searcher, sqlUnsigned(row[0]), 1.0); sqlFreeResult(&sr); } static void addImagesMatchingName(struct visiSearcher *searcher, struct sqlConnection *conn, struct dyString *dy, char *contributor) /* Add images that are contributed by given contributor to * searcher with a weight of one. Use dy for scratch space for * the query. */ { -int contributorId; dyStringClear(dy); sqlDyStringPrintf(dy, "select image.id from " "contributor,submissionContributor,imageFile,image " "where contributor.name = \"%s\" " "and contributor.id = submissionContributor.contributor " "and submissionContributor.submissionSet = imageFile.submissionSet " "and imageFile.id = image.imageFile" , contributor); addImagesMatchingQuery(searcher, conn, dy->string); } static void visiGeneMatchContributor(struct visiSearcher *searcher, struct sqlConnection *conn, struct slName *wordList) @@ -284,41 +279,37 @@ * names. We also want it so that if you include the initials * after the last name either with or without periods that will * set those matching the last name and initials. For * instance "Smith JJ" or "Smith J.J." or "Smith J. J." all would * match a particular John Jacob Smith, but not Francis K. Smith. * Making this a little more interesting is a case like * "smith li" which could either be two last names, or a last * name followed by initials. We would want to match both * cases. Finally, making it even more interesting, is the * case where the last name is compound, like "Van Koppen" or * "de la Cruz" and the like. Also don't forget the apostrophe * containing names like O'Shea. */ { struct slName *word; struct dyString *query = dyStringNew(0); -struct sqlResult *sr; -char **row; for (word = wordList; word != NULL; ) { struct slName *nameList, *name; int maxWordsUsed = 0; dyStringClear(query); - sqlDyStringPrintf(query, "select name from contributor where name like \""); - dyStringAppend(query, word->name); - dyStringAppend(query, " %\""); + sqlDyStringPrintf(query, "select name from contributor where name like '%s%%'", word->name); nameList = sqlQuickList(conn, query->string); if (nameList != NULL) { for (name = nameList; name != NULL; name = name->next) { int wordsUsed = countPartsUsedInName(name->name, word); if (wordsUsed > maxWordsUsed) maxWordsUsed = wordsUsed; } for (name = nameList; name != NULL; name = name->next) { if (countPartsUsedInName(name->name, word) == maxWordsUsed) addImagesMatchingName(searcher, conn, query, name->name); } while (--maxWordsUsed >= 0) @@ -339,31 +330,31 @@ struct slInt *image; for (image = imageList; image != NULL; image = image->next) visiSearcherAdd(searcher, image->val, 1.0); slFreeList(&imageList); } static void visiGeneMatchGene(struct visiSearcher *searcher, struct sqlConnection *conn, struct slName *wordList) /* Add images matching genes in wordList to searcher. * The wordList can include wildcards. */ { struct slName *word; for (word = wordList; word != NULL; word = word->next) { char *sqlPat = sqlLikeFromWild(word->name); - struct slInt *imageList, *image; + struct slInt *imageList; if (sqlWildcardIn(sqlPat)) imageList = visiGeneSelectNamed(conn, sqlPat, vgsLike); else imageList = visiGeneSelectNamed(conn, sqlPat, vgsExact); addImageListAndFree(searcher, imageList); freez(&sqlPat); } } static void visiGeneMatchAccession(struct visiSearcher *searcher, struct sqlConnection *conn, struct slName *wordList) /* Add images matching RefSeq, LocusLink, GenBank, or UniProt * accessions to searcher. */ { struct slName *word; @@ -385,136 +376,137 @@ dyStringClear(dy); sqlDyStringPrintf(dy, "select imageProbe.image from " "bodyPart,expressionLevel,imageProbe " "where bodyPart.name = \"%s\" " "and bodyPart.id = expressionLevel.bodyPart " "and expressionLevel.imageProbe = imageProbe.id" , bodyPart); addImagesMatchingQuery(searcher, conn, dy->string); dyStringClear(dy); sqlDyStringPrintf(dy, "select image.id from bodyPart,specimen,image " "where bodyPart.name = \"%s\" " "and bodyPart.id = specimen.bodyPart " - "and specimen.id = image.specimen"); + "and specimen.id = image.specimen" + , bodyPart); addImagesMatchingQuery(searcher, conn, dy->string); } static void visiGeneMatchBodyPart(struct visiSearcher *searcher, struct sqlConnection *conn, struct slName *wordList) /* Add images matching bodyPart to searcher. * This is a little complicated by some body parts containing * multiple words, like "choroid plexus". */ { visiGeneMatchMultiWord(searcher, conn, wordList, "bodyPart", addImagesMatchingBodyPart); } void addImagesMatchingStage(struct visiSearcher *searcher, struct sqlConnection *conn, int schemeId, int taxon, char *minAge) /* Given a developmental stage scheme (schemeId) and a specific * stage, return all images that match stage */ { struct dyString *dy = dyStringNew(0); char *maxAge; -struct sqlResult *sr; -char **row; dyStringClear(dy); sqlDyStringPrintf(dy, "select age from lifeStage where lifeStageScheme = %d ", schemeId); dyStringPrintf(dy, "and age > %s order by age", minAge); maxAge = sqlQuickString(conn, dy->string); dyStringClear(dy); sqlDyStringPrintf(dy, "select image.id from specimen,image "); dyStringPrintf(dy, "where specimen.age >= %s ", minAge); if (maxAge != NULL) - dyStringPrintf(dy, "and specimen.age < %s ", maxAge); -dyStringPrintf(dy, "and specimen.taxon = %d ", taxon); -dyStringPrintf(dy, "and specimen.id = image.specimen"); + sqlDyStringPrintf(dy, "and specimen.age < %s ", maxAge); +sqlDyStringPrintf(dy, "and specimen.taxon = %d ", taxon); +sqlDyStringPrintf(dy, "and specimen.id = image.specimen"); addImagesMatchingQuery(searcher, conn, dy->string); dyStringFree(&dy); } -static void visiGeneMatchStage(struct visiSearcher *searcher, +void visiGeneMatchStage(struct visiSearcher *searcher, struct sqlConnection *conn, struct slName *wordList) /* Add images matching Theiler or other developmental stage */ { struct slName *word; struct dyString *dy = dyStringNew(0); struct sqlResult *sr; char **row; for (word = wordList; word != NULL && word->next != NULL; word = word->next) { int schemeId = 0,taxon=0; dyStringClear(dy); sqlDyStringPrintf(dy, "select id,taxon from lifeStageScheme where name = \"%s\"", word->name); sr = sqlGetResult(conn, dy->string); if ((row = sqlNextRow(sr)) != NULL) { schemeId = sqlUnsigned(row[0]); taxon = sqlUnsigned(row[1]); } sqlFreeResult(&sr); if (schemeId > 0) { char *specificStage = word->next->name; char *minAge; dyStringClear(dy); sqlDyStringPrintf(dy, "select age from lifeStage where name = \"%s\" ", specificStage); - dyStringPrintf(dy, "and lifeStageScheme = %d\n", schemeId); + sqlDyStringPrintf(dy, "and lifeStageScheme = %d\n", schemeId); minAge = sqlQuickString(conn, dy->string); if (minAge != NULL) addImagesMatchingStage(searcher, conn, schemeId, taxon, minAge); freez(&minAge); } } dyStringFree(&dy); } static void addImagesMatchingYears(struct visiSearcher *searcher, struct sqlConnection *conn, int minYear, int maxYear) /* Fold in images that are published between given years */ { struct dyString *dy = dyStringNew(0); sqlDyStringPrintf(dy, "select image.id from submissionSet,imageFile,image " "where submissionSet.year >= %d and submissionSet.year <= %d " "and submissionSet.id = imageFile.submissionSet " "and imageFile.id = image.imageFile" , minYear, maxYear); addImagesMatchingQuery(searcher, conn, dy->string); dyStringFree(&dy); } static void visiGeneMatchYear(struct visiSearcher *searcher, struct sqlConnection *conn, struct slName *wordList) /* Fold in matches to a year. */ { struct slName *word; -char *now = sqlQuickString(conn, NOSQLINJ "select now()"); +char query[1024]; +sqlSafef(query, sizeof query, "select now()"); +char *now = sqlQuickString(conn, query); int currentYear = atoi(now); int maxYear = currentYear+1; /* May be slightly ahead of publication */ int minYear = 1988; /* Oldest record in Jackson Labs database. */ for (word = wordList; word != NULL; word = word->next) { char *name = word->name; if (strlen(name) == 4 && isdigit(name[0])) { int year = atoi(name); if (year >= minYear && year <= maxYear) addImagesMatchingYears(searcher, conn, year, year); } } }