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/hgFindSpec/hgFindSpec.c src/hg/makeDb/hgFindSpec/hgFindSpec.c index 03e2874..c876155 100644 --- src/hg/makeDb/hgFindSpec/hgFindSpec.c +++ src/hg/makeDb/hgFindSpec/hgFindSpec.c @@ -185,63 +185,63 @@ hfsNext = hfs->next; if (! hashLookup(uniqHash, hfs->searchName)) { hashAdd(uniqHash, hfs->searchName, hfs); slAddHead(pSpecList, hfs); } } } void updateBigTextField(struct sqlConnection *conn, char *table, char *whereField, char *whereVal, char *textField, char *textVal) /* Generate sql code to update a big text field that may include * newlines and stuff. */ { -struct dyString *dy = newDyString(4096); -sqlDyStringPrintf(dy, "update %s set %s=", table, textField); -dyStringQuoteString(dy, '"', textVal); -dyStringPrintf(dy, " where %s = '%s'", whereField, whereVal); +struct dyString *dy = dyStringNew(4096); +sqlDyStringPrintf(dy, "update %s set %s='%s'", table, textField, textVal); +sqlDyStringPrintf(dy, " where %s = '%s'", whereField, whereVal); sqlUpdate(conn, dy->string); -freeDyString(&dy); +dyStringFree(&dy); } char *subTrackName(char *create, char *tableName) /* Substitute tableName for whatever is between CREATE TABLE and first '(' freez()'s create passed in. */ { char newCreate[strlen(create) + strlen(tableName) + 10]; char *front = NULL; char *rear = NULL; int length = (strlen(create) + strlen(tableName) + 10); /* try to find "TABLE" or "table" in create */ front = strstr(create, "TABLE"); if(front == NULL) front = strstr(create, "table"); if(front == NULL) errAbort("hgFindSpec::subTrackName() - Can't find 'TABLE' in %s", create); /* try to find first "(" in string */ rear = strstr(create, "("); if(rear == NULL) errAbort("hgFindSpec::subTrackName() - Can't find '(' in %s", create); /* set to NULL character after "TABLE" */ front += 5; *front = '\0'; -sqlSafef(newCreate, length , "%-s %s %-s", create, tableName, rear); +// create argument from trusted source .sql file on disk +safef(newCreate, length, "%s %s %s", create, tableName, rear); return cloneString(newCreate); } void layerOn(boolean strict, char *database, char *dir, struct hash *uniqHash, struct hash *htmlHash, boolean mustExist, struct hgFindSpec **hfsList) /* Read search specs from trackDb.ra from directory, * and layer them on top of whatever is in hfsList. */ { char raFile[512]; if (raName[0] != '/') safef(raFile, sizeof(raFile), "%s/%s", dir, raName); else safef(raFile, sizeof(raFile), "%s", raName); if (fileExists(raFile)) @@ -297,60 +297,62 @@ slSort(&hfsList, hgFindSpecCmp); if (verboseLevel() > 0) printf("Loaded %d search specs total\n", slCount(hfsList)); /* Write to tab-separated file. */ { FILE *f = mustOpen(tab, "w"); for (hfs = hfsList; hfs != NULL; hfs = hfs->next) hgFindSpecTabOut(hfs, f); carefulClose(&f); } /* Update database */ { char *create, *end; - char query[256]; + char query[2048]; struct sqlConnection *conn = sqlConnect(database); /* Load in table definition. */ readInGulp(sqlFile, &create, NULL); create = trimSpaces(create); create = subTrackName(create, hgFindSpecName); end = create + strlen(create)-1; if (*end == ';') *end = 0; - sqlRemakeTable(conn, hgFindSpecName, create); + sqlSafef(query, sizeof query, create, NULL); + sqlRemakeTable(conn, hgFindSpecName, query); /* Load in regular fields. */ sqlSafef(query, sizeof query, "load data local infile '%s' into table %s", tab, hgFindSpecName); sqlUpdate(conn, query); /* Load in settings fields. */ for (hfs = hfsList; hfs != NULL; hfs = hfs->next) { if (hfs->settingsHash != NULL) { char *settings = settingsFromHash(hfs->settingsHash); updateBigTextField(conn, hgFindSpecName, "searchName", hfs->searchName, "searchSettings", settings); freeMem(settings); } } - sqlUpdate(conn, NOSQLINJ "flush tables"); + sqlSafef(query, sizeof query, "flush tables"); + sqlUpdate(conn, query); sqlDisconnect(&conn); if (verboseLevel() > 0) printf("Loaded database %s\n", database); } unlink(tab); } void adjustTrackDbName(char *hgFindSpecName) /* Some hgFindSpec info is pulled from the trackDb table. When the * hgFindSpec name is hgFindSpec_$USER and the user's ~/.hg.conf file * specifies trackDb_$USER, that works fine. However, when the * hgFindSpec name is just hgFindSpec (as for make alpha / make strict * invocations), but ~/hg.conf says trackDb_$USER, they're inconsistent. * So to make a long story short -- circumvent the ~/.hg.conf! */ {