2016-09-29 130 views
0

我想用dbms_store.insertXML將一些已消毒的XML插入到表中。該插入需要存儲特殊字符,而不是他們逃脫equivilant。用dbms_store.insertXML插入特殊字符

示例表:

CREATE TABLE xml_test 
(
    key_num NUMBER, 
    value_desc VARCHAR2 (1000) 
); 

實例功能使用DBMS_XMLSTORE:

CREATE OR REPLACE FUNCTION fn_insertxml 
(
    pi_xmldoc IN CLOB, 
    pi_tablename IN VARCHAR2 
) 
    RETURN NUMBER 
IS 
    v_ctx_type DBMS_XMLSTORE.ctxtype; 
    v_rows NUMBER; 
BEGIN 
    -- Set the context to the table to be inserted into 
    v_ctx_type := DBMS_XMLSTORE.newcontext (pi_tablename); 

    -- Insert document 
    v_rows := DBMS_XMLSTORE.insertxml (v_ctx_type, pi_xmldoc); 

    -- Close the context 
    DBMS_XMLSTORE.closecontext (v_ctx_type); 

    RETURN v_rows; 
END fn_insertxml; 

示例代碼:

DECLARE 
    k_injected_input CONSTANT VARCHAR2 (4000) := q'[<![CDATA[x]]></VALUE_DESC><KEY_NUM>-1</KEY_NUM><VALUE_DESC><![CDATA[example text]'; 
    k_xml_doc CONSTANT VARCHAR2 (4000) := q'[<ROWSET><ROW num='0'><KEY_NUM><![CDATA[1]]></KEY_NUM><VALUE_DESC><![CDATA[%s]]></VALUE_DESC></ROW></ROWSET>]'; 

    v_row_num NUMBER; 
    v_sanitised_input varchar2(4000); 
    v_xml_doc VARCHAR2 (4000); 
BEGIN 
    --Sanitised input 
    v_sanitised_input := DBMS_XMLGEN.CONVERT (k_injected_input, DBMS_XMLGEN.entity_encode); 

    --If I insert this as it is, the sanitized input is stored 
    v_xml_doc := REPLACE (k_xml_doc, '%s', v_sanitised_input); 
    v_row_num := fn_insertxml (v_xml_doc, 'XML_TEST'); 

    --If I attempt to decode the xml it is open to XML injection (key_num is set to -1) 
    v_sanitised_input := DBMS_XMLGEN.CONVERT (v_sanitised_input, DBMS_XMLGEN.entity_decode); 
    v_xml_doc := REPLACE (k_xml_doc, '%s', v_sanitised_input); 
    v_row_num := fn_insertxml (v_xml_doc, 'XML_TEST'); 
END; 

任何人有什麼想法?

回答

0

解決了這個問題。

在不太可能發生的情況下,任何人都會遇到這種情況。 DBMS_XMLSTORE.insertxml會將轉義字符轉換回原始值,只要它們不包含在CDATA中。