Focal Point
URL encoding and SQL

This topic can be found at:
https://forums.informationbuilders.com/eve/forums/a/tpc/f/7971057331/m/8281022652

November 07, 2007, 05:07 PM
jkyle
URL encoding and SQL
I am trying to understand how WebFOCUS handles URL encoding and decoding. It seems like when variables are sent to a focexec through the url (from form fields), all of the characters are being properly encoded (%27, for example), but by the time the variables are process in the focexec, these values are decoded again. This means that potentially malicious characters are being processed through our .FEXs and into SQL statements. Is there an option to prevent this decoding from taking place?

Much thanks,
Kyle


WebFOCUS 7.6.x
Windows
Output: Excel, PDF, HTML
November 07, 2007, 05:12 PM
Francis Mariani
Can you give me examples of "malicious characters"?


Francis


Give me code, or give me retirement. In FOCUS since 1991

Production: WF 7.7.05M, Dev Studio, BID, MRE, WebSphere, DB2 / Test: WF 8.1.05M, App Studio, BI Portal, Report Caster, jQuery, HighCharts, Apache Tomcat, MS SQL Server
November 07, 2007, 05:18 PM
jkyle
Sure: ; ' < > ( ) etc.

Basically stuff that would make SQL unhappy.


WebFOCUS 7.6.x
Windows
Output: Excel, PDF, HTML
November 08, 2007, 10:02 AM
Francis Mariani
I don't understand why these characters would make SQL unhappy. If they're used in a WHERE statement, they're most likely within quote characters so they would be treated as an alpha string.

-SET &TEST1 = 'D<T';

SET SQLENGINE  = DB2
-RUN

SQL
SELECT * 
FROM BASEL.TIME_D
WHERE NOT (PERIOD_TYPE LIKE '&TEST1')
FETCH FIRST 100 ROWS ONLY
END
-RUN


This works for all the characters you mention, except the single quote character.

Handling that character is most likely a WebFOCUS issue, not a SQL one and can be resolved by using the QUOTEDSTRING suffix. With this method, all the characters you mention would work without any problem:

-SET &TEST1 = 'D'T';

-*-- or -SET &TEST1 = 'D)T';

SET SQLENGINE  = DB2
-RUN

SQL
SELECT * 
FROM BASEL.TIME_D
WHERE NOT (PERIOD_TYPE LIKE &TEST1.QUOTEDSTRING )
FETCH FIRST 100 ROWS ONLY
END
-RUN


By the way, my guess is that the temporary encoding you describe is normal browser behaviour, the characters are encoded while in the URL, but are automatically decoded when they get passed to whatever program receives them.


Francis


Give me code, or give me retirement. In FOCUS since 1991

Production: WF 7.7.05M, Dev Studio, BID, MRE, WebSphere, DB2 / Test: WF 8.1.05M, App Studio, BI Portal, Report Caster, jQuery, HighCharts, Apache Tomcat, MS SQL Server
November 08, 2007, 10:58 AM
jkyle
Francis, thanks for the reply.

If we have an SQL statement like

SELECT * FROM owners WHERE username = '" &NAME "'

The problem is when something like this gets inserted into the url:

&NAME=junk';DROP TABLE owners; SELECT * FROM data WHERE info LIKE '%

Even when using MODIFYs instead of direct SQL statements, if these characters don't remain safely escaped they could cause serious damage to the data.

Does that make sense? It could very well be default browser behavior; I'm just trying to understand how to prevent these SQL injections in WebFOCUS.

Thanks again!


WebFOCUS 7.6.x
Windows
Output: Excel, PDF, HTML
November 08, 2007, 11:06 AM
Francis Mariani
If you're worried about somebody passing this kind of stuff to WebFOCUS then I imagine there are issues with your network/database/WebFOCUS security. I don't see how an encoded character would protect you from "DROP TABLE owners;".


Francis


Give me code, or give me retirement. In FOCUS since 1991

Production: WF 7.7.05M, Dev Studio, BID, MRE, WebSphere, DB2 / Test: WF 8.1.05M, App Studio, BI Portal, Report Caster, jQuery, HighCharts, Apache Tomcat, MS SQL Server
November 08, 2007, 12:22 PM
GinnyJakes
Or you may have to examine the incoming data for unwanted characters and removing them before executing the SQL.

Or javascript on the page itself to evaluate the data before sending it to WebFOCUS.


Ginny
---------------------------------
Prod: WF 7.7.01 Dev: WF 7.6.9-11
Admin, MRE,self-service; adapters: Teradata, DB2, Oracle, SQL Server, Essbase, ESRI, FlexEnable, Google
November 08, 2007, 01:40 PM
jkyle
I think you are right about the encoding stuff being the wrong way to handle it... Instead of filtering out bad characters (or allowing good ones), it'd be good to know how WebFOCUS translates TABLE FILEs and MODIFY FILEs into SQL.

Do &vars get inserted into the SQL request and evaluated as SQL, or does WebFOCUS use prepare statements where the SQL is evaluated and THEN &variables are inserted?


WebFOCUS 7.6.x
Windows
Output: Excel, PDF, HTML
November 08, 2007, 02:36 PM
Francis Mariani
Have you tried turning the SQL Trace on? You can see the SQL that gets generated by WebFOCUS.

See how to turn traces on here: Report never seems to stop running

amper variables get inserted into the WebFOCUS code as alphanumeric strings and become part of the code. The generated SQL is a SELECT statement translated from the WebFOCUS code, after the amper variables are inserted into the code.


Francis


Give me code, or give me retirement. In FOCUS since 1991

Production: WF 7.7.05M, Dev Studio, BID, MRE, WebSphere, DB2 / Test: WF 8.1.05M, App Studio, BI Portal, Report Caster, jQuery, HighCharts, Apache Tomcat, MS SQL Server