c# - How to avoid XML Injection when using WriteElementString -
i have following code , throws xml injection(please see highlighted text) .can let me know how can removed
private string getrecordset(oledbdatareader odr) { xmltextwriter xtwriter = null; stringwriter oswriter = null; int ireccnt=0; string sname = string.empty; string svalue = string.empty; try { //create out xml oswriter = new stringwriter(); xtwriter = new xmltextwriter(oswriter); xtwriter.formatting = formatting.indented; xtwriter.writestartelement("session"); while(odr.read()) { ireccnt++; sname = odr.getvalue(0).tostring(); svalue = odr.getvalue(1).tostring(); **xtwriter.writeelementstring(sname, svalue);** } xtwriter.writeelementstring("totalrecords", ireccnt.tostring()); xtwriter.writeendelement(); //rowset end //return xml string. if no records found return empty string string srtrn = oswriter.tostring(); if (ireccnt == 0) srtrn = string.empty; return srtrn; }
you're not sanitizing input in way. xml injectionable because of xml metacharacters can used change code behavior unintended. examples of metacharacters: single quote, double quote, <, > - when put through code cause code write xml elements or attributes unintended.
Comments
Post a Comment