080a160c7b9595d516c9c70e83689a09b60839d0 galt Mon Jun 3 12:16:53 2013 -0700 fix SQL Injection diff --git src/hg/lib/protVar.c src/hg/lib/protVar.c index 60bb398..1270028 100644 --- src/hg/lib/protVar.c +++ src/hg/lib/protVar.c @@ -89,68 +89,39 @@ while ((row = sqlNextRow(sr)) != NULL) { el = protVarLoad(row); slAddHead(&list, el); } slReverse(&list); sqlFreeResult(&sr); return list; } void protVarSaveToDb(struct sqlConnection *conn, struct protVar *el, char *tableName, int updateSize) /* Save protVar 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. Note that strings must be escaped to allow insertion into the database. - * For example "autosql's features include" --> "autosql\'s features include" - * If worried about this use protVarSaveToDbEscaped() */ + * inserted as NULL. Strings are automatically escaped to allow insertion into the database. */ { struct dyString *update = newDyString(updateSize); -dyStringPrintf(update, "insert into %s values ( '%s','%s','%s','%s','%s',%u)", +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); } -void protVarSaveToDbEscaped(struct sqlConnection *conn, struct protVar *el, char *tableName, int updateSize) -/* Save protVar 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. Automatically - * escapes all simple strings (not arrays of string) but may be slower than protVarSaveToDb(). - * For example automatically copies and converts: - * "autosql's features include" --> "autosql\'s features include" - * before inserting into database. */ -{ -struct dyString *update = newDyString(updateSize); -char *id, *name, *srcId, *baseChangeType, *location; -id = sqlEscapeString(el->id); -name = sqlEscapeString(el->name); -srcId = sqlEscapeString(el->srcId); -baseChangeType = sqlEscapeString(el->baseChangeType); -location = sqlEscapeString(el->location); - -dyStringPrintf(update, "insert into %s values ( '%s','%s','%s','%s','%s',%u)", - tableName, id, name, srcId, baseChangeType, location, el->coordinateAccuracy); -sqlUpdate(conn, update->string); -freeDyString(&update); -freez(&id); -freez(&name); -freez(&srcId); -freez(&baseChangeType); -freez(&location); -} struct protVar *protVarCommaIn(char **pS, struct protVar *ret) /* Create a protVar out of a comma separated string. * This will fill in ret if non-null, otherwise will * return a new protVar */ { char *s = *pS; if (ret == NULL) AllocVar(ret); ret->id = sqlStringComma(&s); ret->name = sqlStringComma(&s); ret->srcId = sqlStringComma(&s); ret->baseChangeType = sqlStringComma(&s); ret->location = sqlStringComma(&s); @@ -296,66 +267,39 @@ while ((row = sqlNextRow(sr)) != NULL) { el = protVarPosLoad(row); slAddHead(&list, el); } slReverse(&list); sqlFreeResult(&sr); return list; } void protVarPosSaveToDb(struct sqlConnection *conn, struct protVarPos *el, char *tableName, int updateSize) /* Save protVarPos 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. Note that strings must be escaped to allow insertion into the database. - * For example "autosql's features include" --> "autosql\'s features include" - * If worried about this use protVarPosSaveToDbEscaped() */ + * inserted as NULL. Strings are automatically escaped to allow insertion into the database. */ { struct dyString *update = newDyString(updateSize); -dyStringPrintf(update, "insert into %s values ( %u,'%s',%u,%u,'%s','%s','%s')", +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); } -void protVarPosSaveToDbEscaped(struct sqlConnection *conn, struct protVarPos *el, char *tableName, int updateSize) -/* Save protVarPos 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. Automatically - * escapes all simple strings (not arrays of string) but may be slower than protVarPosSaveToDb(). - * For example automatically copies and converts: - * "autosql's features include" --> "autosql\'s features include" - * before inserting into database. */ -{ -struct dyString *update = newDyString(updateSize); -char *chrom, *name, *strand, *label; -chrom = sqlEscapeString(el->chrom); -name = sqlEscapeString(el->name); -strand = sqlEscapeString(el->strand); -label = sqlEscapeString(el->label); - -dyStringPrintf(update, "insert into %s values ( %u,'%s',%u,%u,'%s','%s','%s')", - tableName, el->bin, chrom, el->chromStart, el->chromEnd, name, strand, label); -sqlUpdate(conn, update->string); -freeDyString(&update); -freez(&chrom); -freez(&name); -freez(&strand); -freez(&label); -} struct protVarPos *protVarPosCommaIn(char **pS, struct protVarPos *ret) /* Create a protVarPos out of a comma separated string. * This will fill in ret if non-null, otherwise will * return a new protVarPos */ { char *s = *pS; if (ret == NULL) AllocVar(ret); ret->bin = sqlUnsignedComma(&s); ret->chrom = sqlStringComma(&s); ret->chromStart = sqlUnsignedComma(&s); ret->chromEnd = sqlUnsignedComma(&s); ret->name = sqlStringComma(&s); @@ -492,64 +436,39 @@ while ((row = sqlNextRow(sr)) != NULL) { el = protVarAttrLoad(row); slAddHead(&list, el); } slReverse(&list); sqlFreeResult(&sr); return list; } void protVarAttrSaveToDb(struct sqlConnection *conn, struct protVarAttr *el, char *tableName, int updateSize) /* Save protVarAttr 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. Note that strings must be escaped to allow insertion into the database. - * For example "autosql's features include" --> "autosql\'s features include" - * If worried about this use protVarAttrSaveToDbEscaped() */ + * inserted as NULL. Strings are automatically escaped to allow insertion into the database. */ { struct dyString *update = newDyString(updateSize); -dyStringPrintf(update, "insert into %s values ( '%s','%s',%s)", +sqlDyStringPrintf(update, "insert into %s values ( '%s','%s',%s)", tableName, el->id, el->attrType, el->attrVal); sqlUpdate(conn, update->string); freeDyString(&update); } -void protVarAttrSaveToDbEscaped(struct sqlConnection *conn, struct protVarAttr *el, char *tableName, int updateSize) -/* Save protVarAttr 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. Automatically - * escapes all simple strings (not arrays of string) but may be slower than protVarAttrSaveToDb(). - * For example automatically copies and converts: - * "autosql's features include" --> "autosql\'s features include" - * before inserting into database. */ -{ -struct dyString *update = newDyString(updateSize); -char *id, *attrType, *attrVal; -id = sqlEscapeString(el->id); -attrType = sqlEscapeString(el->attrType); -attrVal = sqlEscapeString(el->attrVal); - -dyStringPrintf(update, "insert into %s values ( '%s','%s','%s')", - tableName, id, attrType, attrVal); -sqlUpdate(conn, update->string); -freeDyString(&update); -freez(&id); -freez(&attrType); -freez(&attrVal); -} struct protVarAttr *protVarAttrCommaIn(char **pS, struct protVarAttr *ret) /* Create a protVarAttr out of a comma separated string. * This will fill in ret if non-null, otherwise will * return a new protVarAttr */ { char *s = *pS; if (ret == NULL) AllocVar(ret); ret->id = sqlStringComma(&s); ret->attrType = sqlStringComma(&s); ret->attrVal = sqlStringComma(&s); *pS = s; return ret; @@ -676,68 +595,39 @@ while ((row = sqlNextRow(sr)) != NULL) { el = protVarLinkLoad(row); slAddHead(&list, el); } slReverse(&list); sqlFreeResult(&sr); return list; } void protVarLinkSaveToDb(struct sqlConnection *conn, struct protVarLink *el, char *tableName, int updateSize) /* Save protVarLink 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. Note that strings must be escaped to allow insertion into the database. - * For example "autosql's features include" --> "autosql\'s features include" - * If worried about this use protVarLinkSaveToDbEscaped() */ + * inserted as NULL. Strings are automatically escaped to allow insertion into the database. */ { struct dyString *update = newDyString(updateSize); -dyStringPrintf(update, "insert into %s values ( '%s','%s','%s','%s','%s')", +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); } -void protVarLinkSaveToDbEscaped(struct sqlConnection *conn, struct protVarLink *el, char *tableName, int updateSize) -/* Save protVarLink 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. Automatically - * escapes all simple strings (not arrays of string) but may be slower than protVarLinkSaveToDb(). - * For example automatically copies and converts: - * "autosql's features include" --> "autosql\'s features include" - * before inserting into database. */ -{ -struct dyString *update = newDyString(updateSize); -char *id, *attrType, *raKey, *acc, *displayVal; -id = sqlEscapeString(el->id); -attrType = sqlEscapeString(el->attrType); -raKey = sqlEscapeString(el->raKey); -acc = sqlEscapeString(el->acc); -displayVal = sqlEscapeString(el->displayVal); - -dyStringPrintf(update, "insert into %s values ( '%s','%s','%s','%s','%s')", - tableName, id, attrType, raKey, acc, displayVal); -sqlUpdate(conn, update->string); -freeDyString(&update); -freez(&id); -freez(&attrType); -freez(&raKey); -freez(&acc); -freez(&displayVal); -} struct protVarLink *protVarLinkCommaIn(char **pS, struct protVarLink *ret) /* Create a protVarLink out of a comma separated string. * This will fill in ret if non-null, otherwise will * return a new protVarLink */ { char *s = *pS; if (ret == NULL) AllocVar(ret); ret->id = sqlStringComma(&s); ret->attrType = sqlStringComma(&s); ret->raKey = sqlStringComma(&s); ret->acc = sqlStringComma(&s); ret->displayVal = sqlStringComma(&s);