簡體   English   中英

在啟用 WAF 的情況下阻止 AJAX 在 Amazon ALB 中托管的 webapp 上發布請求 - 以防表單數據包含空格字符

[英]Blocking AJAX Post requests on webapp hosted in Amazon ALB with WAF enabled - in case form data contains space character

我的應用程序托管在 ALB 中並啟用了 waf,在所有 ajax 請求中獲得 403,其中包含“anything space on *”一詞。 例如,如果我有一個輸入為“test one”的文本字段,當相同的數據傳遞給 ajax 數據時出現 403 錯誤。 當我使用 stringify 相同的數據時,錯誤不會出現。

var value = "test one"; //inputFieldText
$ajax({"url":"url",data:value}) // data from input field, if having space on* failed. 

$ajax({"url":"url",data:JSON.stringify(value)}) // if same data is stringified then working fine.

如果 stringify 是唯一的方法,那么我的應用程序中有 1000 個 ajax 請求,有什么解決方法或建議嗎? 請指教。

您可以簡單地覆蓋 jQuery $ajax 並在那里應用JSON.stringify()

const originalAjax = $.ajax;
$.ajax = (...args) => {
    // play with data before calling ajax
    return originalAjax(...args);
};

或者您可以擴展 $ajax function 並在擴展版本中應用您自己的邏輯:

(function(root, factory) {
    if (typeof define == 'function' && define.amd) {
        define(['jquery'], function(jQuery) {
            factory(jQuery);
        });
    } else {
        factory(root.jQuery);
    }
})(this, function(jQuery) {

    var ajax = jQuery.ajax,
    
    function extendedAjax(options) {
        var promise  = $.Deferred();
        var success  = options.success;
        var error    = options.error;
        var complete = options.complete;
        var params, that = this;
        
        params = {
            complete: function(xhr, status) {
                if (complete) complete.apply(that, arguments);
            },
            success: function() {
                if (success) success.apply(that, arguments);
                promise.resolveWith(this, arguments);
            },
            error: function() {
                if (error) error.apply(this, arguments);
                promise.rejectWith(this, arguments);
            }
        };

        ajax(jQuery.extend({}, options, params));

        return promise;
    };
    
    jQuery.ajax = function(options) {
        return extendedAjax(options);
    };

});

這將有助於一勞永逸地實施任何自定義邏輯。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM