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/cgilib/hashJoin.c src/hg/cgilib/hashJoin.c index 0994857..7a92ee9 100644 --- src/hg/cgilib/hashJoin.c +++ src/hg/cgilib/hashJoin.c @@ -61,34 +61,34 @@ lmAllocArray(self->lm, self->colValues, valCount); int i; for (i = 0; i < valCount; i++) self->colValues[i] = dyStringNew(0); self->db = lmCloneString(self->lm, keyDtf->database); self->table = lmCloneString(self->lm, keyDtf->table); struct dyString *query = sqlDyStringCreate("select %s", keyDtf->field); struct joinerDtf *dtf; for (dtf = valDtfs; dtf != NULL; dtf = dtf->next) { if (differentString(dtf->database, self->db) || differentString(dtf->table, self->table)) errAbort("hashJoinNew: inconsistent key field (%s.%s.%s) and value field (%s.%s.%s)", keyDtf->database, keyDtf->table, keyDtf->field, dtf->database, dtf->table, dtf->field); - dyStringAppendC(query, ','); - dyStringAppend(query, dtf->field); + sqlDyStringPrintf(query, ","); + sqlDyStringPrintf(query, "%s", dtf->field); } -dyStringPrintf(query, " from %s", self->table); +sqlDyStringPrintf(query, " from %s", self->table); self->query = dyStringCannibalize(&query); self->naForMissing = naForMissing; return self; } struct hashJoin *hashJoinNext(struct hashJoin *el) /* Get the next hashJoin in a list of hashJoins. */ { return el->next; } struct hjAddOneContext // joinerFieldIterateKey context for use by hashJoinAddOne { struct hash *hash; @@ -97,31 +97,33 @@ static void hashJoinAddOne(void *context, char *key) /* Add values from context to hash from context for key. * This is a callback for joinerFieldIterateKey; context is struct hjAddOneContext *. */ { struct hjAddOneContext *ctx = context; hashAdd(ctx->hash, key, ctx->clonedValues); } static void hashJoinLoad(struct hashJoin *self) /* Load table contents into hash. */ { if (self->loaded) errAbort("hashJoinLoad: loaded flag already set"); struct sqlConnection *conn = hAllocConn(self->db); -int rowCount = sqlRowCount(conn, self->table); +char queryTblSafe[1024]; +sqlSafef(queryTblSafe, sizeof queryTblSafe, "%s", self->table); +int rowCount = sqlRowCount(conn, queryTblSafe); int hashSize = min(digitsBaseTwo(rowCount), hashMaxSize); self->hash = hashNew(hashSize); char **row; struct sqlResult *sr = sqlGetResult(conn, self->query); while ((row = sqlNextRow(sr)) != NULL) { char **clonedValues = lmCloneRow(self->lm, row+1, self->valCount); struct hjAddOneContext context = { self->hash, clonedValues }; // If necessary, process key according to self->jfA. if (self->jfB) joinerFieldIterateKey(self->jfB, hashJoinAddOne, &context, row[0]); else hashAdd(self->hash, row[0], clonedValues); } self->loaded = TRUE;