All files legacy.js

85.71% Statements 60/70
78.57% Branches 33/42
100% Functions 10/10
85.07% Lines 57/67

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311                                                        1x             1x 1x               1x               1x                 1x                         1x                           1x                                 201x         201x 106x       95x 185x           8x         87x 220x 41x         46x                           211x 417x 10x     201x                         201x                 101x       100x 100x                 100x                         10x 10x 10x           10x                         157x 157x 156x                     1x   211x                   1x   211x                 1x       211x 10x 10x 10x     201x   101x 1x   101x 101x 101x     100x 45x   100x     100x     1x       1x 1x                           1x      
/**
 * PrivateBin
 *
 * a zero-knowledge paste bin
 *
 * @see       {@link https://github.com/PrivateBin/PrivateBin}
 * @copyright 2012 Sébastien SAUVAGE ({@link http://sebsauvage.net})
 * @license   {@link https://www.opensource.org/licenses/zlib-license.php The zlib/libpng License}
 * @version   1.3.1
 * @name      Legacy
 * @namespace
 */
 
/**
 * IMPORTANT NOTICE FOR DEVELOPERS:
 * The logic in this file is intended to run in legacy browsers. Avoid any use of:
 * - jQuery (doesn't work in older browsers)
 * - ES5 or newer in general
 * - const/let, use the traditional var declarations instead
 * - async/await or Promises, use traditional callbacks
 * - shorthand function notation "() => output", use the full "function() {return output;}" style
 * - IE doesn't support:
 *   - URL(), use the traditional window.location object
 *   - endsWith(), use indexof()
 * - yes, this logic needs to support IE 6, to at least display the error message
 */
 
'use strict';
(function() {
    /**
     * compatibility check
     *
     * @name   Check
     * @class
     */
    var Check = (function () {
        var me = {};
 
        /**
         * Status of the initial check, true means it passed
         *
         * @private
         * @prop   {bool}
         */
        var status = false;
 
        /**
         * Initialization check did run
         *
         * @private
         * @prop   {bool}
         */
        var init = false;
 
        /**
         * blacklist of UserAgents (parts) known to belong to a bot
         *
         * @private
         * @enum   {Array}
         * @readonly
         */
        var badBotUA = [
            'Bot',
            'bot'
        ];
 
        /**
         * whitelist of top level domains to consider a secure context,
         * regardless of protocol
         *
         * @private
         * @enum   {Array}
         * @readonly
         */
        var tld = [
            '.onion',
            '.i2p'
        ];
 
        /**
         * whitelist of hostnames to consider a secure context,
         * regardless of protocol
         *
         * @private
         * @enum   {Array}
         * @readonly
         */
        // whitelists of TLDs & local hostnames
        var hostname = [
            'localhost',
            '127.0.0.1',
            '[::1]'
        ];
 
        /**
         * check if the context is secure
         *
         * @private
         * @name   Check.isSecureContext
         * @function
         * @return {bool}
         */
        function isSecureContext()
        {
            // use .isSecureContext if available
            Iif (window.isSecureContext === true || window.isSecureContext === false) {
                return window.isSecureContext;
            }
 
            // HTTP is obviously insecure
            if (window.location.protocol !== 'http:') {
                return true;
            }
 
            // filter out actually secure connections over HTTP
            for (var i = 0; i < tld.length; i++) {
                if (
                    window.location.hostname.indexOf(
                        tld[i],
                        window.location.hostname.length - tld[i].length
                    ) !== -1
                ) {
                    return true;
                }
            }
 
            // whitelist localhost for development
            for (var j = 0; j < hostname.length; j++) {
                if (window.location.hostname === hostname[j]) {
                    return true;
                }
            }
 
            // totally INSECURE http protocol!
            return false;
        }
 
        /**
         * checks whether this is a bot we dislike
         *
         * @private
         * @name   Check.isBadBot
         * @function
         * @return {bool}
         */
        function isBadBot() {
            // check whether a bot user agent part can be found in the current
            // user agent
            for (var i = 0; i < badBotUA.length; i++) {
                if (navigator.userAgent.indexOf(badBotUA[i]) !== -1) {
                    return true;
                }
            }
            return false;
        }
 
        /**
         * checks whether this is an unsupported browser, via feature detection
         *
         * @private
         * @name   Check.isOldBrowser
         * @function
         * @return {bool}
         */
        function isOldBrowser() {
            // webcrypto support
            if (!(
                'crypto' in window &&
                'getRandomValues' in window.crypto &&
                'subtle' in window.crypto &&
                'encrypt' in window.crypto.subtle &&
                'decrypt' in window.crypto.subtle &&
                'Uint8Array' in window &&
                'Uint32Array' in window
            )) {
                return true;
            }
 
            // async & ES6 support
            try {
                eval('async () => {}');
            } catch (e) {
                if (e instanceof SyntaxError) {
                    return true;
                } else {
                    throw e; // throws CSP error
                }
            }
 
            return false;
        }
 
        /**
         * shows an error message
         *
         * @private
         * @name   Check.showError
         * @param  {string} message
         * @function
         */
        function showError(message)
        {
            var element = document.getElementById('errormessage');
            Eif (message.indexOf('<a') === -1) {
                element.appendChild(
                    document.createTextNode(message)
                );
            } else {
                element.innerHTML = message;
            }
            removeHiddenFromId('errormessage');
        }
 
        /**
         * removes "hidden" CSS class from element with given ID
         *
         * @private
         * @name   Check.removeHiddenFromId
         * @param  {string} id
         * @function
         */
        function removeHiddenFromId(id)
        {
            var element = document.getElementById(id);
            if (element) {
                element.className = element.className.replace(/\bhidden\b/g, '');
            }
        }
 
        /**
         * returns if the check has concluded
         *
         * @name   Check.getInit
         * @function
         * @return {bool}
         */
        me.getInit = function()
        {
            return init;
        };
        
        /**
         * returns the current status of the check
         *
         * @name   Check.getStatus
         * @function
         * @return {bool}
         */
        me.getStatus = function()
        {
            return status;
        };
        
        /**
         * init on application start, returns an all-clear signal
         *
         * @name   Check.init
         * @function
         */
        me.init = function()
        {
            // prevent bots from viewing a paste and potentially deleting data
            // when burn-after-reading is set
            if (isBadBot()) {
                showError('I love you too, bot…');
                init = true;
                return;
            }
 
            if (isOldBrowser()) {
                // some browsers (Chrome based ones) would have webcrypto support if using HTTPS
                if (!isSecureContext()) {
                    removeHiddenFromId('insecurecontextnotice');
                }
                removeHiddenFromId('oldnotice');
                init = true;
                return;
            }
 
            if (!isSecureContext()) {
                removeHiddenFromId('httpnotice');
            }
            init = true;
 
            // only if everything passed, we set the status to true
            status = true;
        };
 
        return me;
    })();
 
    // main application start, called when DOM is fully loaded
    Eif (document.readyState === 'complete' || (!document.attachEvent && document.readyState === 'interactive')) {
        Check.init();
    } else {
        if (document.addEventListener) {
            // first choice is DOMContentLoaded event
            document.addEventListener('DOMContentLoaded', Check.init, false);
            // backup is window load event
            window.addEventListener('load', Check.init, false);
        } else {
            // must be IE
            document.attachEvent('onreadystatechange', Check.init);
            window.attachEvent('onload', Check.init);
        }
    }
 
    this.Legacy = {
        Check: Check
    };
}).call(this);