Search code examples
javascriptjqueryajaxgetcode-injection

Sending AJAX request to secured websites


I'm currently writing a script that is meant to GET information from a website. What's unique about this website is that it's password protected (user gets prompted an alert with username and password field similar to the prompt when accessing a router). I'm wondering if there's anyway I can have my script inject these fields automatically? Is there anything I can add to the request below to help inject the username and password into the fields? Thanks in advance.

$.ajax({
    url: url,
    type: 'GET',
    success: function(data){
      alert(data);
    }
});

Solution

  • You can inspect the network request in the Network tab of your browser's developer tools and examine the Authentication header in the request to figure out which authentication scheme is in use. Then you should be able to replicate it using some combination of the headers, username and password options to jQuery's Ajax method. Note that you'll likely have to set withCredentials as described on that page too.

    Odd are, however, that you will bounce right off the same origin policy as sites using HTTP authentication are less likely to have granted permission via CORS.