Using HTTP POST with typeahead js and the js sniffer

By default, bloodhound.js will request via HTTP GET, but this leaves you vulnerable to JSON hijacking . Since I have sensitive information that I want to upload to typeahead, HTTP GET leaves me vulnerable. Previously, there was an option for selecting a message (as shown here: typeahead.js remote access to the problem report after publication ), but this does not work with the latest version (v.0.11.1).

+4
source share
2 answers

It took me a lot of suffering and experimentation to get this. The latest version (v.0.11.1) has a function function transport, you can use it to delegate to everything you want (websites or a plain old one $.ajaxwith a message).

var accountBloodhound = new Bloodhound({
    datumTokenizer: Bloodhound.tokenizers.obj.whitespace('value'),
    queryTokenizer: Bloodhound.tokenizers.whitespace,

    remote: {
        url: '/account/search#%QUERY',
        wildcard: '%QUERY',
        transport: function (opts, onSuccess, onError) {
            var url = opts.url.split("#")[0];
            var query = opts.url.split("#")[1];
            $.ajax({
                url: url,
                data: "search=" + query,
                type: "POST",
                success: onSuccess,
                error: onError,
            })
        }
    }
});

$('#remote .typeahead').typeahead(null, {
    name: 'best-pictures',
    display: 'value',
    source: accountBloodhound 
}).bind('typeahead:select', function (ev, suggestion) {
    console.log('Selection: ' + suggestion);
});
+8
source

This link will help

var searchAuto = new Bloodhound({
    datumTokenizer: Bloodhound.tokenizers.obj.whitespace('word'),
    queryTokenizer: Bloodhound.tokenizers.whitespace,
    remote: {
        url: base_url + 'ajax/get_words',
        prepare: function (q, rq) {

            $(".typeahead-loader").show();

            rq.data = {
                q: $("#SearchData").val(),
                // source: 'S01',
                searchtype: $("#Searchtype").val(),
                language: $("#language").val(),
                author: $("#author").val(),
                raag: $("#raag").val(),
                page_from: $("#page_from").val(),
                page_to: $("#page_to").val()
            };
            return rq;
        },
        transport: function (obj, onS, onE) {

            $.ajax(obj).done(done).fail(fail).always(always);

            function done(data, textStatus, request) {
                // Don't forget to fire the callback for Bloodhound
                onS(data);
            }

            function fail(request, textStatus, errorThrown) {
                // Don't forget the error callback for Bloodhound
                onE(errorThrown);
            }

            function always() {
                $(".typeahead-loader").hide();
            }
        }
    }
});

if you execute console.log obj, i.e. first parameter you get

enter image description here

and you can easily override typeinobj

obj.type = 'POST'

Hope this helps ...

+2
source

Source: https://habr.com/ru/post/1611841/


All Articles