SlideShare uma empresa Scribd logo
1 de 71
Advanced Web Programming outside of APEX Filipe Silva Silva . Filipe @ gmail . com Faculdade de Engenharia - Universidade do Porto Portugal
Advanced Web Programming outside of beyond APEX Filipe Silva Silva . Filipe @ gmail . com Faculdade de Engenharia - Universidade do Porto Portugal
Brief review of PL/SQL Web Toolkit Web Request processing Security Error management Improve DB use File upload/ download and generation Best Practices PL/SQL Gateways alternatives Topics
Some Examples
Some Examples
Some Examples
Main Aplication: Almost 2 million PL/SQL code lines 4 000 Java lines 120 000 calls to HTP Framework: 240 000 PL/SQL code lines 1 300 Java lines 11 000 calls to HTP Statistics for SIGARRA© Documents generated (at FEUP) September 17-9-2010 :     1 230 493 (high) 05-09-2010:     196 595 (low) month average:  555 868 /day
The HTTP Server receives a request from a client browser (1) …routes the request to the plsql gateway (adapter) …that ”maps” the request to an Oracle DB call (2) Web Request Processing http://<http_server_name>:<port>/<dad>/<procedure>?<var1>=<value_var1>
Web Request Processing (cont.) Use of PL/SQL Web Toolkit will create an output in a buffer array (3) If no unhandled exception was raise a COMMIT is made at the end of the PL/SQL call The plsql Gateway gets the OWA buffer content and sends it to the HTTP Server (some sanitization is made) (4) …thatdeliverstheresult to theclient. (5)
Allows to generate web content directly from  Oracle (since 8.1.6) Has direct access to the DB (no-round trip) It’s easy to learn Keeps all the code in the same place (backups,…) You can keep all the files in DB PL/SQL Web Toolkit
PL/SQL Web Toolkit HTP – write to OWA  (Web) output HTF – construct HTML (mainly) OWA_CACHE  - writescachingheadertags OWA_UTIL - retrievesenvironment variables, write header,... OWA_COOKIE – send and receive browser cookies OWA_OPT_LOCK –handles optimistic lock OWA_IMAGE – get coordinates from  user click in image map OWA_SEC – security programs OWA_CUSTOM - authorize function OWA_PATTERN – pattern matching OWA_MATCH – pattern matching OWA_TEXT  - stringmanipulation WPG_DOCLOAD – BLOBs and BFILEs downloads
HTP.p OWA_UTIL.mime_header OWA_UTIL.redirect_url OWA_UTIL.status_line OWA_UTIL.http_header_close OWA_UTIL.get_procedure OWA_UTIL.get_cgi_env WPG_DOCLOAD.download_file PL/SQL Web Toolkit ,[object Object]
OWA_COOKIE.get
HTF.escape_sc,[object Object]
DECLARE (...)     lv_cookies     VARCHAR2 (2000) := 'TESTE=Teste;'; BEGIN li_version := OWA.initialize; OWA_COOKIE.init;                   -- so it will reload the cookies buffer        la_name_arr (1) := 'HTTP_COOKIE';        la_value_arr (1) := lv_cookies;        li_num := li_num +1;     OWA.init_cgi_env (li_num, la_name_arr, la_value_arr);     my_program();     irows := 99999999999; OWA.get_page (thepage => la_thepage, irows => li_irows); (...) END; Test Output with cookies OWA_COOKIE.cookie         RECORD ( name     varchar2(4096), vals     vc_arr, num_vals integer ) PROCEDURE my_program IS  (...) lrec_cookieOWA_COOKIE.cookie BEGIN (...)     lrec_cookie := OWA_COOKIE.get ('TESTE'); IF lrec_cookie.num_vals > 0 THEN HTP.p ('Cookie value:' || lrec_cookie.vals (1)); ENDIF; (...) END;
Needs ACL (access control list) permissions in 11g Allows HTTP callouts from PL/SQL and SQL Takes a URL as parameter and returns corresponding Webpage Request :data is returned in the form of pieces, each piece is varchar2(2000) Request Pieces: all pieces obtained at once as a table type of Varchar2(2000) UTL_HTTP
DECLARE l_request         UTL_HTTP.req;   l_responseUTL_HTTP.resp; (…) BEGIN UTL_HTTP.set_proxy (proxy => ‘myproxy‘, no_proxy_domains   => ‘dom1,dom2');  l_request := UTL_HTTP.begin_request ('http://someurl’ ,method   => 'GET');  UTL_HTTP.set_header (l_request, 'User-Agent', 'Mozilla/4.0'); l_response := UTL_HTTP.get_response (l_request); DBMS_OUTPUT.put_line (' HTTP response: status code: ' || l_response.status_code || ' reason phrase: ' || l_response.reason_phrase); read_header (); read_lines (); UTL_HTTP.end_response (l_response); END; UTL_HTTP PROCEDURE read_header  IS lv_header_name    VARCHAR2 (256); lv_header_value   VARCHAR2 (1024);     BEGIN         FOR i IN 1 .. UTL_HTTP.get_header_count (l_response)         LOOP UTL_HTTP.get_header (l_response, i,  lv_header_name, lv_header_value); DBMS_OUTPUT.put_line (lv_header_name || ': ' || lv_header_value);         END LOOP; END read_header;     PROCEDURE read_lines IS lv_line           VARCHAR2 (32767);     BEGIN         LOOP UTL_HTTP.read_line (l_response, lv_line); DBMS_OUTPUT.put_line (lv_line);         END LOOP;     EXCEPTION         WHEN UTL_HTTP.end_of_body         THEN                NULL; END read_lines;
Read RSS ReadWebservices (REST, …) ReadWebpages Get Documents via URL  Example: crawling sites to index by Oracle Text  … UTL_HTTP: Uses
HTTP is a “stateless” protocol Means credentials have to go with every request (i.e. cookies) Should use SSL for everything requiring authentication (https) Important answer status 200 – ok 204 – no content 304 – not modified 403 – forbidden 404 – not found  About HTTP
Improve DB use (1) HTTP Requests Types HTTP request types supported: GET – parameters passed by query string (URL) POST – parameters passed by HTTP Request body (large amount of data, file uploads, password forms, etc..) HEAD – like GET but web server only returns header info So...only return header info and exit the procedure!  (…) –- send header IF OWA_UTIL.get_cgi_env ('REQUEST_METHOD') = ‘HEAD'     THEN         RETURN;  END IF;  (…) –- send content
DAD configuration  PlsqlErrorStyle – use ApacheStyle in production systems   PlsqlExclusionList  - Blacklist default: sys.*, dbms_*, utl_*, owa_*, owa.*, htp.*, htf.*  PlsqlRequestValidationFunction – blacklist or whitelist booleanfunction_name (procedure_name IN varchar2)  Security MOD_PLSQL
FUNCTIONmy_validation_check (procedure_nameINVARCHAR2) RETURNBOOLEAN IS     li_check   PLS_INTEGER; BEGIN     SELECT 1       INTO li_check       FROM my_pages      WHERE name = UPPER (procedure_name); RETURNTRUE; EXCEPTION     WHEN NO_DATA_FOUND     THEN RETURNFALSE; END my_validation_check; PlsqlRequestValidationFunction
Use a whitelist Web procedure table Best practices (0)
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"> <html> <head> <title>Hello world</title> <link rel="stylesheet" href="mystyle.css"> </head> <body>   Hello World! </body> </html> HTML
Why? HTML code in lowercase letters, and NEVER skip closing tags (like </p>) Parsing errors => stop parse! Not supported by totally of HTF (and HTP) XHTML
Make your own HTP and HTF packages The new HTP must call sys.HTP to send the buffer to the OWA Output and use the same buffer types Create procedures for new tags, opening and closing tags Delete/comment obsolete tags XHTML complain (lower case, close tags, …) Add ‘class’ attribute to tags (…) Allow buffer control Read without changing it (or clean it) Manipulation For instance, if you need to get some storage buffer (3 columns divs problem) Best practices (1)
Text files CSS JSON  http://reseau.erasme.org/pl-sql-library-for-JSON http://sourceforge.net/projects/pljson/ etc.. XML RSS http://tylermuth.wordpress.com/2008/01/22/producing-rss-from-plsql/ etc.. What else?
PDF PL_FPDF - http://www.erasme.org/PL-FPDF,1337 PL/PDF - http://plpdf.com/  € AS_PDF - http://technology.amis.nl/blog/8650/as_pdf-generating-a-pdf-document-with-some-plsql Youcanalsomanipulateimage files using Oracle interMedia To createthumbnails Putwatermark (11.2) … What else?
QrCode http://sourceforge.net/projects/oracle-jutils/ ZIP (BLOBs) What else?
owa_util.mime_header('application/vnd.ms-excel');  Use SYLK or even HTML!! SYLK is a text-based interchange format for spreadsheets; it supports formulas, borders, fonts, point sizes, etc.  Or CSV (Comma Separated File) (mime-type: text/csv)  PL/SQL ExcelDocumentType http://www.jasonsdevelopercorner.com/?page_id=8 PL/XLS-XML € http://plpdf.com/ Excel
SVG (XML) FusionCharts	http://www.jasonsdevelopercorner.com/?page_id=22 Needgraphics?
DAD configuration: PlsqlDocumentTablename  - the table name for storing documents when file uploads are performed through the DAD Table definition: NAME VARCHAR(128) UNIQUE NOT NULL, MIME_TYPE VARCHAR(128), DOC_SIZE NUMBER, DAD_CHARSET VARCHAR(128), LAST_UPDATED DATE, CONTENT_TYPE VARCHAR(128), [[CONTENT LONG RAW],    -- files with extensions in PlsqlUploadAsLongRaw  [BLOB_CONTENT BLOB]] – other files (...) – can have more columns File upload
HTML: <form enctype="multipart/form-data" action=“my_pack.my_proc" method="POST"> <input type="file" name=“p_my_file“ /> (...) In DB: procedure my_proc (p_my_file IN varchar2, …) is lrt_docdocuments_table%ROWTYPE; begin  	select *  		into lrt_doc 		from documents_table 		where name=p_my_file; (…) File upload
Do not keep documents in the uploaded documents table Avoid collisions Provide distinct access privileges Backup polices Distinct Business logic … Best practice (2)
1) Send Header info OWA_UTIL.mime_header(prt_doc.mime_type, FALSE); 			-- specify mime_type and do not close header HTP.p ('Last-Modified: ' ||pv_last_updated); -- allow cache HTP.p ('Content-Length: ' ||prt_doc.doc_size); -- download info HTP.p ('Content-disposition: attachment; filename=“'|| prt_doc.filename||'” '); -- “save As” OWA_UTIL.http_header_close; --close header File download
2) Send Data 2a) If BLOB call: DECLARE lb_lob   BLOB := prt_doc.content; -- due to need to be an IN OUT BEGIN WPG_DOCLOAD.download_file (p_blob => lb_lob); END; 	2b) If CLOB: DECLARE lb_lob   BLOB := clob_util.clob2blob (p_clob);  BEGIN WPG_DOCLOAD.download_file (p_blob => lb_lob); END; File download
DECLARE offset   PLS_INTEGER;         len      PLS_INTEGER;         amount   PLS_INTEGER := 32000; BEGIN         len := NVL (DBMS_LOB.getlength (c_lob), 0); FOR i IN0 .. len / amount LOOP             offset := (amount * i) + 1; HTP.prn(DBMS_LOB.SUBSTR(c_lob, amount , offset)); ENDLOOP; END; File download Or2b) If CLOB
 FUNCTION clob2blob (p_clob IN CLOB) RETURN BLOB DETERMINISTICIS         lbl_blob         BLOB;         li_dest_offset   INTEGER := 1;         li_src_offset    INTEGER := 1;         li_amount        INTEGER := DBMS_LOB.lobmaxsize;         ln_blob_csid     NUMBER := DBMS_LOB.default_csid;         li_lang_ctx      INTEGER := DBMS_LOB.default_lang_ctx;         li_warning       INTEGER;     BEGIN         DBMS_LOB.createtemporary (lob_loc   => lbl_blob, cache     => TRUE, dur       => DBMS_LOB.call);         DBMS_LOB.converttoblob (dest_lob       => lbl_blob                                ,src_clob       => p_clob                                ,amount         => li_amount                                ,dest_offset    => li_dest_offset                                ,src_offset     => li_src_offset                                ,blob_csid      => ln_blob_csid                                ,lang_context   => li_lang_ctx                                ,warning        => li_warning);         RETURN lbl_blob;     END clob2blob; CLOB 2 BLOB
An Example: OWA_UTIL.mime_header ('text/css', FALSE); -- sends mime-type  						               -- and doesn’t  closes the header HTP.P ('Last-Modified: ' || lv_updated_date);  HTP.P ('Cache-Control: public, s-maxage=300, max-age=86400'); -- or  -- HTP.p ('Cache-Control: private, max-age=86400'); --or -- HTP.p (‘Cache-Control: no-cache’);  --or even no-store OWA_UTIL.http_header_close; --close the header (…) Cache – Last Modified
-- Thu, 30 Sep 2010 16:03:22 GMT lv_updated_date :=TO_CHAR ( SYS_EXTRACT_UTC (                   CAST (NVL (pd_last_modified, SYSDATE) 		AS TIMESTAMP)),                'Dy, DD Mon YYYY HH24:MI:SS',                'NLS_DATE_LANGUAGE=''AMERICAN''')             || ' GMT'; Last-Modified
Then I can check if the browser has the document in cache IF OWA_UTIL.get_cgi_env ('HTTP_IF_MODIFIED_SINCE') = lv_updated_date       THEN OWA_UTIL.status_line (nstatus         => 304, creason         => 'Not Modified', bclose_header   => TRUE); RETURN;  END IF; Improve DB use (2)
Can also use ETag: HTP.p('ETag: ' || lv_file_etag); 		-- or owa_cache.set_cache(p_etag, p_level);  		--  length(lv_file_etag)<= 55 And to check: OWA_UTIL.get_cgi_env('HTTP_IF_NONE_MATCH') --needs PlsqlCGIEnvironmentList HTTP_IF_NONE_MATCH Cache - ETag
Pseudo-Code ,[object Object]
-> if so return status 503 -> exit
Output HTTP (Response) header info
If HEAD request
-> exit
Output ContentBest Practices (3)Improve DB use (1+2)
If a page can only be access by a POST form -> check it! owa_util.get_cgi_env( 'REQUEST_METHOD' ) =‘POST’ and 		  	           owa_util.get_cgi_env( 'HTTP_REFERER‘) is not null  -- maybe even check that the referer is from the same site or an exact page … If a page is only to be access by a AJAX -> check it! OWA_UTIL.get_cgi_env ('X-Requested-With') is not null 	    -- needs DAD PlsqlCGIEnvironmentList X-Requested-With But an hacker can avoid that if they know of it  Disallow Access
begin (…) exceptions (…) whenothersthen qem_web.register_error; end; Error Manager
Error Manager
Error Manager
Error Manager
 Avoid reporting system messages that the normal user cannot understand but an hacker could use (Error message SQL Injection). (i.e SQLERRM, …) The errors should be reported to a system maintainer even if the user doesn’t report them.  Should have enough information to replicate the error.  We based our solution on Quest Error Manager (http://qem.inside.quest.com/index.jspa) Error Manager
Quest Error Manager Log 4 PLSQL Logger PL/SQL Log & DBugLibrary PLJ_LG … Error and Log Libraries
Have an error handler for when others in every procedure that will generate your web pages The ones that are to called by URL They are in a whitelist table, right? With PL/Scope (11g) you can have a script that check the use of your error report program inside the procedures Best Pratices (4)
The OWASP Top 10 Web Application Security Risks for 2010 are:  A1: Injection  A2: Cross-Site Scripting (XSS)  A3: Broken Authentication and Session Management  A4: Insecure Direct Object References  A5: Cross-Site Request Forgery (CSRF)  A6: Security Misconfiguration  A7: Insecure Cryptographic Storage  A8: Failure to Restrict URL Access  A9: Insufficient Transport Layer Protection  A10: Unvalidated Redirects and Forwards  Security
One of the more devastating attacks on a web application One of the most common attacks Probably the most popular technique used for database intrusion caused by inefficient input validation Some real World examples (2005-2010): http://en.wikipedia.org/wiki/SQL_injection#Real-world_examples SQL injection: what?
PROCEDUREmy_bad_proc (v inVARCHAR2) IS TYPE cursortyp ISREFCURSOR; cursordual   cursortyp;     result_v     VARCHAR2 (300);     sql_stmt     VARCHAR2 (300); BEGIN sql_stmt := 'select * from dual where dummy=''' || v || ''';     DBMS_OUTPUT.put_line (sql_stmt); OPEN cursordual FOR sql_stmt; LOOP FETCH cursordual INTO result_v; EXITWHEN cursordual%NOTFOUND;         DBMS_OUTPUT.put_line (result_v); ENDLOOP; END my_bad_proc; Sql injection: example BEGIN my_bad_proc(v=>       	'aaa''            	union        	select login from logins -- END; select * from dual where dummy='aaa'    union   select login from logins -- …… Mark Steven (…)
In Oracle when concatenating non-constants to be used as a statement in EXECUTE IMMEDIATE v_string; OPEN v_cursor FOR v_string; DBMS_SQL.parse (..., v_string, …) Safe code:  SELECT … INTO … And  above if not concatenating with non- constants OPEN … USING var1, var2,… EXECUTE IMMEDIATE …. USING var1, var2,… DBMS_SQL.bind_variable SQL Injection: where?
We need to convert the Text value to a SQL text Literal. If needed by design replace all the quotation marks with two quotations 		replace(v,'''','''''') 			-- for instance to allow O’Neil Use DBMS_ASSERT.ENQUOTE_LITERAL: Enclose the literal with quotation Check with DBMS_ASSERT.enquote_literal the validy catching the exception VALUE_ERROR it raises if invalid OR Use DBMS_ASSERT.enquote_literal to enclose the literal with in quotation marks  and it raises VALUE_ERROR  if invalid. Best Pratices (4)
PROCEDUREmy_good_proc1 (v inVARCHAR2) IS     (…)     lv_literal      VARCHAR2(100):='''' || REPLACE (v, '''', '''''') || ''''; BEGIN    BEGIN lv_literal :=sys.DBMS_ASSERT.enquote_literal(lv_literal );    EXCEPTION WHENVALUE_ERROR THENDBMS_OUTPUT.put_line ('Value entered invalid'); RETURN;      END;     sql_stmt := 'select * from dual where dummy=' || lv_literal;    OPEN cursordual FOR sql_stmt;      (…) END my_good_proc1; DBMS_ASSERT.enquote_literal (1)
PROCEDUREmy_good_proc2(v VARCHAR2) IS  (…) BEGIN    BEGIN          sql_stmt := 'select * from dual where dummy='  	||sys.DBMS_ASSERT.enquote_literal(v);  EXCEPTION WHENVALUE_ERROR THEN         DBMS_OUTPUT.put_line ('Value entered invalid'); RETURN;      END; OPEN cursordual FORsql_stmt; (...) END my_good_proc2; DBMS_ASSERT.enquote_literal (2)
PROCEDUREmy_good_proc2(v VARCHAR2) IS  (…) BEGIN    BEGIN          sql_stmt := 'select * from dual where dummy=‘ 	||sys.DBMS_ASSERT.enquote_literal(REPLACE (v, '''', '''''') );  EXCEPTION WHENVALUE_ERROR THEN         DBMS_OUTPUT.put_line ('Value entered invalid'); RETURN;      END; OPEN cursordual FORsql_stmt; (...) END my_good_proc2; DBMS_ASSERT.enquote_literal (2)
DBMS_ASSERT SIMPLE_SQL_NAME QUALIFIED_SQL_NAME SCHEMA_NAME SQL_OBJECT_NAME ENQUOTE_LITERAL – single quotes check ENQUOTE_NAME – double quotes check
Protect the database and web server (patches) Use minimum privileges AUTHID CURRENT_USER when possible Use of prepared statement Data API Sanitize Input: from users URL parameters Cookies avoid cursor injection (11g): DBMS_SQL.open_cursor(security_level => 2); SQL Injection: Prevention
The OWASP Top 10 Web Application Security Risks for 2010 are:  A1: Injection  A2: Cross-Site Scripting (XSS)  A3: Broken Authentication and Session Management  A4: Insecure Direct Object References  A5: Cross-Site Request Forgery (CSRF)  A6: Security Misconfiguration  A7: Insecure Cryptographic Storage  A8: Failure to Restrict URL Access  A9: Insufficient Transport Layer Protection  A10: Unvalidated Redirects and Forwards  Security
XSS is a form of injection where the interpreter is the browser and attacks are “buried” in the HTML document Cross-Site Scripting (XSS)
Sanitize input HTML Escapeuntrusted data (htf.escape_sc) if HTML input allowed…check it Use the new Content Security Policy header Examples: If no external script and no inline-script X-Content-Security-Policy: allow 'self';  If inline script exists but no external script X-Content-Security-Policy: allow 'self'; options inline-script; Cross-Site Scripting (XSS): Prevention
Avoid iframe/frame X-Frame-Options: [deny | sameOrigin] X-Content-Security-Policy: frame-src: ... HTTPOnly cookie flag (OAS version >= 10.1.3.5.0) ... Cross-Site Scripting (XSS): Prevention (cont.)
The OWASP Top 10 Web Application Security Risks for 2010 are:  A1: Injection  A2: Cross-Site Scripting (XSS)  A3: Broken Authentication and Session Management  A4: Insecure Direct Object References  A5: Cross-Site Request Forgery (CSRF)  A6: Security Misconfiguration  A7: Insecure Cryptographic Storage  A8: Failure to Restrict URL Access  A9: Insufficient Transport Layer Protection  A10: Unvalidated Redirects and Forwards  Security

Mais conteúdo relacionado

Último

Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Allon Mureinik
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Igalia
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Scriptwesley chun
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsTop 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsRoshan Dwivedi
 
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...gurkirankumar98700
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Paola De la Torre
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processorsdebabhi2
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Servicegiselly40
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024The Digital Insurer
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 

Último (20)

Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsTop 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
 
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 

Destaque

PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024Neil Kimberley
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsKurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summarySpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentLily Ray
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project managementMindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...RachelPearson36
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Applitools
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at WorkGetSmarter
 
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...DevGAMM Conference
 

Destaque (20)

Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work
 
ChatGPT webinar slides
ChatGPT webinar slidesChatGPT webinar slides
ChatGPT webinar slides
 
More than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike RoutesMore than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike Routes
 
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
 

Advanced Web Programming outside of APEX

  • 1. Advanced Web Programming outside of APEX Filipe Silva Silva . Filipe @ gmail . com Faculdade de Engenharia - Universidade do Porto Portugal
  • 2. Advanced Web Programming outside of beyond APEX Filipe Silva Silva . Filipe @ gmail . com Faculdade de Engenharia - Universidade do Porto Portugal
  • 3. Brief review of PL/SQL Web Toolkit Web Request processing Security Error management Improve DB use File upload/ download and generation Best Practices PL/SQL Gateways alternatives Topics
  • 7. Main Aplication: Almost 2 million PL/SQL code lines 4 000 Java lines 120 000 calls to HTP Framework: 240 000 PL/SQL code lines 1 300 Java lines 11 000 calls to HTP Statistics for SIGARRA© Documents generated (at FEUP) September 17-9-2010 : 1 230 493 (high) 05-09-2010: 196 595 (low) month average: 555 868 /day
  • 8. The HTTP Server receives a request from a client browser (1) …routes the request to the plsql gateway (adapter) …that ”maps” the request to an Oracle DB call (2) Web Request Processing http://<http_server_name>:<port>/<dad>/<procedure>?<var1>=<value_var1>
  • 9. Web Request Processing (cont.) Use of PL/SQL Web Toolkit will create an output in a buffer array (3) If no unhandled exception was raise a COMMIT is made at the end of the PL/SQL call The plsql Gateway gets the OWA buffer content and sends it to the HTTP Server (some sanitization is made) (4) …thatdeliverstheresult to theclient. (5)
  • 10. Allows to generate web content directly from Oracle (since 8.1.6) Has direct access to the DB (no-round trip) It’s easy to learn Keeps all the code in the same place (backups,…) You can keep all the files in DB PL/SQL Web Toolkit
  • 11. PL/SQL Web Toolkit HTP – write to OWA (Web) output HTF – construct HTML (mainly) OWA_CACHE - writescachingheadertags OWA_UTIL - retrievesenvironment variables, write header,... OWA_COOKIE – send and receive browser cookies OWA_OPT_LOCK –handles optimistic lock OWA_IMAGE – get coordinates from user click in image map OWA_SEC – security programs OWA_CUSTOM - authorize function OWA_PATTERN – pattern matching OWA_MATCH – pattern matching OWA_TEXT - stringmanipulation WPG_DOCLOAD – BLOBs and BFILEs downloads
  • 12.
  • 14.
  • 15. DECLARE (...) lv_cookies VARCHAR2 (2000) := 'TESTE=Teste;'; BEGIN li_version := OWA.initialize; OWA_COOKIE.init; -- so it will reload the cookies buffer la_name_arr (1) := 'HTTP_COOKIE'; la_value_arr (1) := lv_cookies; li_num := li_num +1; OWA.init_cgi_env (li_num, la_name_arr, la_value_arr); my_program(); irows := 99999999999; OWA.get_page (thepage => la_thepage, irows => li_irows); (...) END; Test Output with cookies OWA_COOKIE.cookie RECORD ( name varchar2(4096), vals vc_arr, num_vals integer ) PROCEDURE my_program IS (...) lrec_cookieOWA_COOKIE.cookie BEGIN (...) lrec_cookie := OWA_COOKIE.get ('TESTE'); IF lrec_cookie.num_vals > 0 THEN HTP.p ('Cookie value:' || lrec_cookie.vals (1)); ENDIF; (...) END;
  • 16. Needs ACL (access control list) permissions in 11g Allows HTTP callouts from PL/SQL and SQL Takes a URL as parameter and returns corresponding Webpage Request :data is returned in the form of pieces, each piece is varchar2(2000) Request Pieces: all pieces obtained at once as a table type of Varchar2(2000) UTL_HTTP
  • 17. DECLARE l_request UTL_HTTP.req; l_responseUTL_HTTP.resp; (…) BEGIN UTL_HTTP.set_proxy (proxy => ‘myproxy‘, no_proxy_domains => ‘dom1,dom2'); l_request := UTL_HTTP.begin_request ('http://someurl’ ,method => 'GET'); UTL_HTTP.set_header (l_request, 'User-Agent', 'Mozilla/4.0'); l_response := UTL_HTTP.get_response (l_request); DBMS_OUTPUT.put_line (' HTTP response: status code: ' || l_response.status_code || ' reason phrase: ' || l_response.reason_phrase); read_header (); read_lines (); UTL_HTTP.end_response (l_response); END; UTL_HTTP PROCEDURE read_header IS lv_header_name VARCHAR2 (256); lv_header_value VARCHAR2 (1024); BEGIN FOR i IN 1 .. UTL_HTTP.get_header_count (l_response) LOOP UTL_HTTP.get_header (l_response, i, lv_header_name, lv_header_value); DBMS_OUTPUT.put_line (lv_header_name || ': ' || lv_header_value); END LOOP; END read_header; PROCEDURE read_lines IS lv_line VARCHAR2 (32767); BEGIN LOOP UTL_HTTP.read_line (l_response, lv_line); DBMS_OUTPUT.put_line (lv_line); END LOOP; EXCEPTION WHEN UTL_HTTP.end_of_body THEN NULL; END read_lines;
  • 18. Read RSS ReadWebservices (REST, …) ReadWebpages Get Documents via URL Example: crawling sites to index by Oracle Text … UTL_HTTP: Uses
  • 19. HTTP is a “stateless” protocol Means credentials have to go with every request (i.e. cookies) Should use SSL for everything requiring authentication (https) Important answer status 200 – ok 204 – no content 304 – not modified 403 – forbidden 404 – not found About HTTP
  • 20. Improve DB use (1) HTTP Requests Types HTTP request types supported: GET – parameters passed by query string (URL) POST – parameters passed by HTTP Request body (large amount of data, file uploads, password forms, etc..) HEAD – like GET but web server only returns header info So...only return header info and exit the procedure! (…) –- send header IF OWA_UTIL.get_cgi_env ('REQUEST_METHOD') = ‘HEAD' THEN RETURN; END IF; (…) –- send content
  • 21. DAD configuration PlsqlErrorStyle – use ApacheStyle in production systems PlsqlExclusionList - Blacklist default: sys.*, dbms_*, utl_*, owa_*, owa.*, htp.*, htf.* PlsqlRequestValidationFunction – blacklist or whitelist booleanfunction_name (procedure_name IN varchar2) Security MOD_PLSQL
  • 22. FUNCTIONmy_validation_check (procedure_nameINVARCHAR2) RETURNBOOLEAN IS li_check PLS_INTEGER; BEGIN SELECT 1 INTO li_check FROM my_pages WHERE name = UPPER (procedure_name); RETURNTRUE; EXCEPTION WHEN NO_DATA_FOUND THEN RETURNFALSE; END my_validation_check; PlsqlRequestValidationFunction
  • 23. Use a whitelist Web procedure table Best practices (0)
  • 24. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"> <html> <head> <title>Hello world</title> <link rel="stylesheet" href="mystyle.css"> </head> <body> Hello World! </body> </html> HTML
  • 25. Why? HTML code in lowercase letters, and NEVER skip closing tags (like </p>) Parsing errors => stop parse! Not supported by totally of HTF (and HTP) XHTML
  • 26. Make your own HTP and HTF packages The new HTP must call sys.HTP to send the buffer to the OWA Output and use the same buffer types Create procedures for new tags, opening and closing tags Delete/comment obsolete tags XHTML complain (lower case, close tags, …) Add ‘class’ attribute to tags (…) Allow buffer control Read without changing it (or clean it) Manipulation For instance, if you need to get some storage buffer (3 columns divs problem) Best practices (1)
  • 27. Text files CSS JSON http://reseau.erasme.org/pl-sql-library-for-JSON http://sourceforge.net/projects/pljson/ etc.. XML RSS http://tylermuth.wordpress.com/2008/01/22/producing-rss-from-plsql/ etc.. What else?
  • 28. PDF PL_FPDF - http://www.erasme.org/PL-FPDF,1337 PL/PDF - http://plpdf.com/ € AS_PDF - http://technology.amis.nl/blog/8650/as_pdf-generating-a-pdf-document-with-some-plsql Youcanalsomanipulateimage files using Oracle interMedia To createthumbnails Putwatermark (11.2) … What else?
  • 30. owa_util.mime_header('application/vnd.ms-excel'); Use SYLK or even HTML!! SYLK is a text-based interchange format for spreadsheets; it supports formulas, borders, fonts, point sizes, etc. Or CSV (Comma Separated File) (mime-type: text/csv) PL/SQL ExcelDocumentType http://www.jasonsdevelopercorner.com/?page_id=8 PL/XLS-XML € http://plpdf.com/ Excel
  • 32. DAD configuration: PlsqlDocumentTablename - the table name for storing documents when file uploads are performed through the DAD Table definition: NAME VARCHAR(128) UNIQUE NOT NULL, MIME_TYPE VARCHAR(128), DOC_SIZE NUMBER, DAD_CHARSET VARCHAR(128), LAST_UPDATED DATE, CONTENT_TYPE VARCHAR(128), [[CONTENT LONG RAW], -- files with extensions in PlsqlUploadAsLongRaw [BLOB_CONTENT BLOB]] – other files (...) – can have more columns File upload
  • 33. HTML: <form enctype="multipart/form-data" action=“my_pack.my_proc" method="POST"> <input type="file" name=“p_my_file“ /> (...) In DB: procedure my_proc (p_my_file IN varchar2, …) is lrt_docdocuments_table%ROWTYPE; begin select * into lrt_doc from documents_table where name=p_my_file; (…) File upload
  • 34. Do not keep documents in the uploaded documents table Avoid collisions Provide distinct access privileges Backup polices Distinct Business logic … Best practice (2)
  • 35. 1) Send Header info OWA_UTIL.mime_header(prt_doc.mime_type, FALSE); -- specify mime_type and do not close header HTP.p ('Last-Modified: ' ||pv_last_updated); -- allow cache HTP.p ('Content-Length: ' ||prt_doc.doc_size); -- download info HTP.p ('Content-disposition: attachment; filename=“'|| prt_doc.filename||'” '); -- “save As” OWA_UTIL.http_header_close; --close header File download
  • 36. 2) Send Data 2a) If BLOB call: DECLARE lb_lob BLOB := prt_doc.content; -- due to need to be an IN OUT BEGIN WPG_DOCLOAD.download_file (p_blob => lb_lob); END; 2b) If CLOB: DECLARE lb_lob BLOB := clob_util.clob2blob (p_clob); BEGIN WPG_DOCLOAD.download_file (p_blob => lb_lob); END; File download
  • 37. DECLARE offset PLS_INTEGER; len PLS_INTEGER; amount PLS_INTEGER := 32000; BEGIN len := NVL (DBMS_LOB.getlength (c_lob), 0); FOR i IN0 .. len / amount LOOP offset := (amount * i) + 1; HTP.prn(DBMS_LOB.SUBSTR(c_lob, amount , offset)); ENDLOOP; END; File download Or2b) If CLOB
  • 38. FUNCTION clob2blob (p_clob IN CLOB) RETURN BLOB DETERMINISTICIS lbl_blob BLOB; li_dest_offset INTEGER := 1; li_src_offset INTEGER := 1; li_amount INTEGER := DBMS_LOB.lobmaxsize; ln_blob_csid NUMBER := DBMS_LOB.default_csid; li_lang_ctx INTEGER := DBMS_LOB.default_lang_ctx; li_warning INTEGER; BEGIN DBMS_LOB.createtemporary (lob_loc => lbl_blob, cache => TRUE, dur => DBMS_LOB.call); DBMS_LOB.converttoblob (dest_lob => lbl_blob ,src_clob => p_clob ,amount => li_amount ,dest_offset => li_dest_offset ,src_offset => li_src_offset ,blob_csid => ln_blob_csid ,lang_context => li_lang_ctx ,warning => li_warning); RETURN lbl_blob; END clob2blob; CLOB 2 BLOB
  • 39. An Example: OWA_UTIL.mime_header ('text/css', FALSE); -- sends mime-type -- and doesn’t closes the header HTP.P ('Last-Modified: ' || lv_updated_date); HTP.P ('Cache-Control: public, s-maxage=300, max-age=86400'); -- or -- HTP.p ('Cache-Control: private, max-age=86400'); --or -- HTP.p (‘Cache-Control: no-cache’); --or even no-store OWA_UTIL.http_header_close; --close the header (…) Cache – Last Modified
  • 40. -- Thu, 30 Sep 2010 16:03:22 GMT lv_updated_date :=TO_CHAR ( SYS_EXTRACT_UTC ( CAST (NVL (pd_last_modified, SYSDATE) AS TIMESTAMP)), 'Dy, DD Mon YYYY HH24:MI:SS', 'NLS_DATE_LANGUAGE=''AMERICAN''') || ' GMT'; Last-Modified
  • 41. Then I can check if the browser has the document in cache IF OWA_UTIL.get_cgi_env ('HTTP_IF_MODIFIED_SINCE') = lv_updated_date THEN OWA_UTIL.status_line (nstatus => 304, creason => 'Not Modified', bclose_header => TRUE); RETURN; END IF; Improve DB use (2)
  • 42. Can also use ETag: HTP.p('ETag: ' || lv_file_etag); -- or owa_cache.set_cache(p_etag, p_level); --  length(lv_file_etag)<= 55 And to check: OWA_UTIL.get_cgi_env('HTTP_IF_NONE_MATCH') --needs PlsqlCGIEnvironmentList HTTP_IF_NONE_MATCH Cache - ETag
  • 43.
  • 44. -> if so return status 503 -> exit
  • 48. Output ContentBest Practices (3)Improve DB use (1+2)
  • 49. If a page can only be access by a POST form -> check it! owa_util.get_cgi_env( 'REQUEST_METHOD' ) =‘POST’ and owa_util.get_cgi_env( 'HTTP_REFERER‘) is not null -- maybe even check that the referer is from the same site or an exact page … If a page is only to be access by a AJAX -> check it! OWA_UTIL.get_cgi_env ('X-Requested-With') is not null -- needs DAD PlsqlCGIEnvironmentList X-Requested-With But an hacker can avoid that if they know of it  Disallow Access
  • 50. begin (…) exceptions (…) whenothersthen qem_web.register_error; end; Error Manager
  • 54. Avoid reporting system messages that the normal user cannot understand but an hacker could use (Error message SQL Injection). (i.e SQLERRM, …) The errors should be reported to a system maintainer even if the user doesn’t report them. Should have enough information to replicate the error. We based our solution on Quest Error Manager (http://qem.inside.quest.com/index.jspa) Error Manager
  • 55. Quest Error Manager Log 4 PLSQL Logger PL/SQL Log & DBugLibrary PLJ_LG … Error and Log Libraries
  • 56. Have an error handler for when others in every procedure that will generate your web pages The ones that are to called by URL They are in a whitelist table, right? With PL/Scope (11g) you can have a script that check the use of your error report program inside the procedures Best Pratices (4)
  • 57. The OWASP Top 10 Web Application Security Risks for 2010 are: A1: Injection A2: Cross-Site Scripting (XSS) A3: Broken Authentication and Session Management A4: Insecure Direct Object References A5: Cross-Site Request Forgery (CSRF) A6: Security Misconfiguration A7: Insecure Cryptographic Storage A8: Failure to Restrict URL Access A9: Insufficient Transport Layer Protection A10: Unvalidated Redirects and Forwards Security
  • 58. One of the more devastating attacks on a web application One of the most common attacks Probably the most popular technique used for database intrusion caused by inefficient input validation Some real World examples (2005-2010): http://en.wikipedia.org/wiki/SQL_injection#Real-world_examples SQL injection: what?
  • 59. PROCEDUREmy_bad_proc (v inVARCHAR2) IS TYPE cursortyp ISREFCURSOR; cursordual cursortyp; result_v VARCHAR2 (300); sql_stmt VARCHAR2 (300); BEGIN sql_stmt := 'select * from dual where dummy=''' || v || '''; DBMS_OUTPUT.put_line (sql_stmt); OPEN cursordual FOR sql_stmt; LOOP FETCH cursordual INTO result_v; EXITWHEN cursordual%NOTFOUND; DBMS_OUTPUT.put_line (result_v); ENDLOOP; END my_bad_proc; Sql injection: example BEGIN my_bad_proc(v=> 'aaa'' union select login from logins -- END; select * from dual where dummy='aaa' union select login from logins -- …… Mark Steven (…)
  • 60. In Oracle when concatenating non-constants to be used as a statement in EXECUTE IMMEDIATE v_string; OPEN v_cursor FOR v_string; DBMS_SQL.parse (..., v_string, …) Safe code: SELECT … INTO … And above if not concatenating with non- constants OPEN … USING var1, var2,… EXECUTE IMMEDIATE …. USING var1, var2,… DBMS_SQL.bind_variable SQL Injection: where?
  • 61. We need to convert the Text value to a SQL text Literal. If needed by design replace all the quotation marks with two quotations replace(v,'''','''''') -- for instance to allow O’Neil Use DBMS_ASSERT.ENQUOTE_LITERAL: Enclose the literal with quotation Check with DBMS_ASSERT.enquote_literal the validy catching the exception VALUE_ERROR it raises if invalid OR Use DBMS_ASSERT.enquote_literal to enclose the literal with in quotation marks and it raises VALUE_ERROR if invalid. Best Pratices (4)
  • 62. PROCEDUREmy_good_proc1 (v inVARCHAR2) IS (…) lv_literal VARCHAR2(100):='''' || REPLACE (v, '''', '''''') || ''''; BEGIN BEGIN lv_literal :=sys.DBMS_ASSERT.enquote_literal(lv_literal ); EXCEPTION WHENVALUE_ERROR THENDBMS_OUTPUT.put_line ('Value entered invalid'); RETURN; END; sql_stmt := 'select * from dual where dummy=' || lv_literal; OPEN cursordual FOR sql_stmt; (…) END my_good_proc1; DBMS_ASSERT.enquote_literal (1)
  • 63. PROCEDUREmy_good_proc2(v VARCHAR2) IS (…) BEGIN BEGIN sql_stmt := 'select * from dual where dummy=' ||sys.DBMS_ASSERT.enquote_literal(v); EXCEPTION WHENVALUE_ERROR THEN DBMS_OUTPUT.put_line ('Value entered invalid'); RETURN; END; OPEN cursordual FORsql_stmt; (...) END my_good_proc2; DBMS_ASSERT.enquote_literal (2)
  • 64. PROCEDUREmy_good_proc2(v VARCHAR2) IS (…) BEGIN BEGIN sql_stmt := 'select * from dual where dummy=‘ ||sys.DBMS_ASSERT.enquote_literal(REPLACE (v, '''', '''''') ); EXCEPTION WHENVALUE_ERROR THEN DBMS_OUTPUT.put_line ('Value entered invalid'); RETURN; END; OPEN cursordual FORsql_stmt; (...) END my_good_proc2; DBMS_ASSERT.enquote_literal (2)
  • 65. DBMS_ASSERT SIMPLE_SQL_NAME QUALIFIED_SQL_NAME SCHEMA_NAME SQL_OBJECT_NAME ENQUOTE_LITERAL – single quotes check ENQUOTE_NAME – double quotes check
  • 66. Protect the database and web server (patches) Use minimum privileges AUTHID CURRENT_USER when possible Use of prepared statement Data API Sanitize Input: from users URL parameters Cookies avoid cursor injection (11g): DBMS_SQL.open_cursor(security_level => 2); SQL Injection: Prevention
  • 67. The OWASP Top 10 Web Application Security Risks for 2010 are: A1: Injection A2: Cross-Site Scripting (XSS) A3: Broken Authentication and Session Management A4: Insecure Direct Object References A5: Cross-Site Request Forgery (CSRF) A6: Security Misconfiguration A7: Insecure Cryptographic Storage A8: Failure to Restrict URL Access A9: Insufficient Transport Layer Protection A10: Unvalidated Redirects and Forwards Security
  • 68. XSS is a form of injection where the interpreter is the browser and attacks are “buried” in the HTML document Cross-Site Scripting (XSS)
  • 69. Sanitize input HTML Escapeuntrusted data (htf.escape_sc) if HTML input allowed…check it Use the new Content Security Policy header Examples: If no external script and no inline-script X-Content-Security-Policy: allow 'self'; If inline script exists but no external script X-Content-Security-Policy: allow 'self'; options inline-script; Cross-Site Scripting (XSS): Prevention
  • 70. Avoid iframe/frame X-Frame-Options: [deny | sameOrigin] X-Content-Security-Policy: frame-src: ... HTTPOnly cookie flag (OAS version >= 10.1.3.5.0) ... Cross-Site Scripting (XSS): Prevention (cont.)
  • 71. The OWASP Top 10 Web Application Security Risks for 2010 are: A1: Injection A2: Cross-Site Scripting (XSS) A3: Broken Authentication and Session Management A4: Insecure Direct Object References A5: Cross-Site Request Forgery (CSRF) A6: Security Misconfiguration A7: Insecure Cryptographic Storage A8: Failure to Restrict URL Access A9: Insufficient Transport Layer Protection A10: Unvalidated Redirects and Forwards Security
  • 72. Password Strength Password storage encrypted Login attemps restricted (by time/tries) Avoid Cross-Site Scripting (XSS) No session Id in URL Protecting Credentials in Transit: cookies secure (HTTPS) AUTOCOMPLETE=OFF in authentication forms … Broken Authentication and Session Management : Prevention
  • 73. DBMS_EXPORT_EXTENSION DBMS_CDC_IMPDP DBMS_CDC_ISUBSCRIBE DBMS_CDC_SUBSCRIBE DRILOAD SDO_TOPO_DROP_FTBL SQL LT DBMS_METADATA ALTER SESSION SET NLS_DATE_FORMAT (lateral SQL Injection) Advice: check Oracle Security bugs
  • 74. Mod_plsql in Oracle HTTP Server (since 8i) (Build on Apache 1.3 or 2.2) Embedded PL/SQL Gateway (DBMS_EPG) Apex Listener Doug McMahon’s Mod_owa Thoth Gateway in Microsoft Internet Information Server DBPrism Servlet engine (Tomcat,…) JOPA Gateway Servlet(Tomcat,…) Total Knowledge’s Mod_plsql Alternative PL/SQL Gateways
  • 75. Web Request processing File upload/ download and generation Best Practices HTP/HTF replacer Error management Improve DB use (HEAD, client file caching) Security Black listing, white listing How to avoid SQL Injection PL/SQL Gateways alternatives Summary
  • 76. Questions? Filipe Silva Silva . Filipe @ gmail . com Blog: http://oracleblues.blogspot.com Label: Oracle Blues Faculdade de Engenharia - Universidade do Porto Portugal Thanks for listening!