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/test/otest/test1.c src/hg/test/otest/test1.c index b645d86..db64458 100644 --- src/hg/test/otest/test1.c +++ src/hg/test/otest/test1.c @@ -6,63 +6,77 @@ #include "portable.h" #include "jksql.h" #include "hash.h" #include "dystring.h" #include "linefile.h" #define sqlQuery sqlGetResult char *database = "test"; void dropAll() /* Drop all tables. */ { struct sqlConnection *conn = sqlConnect(database); -sqlGetResult(conn, NOSQLINJ "drop table word"); -sqlGetResult(conn, NOSQLINJ "drop table lineWords"); -sqlGetResult(conn, NOSQLINJ "drop table lineSize"); -sqlGetResult(conn, NOSQLINJ "drop table commaLine"); +char query[1024]; +sqlSafef(query, sizeof query, "drop table word"); +sqlGetResult(conn, query); +sqlSafef(query, sizeof query, "drop table lineWords"); +sqlGetResult(conn, query); +sqlSafef(query, sizeof query, "drop table lineSize"); +sqlGetResult(conn, query); +sqlSafef(query, sizeof query, "drop table commaLine"); +sqlGetResult(conn, query); sqlDisconnect(&conn); } void createAll() /* Define all tables. */ { struct sqlConnection *conn = sqlConnect(database); -sqlGetResult(conn, - NOSQLINJ "CREATE table word (" + +sqlSafef(query, sizeof query, + "CREATE table word (" "id smallint not null primary key," "word varchar(250) not null" ")" ); -sqlGetResult(conn, - NOSQLINJ "CREATE table lineSize (" +sqlGetResult(conn, query); + +sqlSafef(query, sizeof query, + "CREATE table lineSize (" "id int(8) not null primary key," "size smallint not null" ")" ); -sqlGetResult(conn, - NOSQLINJ "CREATE table lineWords (" +sqlGetResult(conn, query); + +sqlSafef(query, sizeof query, + "CREATE table lineWords (" "line int(8) not null," "word smallint not null," "pos smallint not null" ")" ); -sqlGetResult(conn, - NOSQLINJ "CREATE table commaLine (" +sqlGetResult(conn, query); + +sqlSafef(query, sizeof query, + "CREATE table commaLine (" "id int(8) not null primary key," "size smallint not null," "wordList blob not null" ")" ); +sqlGetResult(conn, query); + sqlDisconnect(&conn); } void freshTables() /* Make database have defined but empty tables. */ { dropAll(); createAll(); } int tokSize(char *text, int size) /* Return size of next token of text. */ { char c; int i; @@ -100,31 +114,31 @@ } void addFile(char *fileName) /* Add all the words in file to database. */ { struct lineFile *lf = lineFileOpen(fileName, FALSE); char *line; int lineSize; char wordBuf[1024]; int lineBuf[512]; int highestWordId = 0; int wordCount; char *nullPt = NULL; struct hash *wordHash = newHash(16); struct hashEl *hel; -struct dyString *query = newDyString(512); +struct dyString *query = dyStringNew(512); struct sqlConnection *conn = sqlConnect(database); int i; int lineCount = 0; while (lineFileNext(lf, &line, &lineSize)) { /* Chop the line up into words. */ int wordCount = 0; int startWord; int wordSize; int sizeLeft; int wordId; int i; for (sizeLeft = lineSize; sizeLeft > 0; sizeLeft -= wordSize, line += wordSize) { @@ -161,32 +175,32 @@ { wordId = (char *)(hel->val) - nullPt; } if (wordCount >= ArraySize(lineBuf)) { errAbort("Too many words in line %d of %s", lf->lineIx, lf->fileName); } lineBuf[wordCount++] = wordId; } /* Store the words in the database */ dyStringClear(query); sqlDyStringPrintf(query, "INSERT into commaLine values (%d, %d, '", lineCount, wordCount); for (i=0; i<wordCount; ++i) - dyStringPrintf(query, "%d,", lineBuf[i]); - dyStringAppend(query, "')"); + sqlDyStringPrintf(query, "%d,", lineBuf[i]); + sqlDyStringPrintf(query, "')"); sqlGetResult(conn, query->string); dyStringClear(query); sqlDyStringPrintf(query, "INSERT into lineSize values (%d,%d)", lineCount, wordCount); sqlGetResult(conn, query->string); for (i=0; i<wordCount; ++i) { dyStringClear(query); sqlDyStringPrintf(query, "INSERT into lineWords values (%d,%d,%d)", lineCount, lineBuf[i], i); sqlGetResult(conn, query->string); } ++lineCount; @@ -200,31 +214,33 @@ } char **loadWords() /* Load words from table into array. */ { struct sqlConnection *conn = sqlConnect(database); struct sqlResult *sr; int wordCount; char **words = NULL; int i = 0; char **row; wordCount = sqlTableSize(conn, "word"); uglyf("Got %d words\n", wordCount); words = needMem(wordCount * sizeof(words[0])); -sr = sqlQuery(conn, NOSQLINJ "select * from word"); +char query[1024]; +sqlSafef(query, sizeof query, "select * from word"); +sr = sqlQuery(conn, query); while ((row = sqlNextRow(sr)) != NULL) { words[i] = cloneString(row[1]); ++i; } printf("wordCount %d i %d\n", wordCount, i); sqlFreeResult(&sr); sqlDisconnect(&conn); return words; } void noOutput(FILE *f, char *s) /* Write string to file (maybe) */ { } @@ -239,31 +255,33 @@ /* Do comma based reconstruction. */ { char **words; long start, end; struct sqlConnection *conn = sqlConnect(database); struct sqlResult *sr; char **row; FILE *f = mustOpen(fileName, "w"); start = clock1000(); words = loadWords(); end = clock1000(); printf("Time to load words: %4.3f\n", 0.001*(end-start)); start = clock1000(); -sr = sqlQuery(conn, NOSQLINJ "SELECT * from commaLine"); +char query[1024]; +sqlSafef(query, sizeof query, "SELECT * from commaLine"); +sr = sqlQuery(conn, query); while ((row = sqlNextRow(sr)) != NULL) { int wordCount = sqlUnsigned(row[1]); int i; char *s = row[2],*e; int wordIx; for (i=0; i<wordCount; ++i) { e = strchr(s,','); *e++ = 0; wordIx = sqlUnsigned(s); s = e; fileOutput(f,words[wordIx]); } }