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/gv.c src/hg/cgilib/gv.c index cb3e530..ffbd4b3 100644 --- src/hg/cgilib/gv.c +++ src/hg/cgilib/gv.c @@ -94,35 +94,35 @@ el = gvLoad(row); slAddHead(&list, el); } slReverse(&list); sqlFreeResult(&sr); return list; } void gvSaveToDb(struct sqlConnection *conn, struct gv *el, char *tableName, int updateSize) /* Save gv as a row to the table specified by tableName. * As blob fields may be arbitrary size updateSize specifies the approx size * of a string that would contain the entire query. Arrays of native types are * converted to comma separated strings and loaded as such, User defined types are * inserted as NULL. Strings are automatically escaped to allow insertion into the database. */ { -struct dyString *update = newDyString(updateSize); +struct dyString *update = dyStringNew(updateSize); sqlDyStringPrintf(update, "insert into %s values ( '%s','%s','%s','%s','%s',%u)", tableName, el->id, el->name, el->srcId, el->baseChangeType, el->location, el->coordinateAccuracy); sqlUpdate(conn, update->string); -freeDyString(&update); +dyStringFree(&update); } struct gv *gvCommaIn(char **pS, struct gv *ret) /* Create a gv out of a comma separated string. * This will fill in ret if non-null, otherwise will * return a new gv */ { char *s = *pS; if (ret == NULL) AllocVar(ret); ret->id = sqlStringComma(&s); ret->name = sqlStringComma(&s); ret->srcId = sqlStringComma(&s); @@ -274,35 +274,35 @@ el = gvPosLoad(row); slAddHead(&list, el); } slReverse(&list); sqlFreeResult(&sr); return list; } void gvPosSaveToDb(struct sqlConnection *conn, struct gvPos *el, char *tableName, int updateSize) /* Save gvPos as a row to the table specified by tableName. * As blob fields may be arbitrary size updateSize specifies the approx size * of a string that would contain the entire query. Arrays of native types are * converted to comma separated strings and loaded as such, User defined types are * inserted as NULL. Strings are automatically escaped to allow insertion into the database. */ { -struct dyString *update = newDyString(updateSize); +struct dyString *update = dyStringNew(updateSize); sqlDyStringPrintf(update, "insert into %s values ( %u,'%s',%u,%u,'%s','%s','%s')", tableName, el->bin, el->chrom, el->chromStart, el->chromEnd, el->name, el->strand, el->label); sqlUpdate(conn, update->string); -freeDyString(&update); +dyStringFree(&update); } struct gvPos *gvPosCommaIn(char **pS, struct gvPos *ret) /* Create a gvPos out of a comma separated string. * This will fill in ret if non-null, otherwise will * return a new gvPos */ { char *s = *pS; if (ret == NULL) AllocVar(ret); ret->bin = sqlUnsignedComma(&s); ret->chrom = sqlStringComma(&s); ret->chromStart = sqlUnsignedComma(&s); @@ -444,35 +444,35 @@ el = gvSrcLoad(row); slAddHead(&list, el); } slReverse(&list); sqlFreeResult(&sr); return list; } void gvSrcSaveToDb(struct sqlConnection *conn, struct gvSrc *el, char *tableName, int updateSize) /* Save gvSrc as a row to the table specified by tableName. * As blob fields may be arbitrary size updateSize specifies the approx size * of a string that would contain the entire query. Arrays of native types are * converted to comma separated strings and loaded as such, User defined types are * inserted as NULL. Strings are automatically escaped to allow insertion into the database. */ { -struct dyString *update = newDyString(updateSize); +struct dyString *update = dyStringNew(updateSize); sqlDyStringPrintf(update, "insert into %s values ( '%s','%s','%s')", tableName, el->srcId, el->src, el->lsdb); sqlUpdate(conn, update->string); -freeDyString(&update); +dyStringFree(&update); } struct gvSrc *gvSrcCommaIn(char **pS, struct gvSrc *ret) /* Create a gvSrc out of a comma separated string. * This will fill in ret if non-null, otherwise will * return a new gvSrc */ { char *s = *pS; if (ret == NULL) AllocVar(ret); ret->srcId = sqlStringComma(&s); ret->src = sqlStringComma(&s); ret->lsdb = sqlStringComma(&s); @@ -599,35 +599,35 @@ el = gvAttrLoad(row); slAddHead(&list, el); } slReverse(&list); sqlFreeResult(&sr); return list; } void gvAttrSaveToDb(struct sqlConnection *conn, struct gvAttr *el, char *tableName, int updateSize) /* Save gvAttr as a row to the table specified by tableName. * As blob fields may be arbitrary size updateSize specifies the approx size * of a string that would contain the entire query. Arrays of native types are * converted to comma separated strings and loaded as such, User defined types are * inserted as NULL. Strings are automatically escaped to allow insertion into the database. */ { -struct dyString *update = newDyString(updateSize); +struct dyString *update = dyStringNew(updateSize); sqlDyStringPrintf(update, "insert into %s values ( '%s','%s','%s')", tableName, el->id, el->attrType, el->attrVal); sqlUpdate(conn, update->string); -freeDyString(&update); +dyStringFree(&update); } struct gvAttr *gvAttrCommaIn(char **pS, struct gvAttr *ret) /* Create a gvAttr out of a comma separated string. * This will fill in ret if non-null, otherwise will * return a new gvAttr */ { char *s = *pS; if (ret == NULL) AllocVar(ret); ret->id = sqlStringComma(&s); ret->attrType = sqlStringComma(&s); ret->attrVal = sqlStringComma(&s); @@ -758,35 +758,35 @@ el = gvLinkLoad(row); slAddHead(&list, el); } slReverse(&list); sqlFreeResult(&sr); return list; } void gvLinkSaveToDb(struct sqlConnection *conn, struct gvLink *el, char *tableName, int updateSize) /* Save gvLink as a row to the table specified by tableName. * As blob fields may be arbitrary size updateSize specifies the approx size * of a string that would contain the entire query. Arrays of native types are * converted to comma separated strings and loaded as such, User defined types are * inserted as NULL. Strings are automatically escaped to allow insertion into the database. */ { -struct dyString *update = newDyString(updateSize); +struct dyString *update = dyStringNew(updateSize); sqlDyStringPrintf(update, "insert into %s values ( '%s','%s','%s','%s','%s')", tableName, el->id, el->attrType, el->raKey, el->acc, el->displayVal); sqlUpdate(conn, update->string); -freeDyString(&update); +dyStringFree(&update); } struct gvLink *gvLinkCommaIn(char **pS, struct gvLink *ret) /* Create a gvLink out of a comma separated string. * This will fill in ret if non-null, otherwise will * return a new gvLink */ { char *s = *pS; if (ret == NULL) AllocVar(ret); ret->id = sqlStringComma(&s); ret->attrType = sqlStringComma(&s); ret->raKey = sqlStringComma(&s); @@ -925,35 +925,35 @@ el = gvAttrLongLoad(row); slAddHead(&list, el); } slReverse(&list); sqlFreeResult(&sr); return list; } void gvAttrLongSaveToDb(struct sqlConnection *conn, struct gvAttrLong *el, char *tableName, int updateSize) /* Save gvAttrLong as a row to the table specified by tableName. * As blob fields may be arbitrary size updateSize specifies the approx size * of a string that would contain the entire query. Arrays of native types are * converted to comma separated strings and loaded as such, User defined types are * inserted as NULL. Strings are automatically escaped to allow insertion into the database. */ { -struct dyString *update = newDyString(updateSize); +struct dyString *update = dyStringNew(updateSize); sqlDyStringPrintf(update, "insert into %s values ( '%s','%s',%s)", tableName, el->id, el->attrType, el->attrVal); sqlUpdate(conn, update->string); -freeDyString(&update); +dyStringFree(&update); } struct gvAttrLong *gvAttrLongCommaIn(char **pS, struct gvAttrLong *ret) /* Create a gvAttrLong out of a comma separated string. * This will fill in ret if non-null, otherwise will * return a new gvAttrLong */ { char *s = *pS; if (ret == NULL) AllocVar(ret); ret->id = sqlStringComma(&s); ret->attrType = sqlStringComma(&s); ret->attrVal = sqlStringComma(&s);