Ignore:
Timestamp:
2014-03-13T14:34:48+13:00 (10 years ago)
Author:
ak19
Message:

First security commit. 1. Introducing the new securitools.h and .cpp files, which port the functions necessary to implement security in Greenstone from OWASP-ESAPI for Java, since OWASP's C++ version is largely not yet implemented, even though their code compiles. The newly added runtime-src/packages/security which contains OWASP ESAPI for C++ will therefore be removed again shortly. 2. receptionist.cpp now sets various web-encoded variants for each cgiarg macro, such as HTML entity encoded, attr encoded, javascript encoded (and css encoded variants). These are now used in the macro files based on which variant is suited to the context. 3. This commit further contains the minimum changes to protect the c, d, and p cgi variables.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • main/trunk/greenstone2/runtime-src/src/recpt/receptionist.cpp

    r24895 r28888  
    3636#include "gsdltimes.h"
    3737#include "OIDtools.h"
     38#include "securitytools.h"
    3839#include <assert.h>
    3940#include <time.h>
     
    14851486    compressedoptions = to_uni(compressedoptions);
    14861487  }
    1487   disp.setmacro ("decodedcompressedoptions", displayclass::defaultpackage, dm_safe(compressedoptions));
     1488
     1489  text_t dmacrovalue = dm_safe(compressedoptions);
     1490  disp.setmacro ("decodedcompressedoptions", displayclass::defaultpackage, dmacrovalue);
     1491  disp.setmacro ("decodedcompressedoptionsAttrsafe", displayclass::defaultpackage, encodeForHTMLAttr(dmacrovalue));
    14881492
    14891493#if defined (__WIN32__)
     
    14951499  cgiargsclass::const_iterator argsend = args.end();
    14961500  while (argshere != argsend) {
     1501
     1502    text_t macrovalue = (*argshere).second.value; // and stays like that if ((*argshere).first == "hp")
     1503
    14971504    if (((*argshere).first == "q") ||
    14981505    ((*argshere).first == "qa") ||
     
    15021509    ((*argshere).first == "qpl") ||
    15031510    ((*argshere).first == "qr") ||
    1504     ((*argshere).first == "q2"))
     1511    ((*argshere).first == "q2")) {
     1512
    15051513      // need to escape special characters from query string
    1506       disp.setmacro ("cgiarg" + (*argshere).first,
    1507              displayclass::defaultpackage, html_safe((*argshere).second.value));
    1508     else if ((*argshere).first == "hp") {
    1509       disp.setmacro ("cgiarg" + (*argshere).first, displayclass::defaultpackage, (*argshere).second.value);
    1510     } else {
    1511       disp.setmacro ("cgiarg" + (*argshere).first, displayclass::defaultpackage, dm_safe((*argshere).second.value));
    1512     }
     1514      macrovalue = html_safe(macrovalue);
     1515
     1516    } else  if ((*argshere).first == "hp") {
     1517      if(!isValidURLProtocol(macrovalue)) {
     1518    macrovalue = encodeForURL(macrovalue); // URL has invalid protocol like javascript:, so URL encode it
     1519      }
     1520    }
     1521    else {
     1522      macrovalue = dm_safe(macrovalue);
     1523    }   
     1524
     1525    // set the default value for the macro
     1526    disp.setmacro ("cgiarg" + (*argshere).first, displayclass::defaultpackage, macrovalue);
     1527
     1528    // set macros for the encoded versions of the same value. Uses the functions in securitytools.h
     1529    // https://www.owasp.org/index.php/XSS_%28Cross_Site_Scripting%29_Prevention_Cheat_Sheet
     1530
     1531    text_t htmlsafe = encodeForHTML(macrovalue);
     1532    text_t attrsafe = encodeForHTMLAttr(macrovalue);
     1533    text_t urlsafe = encodeForURL(macrovalue);
     1534    text_t jssafe = encodeForJavascript(macrovalue);
     1535    text_t csssafe = encodeForCSS(macrovalue);
     1536
     1537    disp.setmacro ("cgiarg" + (*argshere).first + "Htmlsafe", displayclass::defaultpackage, htmlsafe);   
     1538    disp.setmacro ("cgiarg" + (*argshere).first + "Attrsafe", displayclass::defaultpackage, attrsafe);
     1539    disp.setmacro ("cgiarg" + (*argshere).first + "Jssafe", displayclass::defaultpackage, jssafe);
     1540    disp.setmacro ("cgiarg" + (*argshere).first + "Csssafe", displayclass::defaultpackage, csssafe);
     1541    disp.setmacro ("cgiarg" + (*argshere).first + "Urlsafe", displayclass::defaultpackage, urlsafe);
     1542   
     1543
    15131544    ++argshere;
    15141545  }
Note: See TracChangeset for help on using the changeset viewer.