We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 9207 ☆ A M B ☆
    • 2,475 Posts
    So SQL injection is prevented so long as we use xPDO and prepared statements etc... but what about XSS stuff? E.g. repopulating a field. Are there some built-in functions to sanitize data for this?
      • 18373 ☆ A M B ☆
      • 3,141 Posts
      The best XSS prevention I know off is running stuff through
      htmlentities($string, ENT_QUOTES, 'UTF-8');

      and making sure your site is served up with a meta charset UTF-8 tag, or even better the header.
        Mark Hamstra • Developer spending his days working on Premium Extras and a MODX Site Dashboard with the ability to remotely upgrade MODX and extras to make the MODX world a little better.

        Tweet me @mark_hamstra, check my infrequent blog at markhamstra.com, my slightly more frequent ramblings at MODX.today or see code at Github.
        • 9207 ☆ A M B ☆
        • 2,475 Posts
        Hmmm... does that prevent against the normal litany of clever XSS workarounds? E.g. http://wiremask.eu/?p=tutorials&id=10

        In the past I've copied some of the XSS filtering functions from CodeIgniter, and that's been pretty effective, but it's a slog.
          • 3749
          • 24,544 Posts
          I also add this to prevent the execution of snippets and exposure of System Settings when allow_tags_in_post is on:

          $post = str_replace(array('[',']'),array('[',']'),$post);
            Did I help you? Buy me a beer
            Get my Book: MODX:The Official Guide
            MODX info for everyone: http://bobsguides.com/modx.html
            My MODX Extras
            Bob's Guides is now hosted at A2 MODX Hosting
            • 9207 ☆ A M B ☆
            • 2,475 Posts
            Clever. Say more about that, Bob. So if someone gets clever, they can post a search term like "[[++super_secret_system_setting]]" and if you're results page prints the unfiltered search term, you could inadvertently expose your super secret system settings?

            What exactly does your str_replace function there do? Did your entities get converted? Because that doesn't look like it would do anything... or???
              • 3749
              • 24,544 Posts
              The forum mangled my code. The replacement terms should be

              array('& #91;','& #93;'),$post)


              Without the space after the &.
                Did I help you? Buy me a beer
                Get my Book: MODX:The Official Guide
                MODX info for everyone: http://bobsguides.com/modx.html
                My MODX Extras
                Bob's Guides is now hosted at A2 MODX Hosting
                • 3749
                • 24,544 Posts
                I should also mention $modx->sanitizeRequest(), which pretty much clears everything suspicious (including MODX tags if the allow_tags_in_post System Setting is set to false). It's called on every request to sanitize the $_POST data.

                public function sanitizeRequest() {
                        $modxtags = array_values($this->modx->sanitizePatterns);
                        modX :: sanitize($_GET, $modxtags, 0);
                        if ($this->modx->getOption('allow_tags_in_post',null,true)) {
                            modX :: sanitize($_POST);
                        } else {
                            modX :: sanitize($_POST, $modxtags);
                        }
                        modX :: sanitize($_COOKIE, $modxtags);
                        modX :: sanitize($_REQUEST, $modxtags);
                        $rAlias = $this->modx->getOption('request_param_alias', null, 'q');
                        if (isset ($_GET[$rAlias])) {
                            $_GET[$rAlias] = preg_replace("/[^A-Za-z0-9_\-\.\/]/", "", $_GET[$rAlias]);
                        }
                    }


                 public static function sanitize(array &$target, array $patterns= array(), $depth= 3, $nesting= 10) {
                        while (list($key, $value)= each($target)) {
                            if (is_array($value) && $depth > 0) {
                                modX :: sanitize($value, $patterns, $depth-1);
                            } elseif (is_string($value)) {
                                if (!empty($patterns)) {
                                    foreach ($patterns as $pattern) {
                                        $nesting = ((integer) $nesting ? (integer) $nesting : 10);
                                        $iteration = 1;
                                        while ($iteration <= $nesting && preg_match($pattern, $value)) {
                                            $value= preg_replace($pattern, '', $value);
                                            $iteration++;
                                        }
                                    }
                                }
                                if (get_magic_quotes_gpc()) {
                                    $target[$key]= stripslashes($value);
                                } else {
                                    $target[$key]= $value;
                                }
                            }
                        }
                        return $target;
                    }
                  Did I help you? Buy me a beer
                  Get my Book: MODX:The Official Guide
                  MODX info for everyone: http://bobsguides.com/modx.html
                  My MODX Extras
                  Bob's Guides is now hosted at A2 MODX Hosting
                  • 9207 ☆ A M B ☆
                  • 2,475 Posts
                  So if you run $modx->sanitizeRequest() anywhere in your Snippet/Plugin, then you should be able to read out the $_POST/$_GET arrays without fear?

                  Is there a System Setting that controls whether that gets done automatically? I remember looking through the modRequest class and I think that gets run inside the handleRequest() function, but that probably only matters inside CMPs, no?
                    • 3749
                    • 24,544 Posts
                    Quote from: Everettg_99 at Jan 18, 2013, 05:16 PM
                    So if you run $modx->sanitizeRequest() anywhere in your Snippet/Plugin, then you should be able to read out the $_POST/$_GET arrays without fear?

                    As I understand it, it gets called on every page request anyway, so the only reason to do any sanitizing is if you have allow_tags_in_post set to true, in which case, you'd want to sanitize the MODX tags.


                    Is there a System Setting that controls whether that gets done automatically? I remember looking through the modRequest class and I think that gets run inside the handleRequest() function, but that probably only matters inside CMPs, no?


                    I think handleRequest() fires on every page load of any kind, but I've never looked that closely so I'm not positive. It always sanitizes the $_POST (no options control it):

                    public function handleRequest() {
                            $this->loadErrorHandler();
                    
                            $this->sanitizeRequest();
                            /* ... */
                    
                      Did I help you? Buy me a beer
                      Get my Book: MODX:The Official Guide
                      MODX info for everyone: http://bobsguides.com/modx.html
                      My MODX Extras
                      Bob's Guides is now hosted at A2 MODX Hosting
                      • 18373 ☆ A M B ☆
                      • 3,141 Posts
                      Quote from: Everettg_99 at Jan 18, 2013, 01:10 PM
                      Hmmm... does that prevent against the normal litany of clever XSS workarounds? E.g. http://wiremask.eu/?p=tutorials&id=10

                      In the past I've copied some of the XSS filtering functions from CodeIgniter, and that's been pretty effective, but it's a slog.

                      As that link says, it only affects htmlentities when using single quotes in the html, so what I use is double quotes and htmlentities. Even then, I think the ENT_QUOTES param properly escapes both, and the charset param and header or meta tag ensures weird UTF-7-like XSS/CRSF attacks.

                      sanitizeRequest should indeed be run on every request, and allow_tags_in_post shouldn't really be enabled in 99% of the sites. It's by default enabled in the mgr context only which is where it should be needed.
                        Mark Hamstra • Developer spending his days working on Premium Extras and a MODX Site Dashboard with the ability to remotely upgrade MODX and extras to make the MODX world a little better.

                        Tweet me @mark_hamstra, check my infrequent blog at markhamstra.com, my slightly more frequent ramblings at MODX.today or see code at Github.