Common stuff for all my devices' web server pages: css, login, log, ipv4, ipv6, firmware update, clock, reset info etc.

Dependents:   oldheating gps motorhome heating

Security

A password has to be set whenever there has been a software reset. Resets following faults or power on do not require a new password as the hash is restored from the RTC GPREG register.

The password is not saved on the device; instead a 32 bit hash of the password is saved. It would take 2^31 attempts to brute force the password: this could be done in under a month if an attempt were possible every millisecond. To prevent this a 200 ms delay is introduced in the reply to the login form, that gives a more reasonable 13 years to brute force the password.

Once the password is accepted a random session id is created. This is 36 bit to give six base 64 characters but without an extra delay. If an attempt could be made every ms then this would still take over a year to brute force.

The most likely attack would to use a dictionary with, say, 10 million entries against the password which would still take 20 days to do.

common/web-ajax-class.js

Committer:
andrewboyson
Date:
2019-07-31
Revision:
127:bd6dd135009d
Parent:
113:23507d14f927
Child:
137:3b6632374855

File content as of revision 127:bd6dd135009d:

//Ajax class
'use strict';

//Exposed properties
let ajaxResponse_   = '';
let ajaxHeaders_    = '';
let ajaxDate_       = null;
let ajaxMs_         =  0;
let ajaxOnResponse_ = null;
let ajaxOnTick_     = null;
let ajaxServer_     = '';

//Private variables
let   ajaxOverrideBlockUpdateOnFocus_ = false;
let   ajaxXhr_                        =  null;
let   ajaxMsCountAtAjaxSend_          =     0;
const ajaxTickMs_                     =   100;
const ajaxUpdateMs_                   = 10000;

//Private utilities
function ajaxGetElementOrNull_(elementName) //Returns the element if it: exists; block is overidden; does not have focus
{
    let elem = document.getElementById(elementName);
    if (!elem) return null;
    if (ajaxOverrideBlockUpdateOnFocus_) return elem;
    if (elem !== document.activeElement) return elem;
    return null;
}
function ajaxHexToBit_(text, iBit)
{
   let value = parseInt(text, 16);
   value >>= iBit;
   return value & 1;
}
function ajaxHexToSignedInt8_(text)
{
    let value = parseInt(text, 16);
    if (value < 0x80) return value;
    return value - 0x100;
}
function ajaxHexToSignedInt16_(text)
{
    let value = parseInt(text, 16);
    if (value < 0x8000) return value;
    return value - 0x10000;
}
function ajaxHexToSignedInt32_(text)
{
    let value = parseInt(text, 16);
    if (value < 0x80000000) return value;
    return value - 0x100000000;
}


//Private ajax functions
function ajaxHandleAjaxResponse_()
{
   if (ajaxXhr_.readyState == 4 && ajaxXhr_.status == 200)
   {
        ajaxResponse_ = ajaxXhr_.responseText;
        ajaxHeaders_  = ajaxXhr_.getAllResponseHeaders();
        let iDateStart = Ajax.headers.toLowerCase().indexOf('date:');
        let iDateEnd   = Ajax.headers.indexOf('\r', iDateStart);
        ajaxDate_      = new Date(Ajax.headers.slice(iDateStart + 5, iDateEnd));

        let elem;
        elem = ajaxGetElementOrNull_('ajax-response'   ); if (elem) elem.textContent = ajaxResponse_;
        elem = ajaxGetElementOrNull_('ajax-headers'    ); if (elem) elem.textContent = ajaxHeaders_;
        elem = ajaxGetElementOrNull_('ajax-date-local' );
        if (elem)
        {
            elem.textContent = ajaxDate_.toLocaleString(    undefined, {  weekday     : 'short'  ,
                                                                          day         : '2-digit',
                                                                          month       : 'short'  ,
                                                                          year        : 'numeric',
                                                                          hour        : '2-digit',
                                                                          minute      : '2-digit',
                                                                          timeZoneName: 'short'
                                                                       }
                                                       );
        }
        if (ajaxOnResponse_) ajaxOnResponse_();
        ajaxOverrideBlockUpdateOnFocus_ = false; //Received response so reset override after display
   }
}
function ajaxSendAjaxRequest_(request) //Used by this script and from HTML page
{
    ajaxXhr_ = new XMLHttpRequest();
    ajaxXhr_.onreadystatechange = ajaxHandleAjaxResponse_;
    if (request)
    {
        request = request.split('+').join('%2B');
        ajaxXhr_.open('GET', ajaxServer_ + '?' + request, true);
    }
    else
    {
        ajaxXhr_.open('GET', ajaxServer_                , true);
    }
    ajaxXhr_.send();
    ajaxMsCountAtAjaxSend_ = ajaxMs_;
}
function AjaxRequest(request) //From html
{
    ajaxOverrideBlockUpdateOnFocus_ = true; //Request has come from an update
    ajaxSendAjaxRequest_(request);
}

//Private functions
function ajaxTick_() //Called about every 100ms
{
    ajaxMs_ += ajaxTickMs_; //Don't use Date.now() as we don't know when the PC's clock will be updated around a leap second
    if (ajaxMs_ >= ajaxMsCountAtAjaxSend_ + ajaxUpdateMs_) ajaxSendAjaxRequest_('');
    if (ajaxOnTick_) ajaxOnTick_();
}
function ajaxInit_()
{
    setInterval(ajaxTick_, ajaxTickMs_);
    ajaxSendAjaxRequest_('');
}

//Exposed public
class Ajax
{
    static get ms        ()  { return ajaxMs_         ; }
    static get response  ()  { return ajaxResponse_   ; }
    static get headers   ()  { return ajaxHeaders_    ; }
    static get date      ()  { return ajaxDate_       ; }
    
    static set tickMs    (v) { ajaxTickMs_     = v; }
    static set updateMs  (v) { ajaxUpdateMs_   = v; }
    static set server    (v) { ajaxServer_     = v; }
    static set onResponse(v) { ajaxOnResponse_ = v; }
    static set onTick    (v) { ajaxOnTick_     = v; }

    static getElementOrNull(elementName) { return ajaxGetElementOrNull_(elementName) ; }
    static hexToBit        (text, iBit ) { return ajaxHexToBit_        (text, iBit ) ; }
    static hexToSignedInt8 (text       ) { return ajaxHexToSignedInt8_ (text       ) ; }
    static hexToSignedInt16(text       ) { return ajaxHexToSignedInt16_(text       ) ; }
    static hexToSignedInt32(text       ) { return ajaxHexToSignedInt32_(text       ) ; }
    
    static init()
    {
        if (document.readyState === 'loading') document.addEventListener('DOMContentLoaded', ajaxInit_ ); // Loading hasn't finished yet
        else                                                                                 ajaxInit_(); //`DOMContentLoaded` has already fired
    }
}