位置图XSS攻击

问题描述 投票:0回答:1

我在 Drupal 工作,我有一个带有输入字段的页面来搜索您的位置。现在,可以进行 XSS 攻击,我正在尝试使用消毒功能来修复它,但它似乎没有做任何事情,我不明白为什么。你们中的任何人都可以查看代码并帮助我吗?这是为了我的实习,如果我能解决这个问题那就太好了。这是我的代码 atm,我得到一个控制台:

Sanitized input: alert("XSS attack!");
。看起来可行,但如果我将其放入输入字段,我仍然会在浏览器中收到警报:
<script>alert("XSS attack!");</script>

function sanitizeInput(input) {
        if (typeof input !== 'string') {
          return '';
        }

        // Remove any HTML tags and attributes using a regular expression
        var sanitizedInput = input.replace(/<[^>]*>?/gm, '');

        // Escape HTML entities
        sanitizedInput = sanitizedInput.replace(/[&<>"']/g, function(match) {
          return {
            '&': '&amp;',
            '<': '&lt;',
            '>': '&gt;',
            '"': '&quot;',
            "'": '&#39;'
          }[match];
        });

        return sanitizedInput;
      }
      
      function onPlaceChanged() {
        var place = autocomplete.getPlace();
        var input = document.getElementById('autocomplete').value;

        if (place === undefined || input === '') {
          return;
        }

        // Sanitize input to prevent XSS attacks
        var sanitizedInput = sanitizeInput(input);
        console.log('Sanitized input:', sanitizedInput);

        if (place !== undefined && place.geometry) {

          var coordinates = {
            lat: place.geometry.location.lat(),
            lng: place.geometry.location.lng()
          };

          findClosestOffice(coordinates);
        } else {
          var locationName;
          if (place !== undefined) {
            locationName = place.name;
          } else {
            locationName = input;
          }

          var geocoder = new google.maps.Geocoder();
          geocoder.geocode({
            componentRestrictions: countryRestrict,
            'address': locationName
          }, function (results, status) {
            if (status === 'OK') {
              var coordinates = {
                lat: results[0].geometry.location.lat(),
                lng: results[0].geometry.location.lng()
              };
              findClosestOffice(coordinates);
            }
          });
        }

        dataLayer.push({
          'event': 'officeSearch',
          'searchTerm': input
        });

        if (input){
          var location = input.split(', ');
          if(offices_tags.hasClass('d-none')) {
            offices_tags.removeClass('d-none');
          }
          offices_tag_label.html(location[0]);
        } else {
          offices_tag_label.html('');
        }

        return;
      } 
javascript xss
1个回答
0
投票

我没有正确使用我的 sanitizedInput。我将“输入”变量更改为“sanitizedInput”,现在它可以工作了。

© www.soinside.com 2019 - 2024. All rights reserved.