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

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -