All files / lib/Service CredentialFilterService.js

61.48% Statements 91/148
55.7% Branches 83/149
70.83% Functions 17/24
62.22% Lines 84/135

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  1x 4x 4x 4x 4x   4x         1x 2x       1x 1x 1x         1x   1x 2x 2x 4x 2x 2x     1x 1x 1x 1x 1x   1x 1x 1x 1x 1x 1x 1x 1x 1x                     48x 8x 4x   8x   40x           1x     16x 16x 32x   3x 2x   3x     29x 4x   25x     1x   24x 24x   24x   23x             23x   1x   22x   4x   18x 15x   3x 1x       2x         2x         2x         2x                 1x     16x   1x                                                                                                               25x     23x       5x                 1x   1x           1x   1x           1x   1x           3x 3x     3x 1x   2x     1x 1x         1x  
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    Eif (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    Eif (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        Iif (mod && mod.__esModule) return mod;
        var result = {};
        Eif (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) Eif (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.CredentialFilterService = exports.FILTERS = void 0;
const core_1 = require("@zxcvbn-ts/core");
const zxcvbnEnPackage = __importStar(require("@zxcvbn-ts/language-en"));
const zxcvbnCommonPackage = __importStar(require("@zxcvbn-ts/language-common"));
var FILTERS;
(function (FILTERS) {
    FILTERS[FILTERS["SHOW_ALL"] = 0] = "SHOW_ALL";
    FILTERS[FILTERS["COMPROMISED"] = 1] = "COMPROMISED";
    FILTERS[FILTERS["STRENGTH_LOW"] = 2] = "STRENGTH_LOW";
    FILTERS[FILTERS["STRENGTH_MEDIUM"] = 3] = "STRENGTH_MEDIUM";
    FILTERS[FILTERS["STRENGTH_GOOD"] = 4] = "STRENGTH_GOOD";
    FILTERS[FILTERS["EXPIRED"] = 5] = "EXPIRED";
    FILTERS[FILTERS["DELETED"] = 6] = "DELETED";
    FILTERS[FILTERS["ENCRYPTION_BROKEN"] = 7] = "ENCRYPTION_BROKEN"; // use FILTERS.ENCRYPTION_BROKEN only when you really know what you are doing
})(FILTERS || (exports.FILTERS = FILTERS = {}));
class CredentialFilterService {
    /**
     * This will not call any logging methods, it's up to the caller or callback to do that.
     *
     * @param credential The credential to check/run hasUnspecifiedEncryptionError() for
     * @param onEncryptionErrorSkippedCredential Callback to call if the credential has an unspecified encryption error
     * @returns True if the credential has an unspecified encryption error, false otherwise
     */
    static checkAndReportEncryptionError(credential, onEncryptionErrorSkippedCredential) {
        if (credential.hasUnspecifiedEncryptionError()) {
            if (onEncryptionErrorSkippedCredential) {
                onEncryptionErrorSkippedCredential(credential);
            }
            return true;
        }
        return false;
    }
    /**
     * Get a filtered list of credentials based on the given filter and additional filters.
     * Won't include credentials with any ((un)specified) encryption errors, except for the explicit ENCRYPTION_BROKEN filter.
     */
    static getFilteredCredentials = (allCredentials, filter, additionalFilterText, additionalFilterTags, 
    /** Each additional filter has to be true, otherwise the credential will not be added to the filtered list */
    additionalFilters, onEncryptionErrorSkippedCredential) => {
        let filtered = [];
        for (const credential of allCredentials) {
            if (filter === FILTERS.ENCRYPTION_BROKEN) {
                // do not care about other major categories (like hidden / deleted / text) for this filter option
                if (credential.hasUnspecifiedEncryptionError()) {
                    filtered.push(credential);
                }
                continue;
            }
            // never include already-known broken credentials in normal filter results
            if (this.checkAndReportEncryptionError(credential, onEncryptionErrorSkippedCredential)) {
                continue;
            }
            if (CredentialFilterService.isHidden(credential)) {
                // hidden credentials are never shown in the list
                // matches usually only the (required) first test credential
                continue;
            }
            const additionalTextFilterEnabled = additionalFilterText !== undefined && additionalFilterText !== '';
            const additionalTagsFilterEnabled = additionalFilterTags !== undefined && additionalFilterTags.length > 0;
            // filter text if possible, otherwise ignore filter and proceed
            if ((!additionalTextFilterEnabled || (additionalTextFilterEnabled && CredentialFilterService.matchesFilterText(credential, additionalFilterText))) &&
                (!additionalTagsFilterEnabled || (additionalTagsFilterEnabled && CredentialFilterService.matchesFilterTags(credential, additionalFilterTags)))) {
                Iif (filter === FILTERS.DELETED) {
                    // deleted credentials will not match any other category
                    if (CredentialFilterService.isDeleted(credential)) {
                        filtered.push(credential);
                    }
                }
                else {
                    if (CredentialFilterService.isDeleted(credential)) {
                        // deleted credentials will not match any other category
                        continue;
                    }
                    if (additionalFilters && additionalFilters.length > 0 && !additionalFilters.every(additionalFilter => additionalFilter(credential))) {
                        // each additional filter has to be true, otherwise skip this credential
                        continue;
                    }
                    if (filter === FILTERS.SHOW_ALL && !this.checkAndReportEncryptionError(credential, onEncryptionErrorSkippedCredential)) {
                        filtered.push(credential);
                    }
                    else if (filter === FILTERS.COMPROMISED) {
                        Iif (CredentialFilterService.isCompromised(credential) && !this.checkAndReportEncryptionError(credential, onEncryptionErrorSkippedCredential)) {
                            filtered.push(credential);
                        }
                    }
                    else Iif (filter === FILTERS.STRENGTH_LOW) {
                        if (CredentialFilterService.hasLowStrength(credential) && !this.checkAndReportEncryptionError(credential, onEncryptionErrorSkippedCredential)) {
                            filtered.push(credential);
                        }
                    }
                    else Iif (filter === FILTERS.STRENGTH_MEDIUM) {
                        if (CredentialFilterService.hasMediumStrength(credential) && !this.checkAndReportEncryptionError(credential, onEncryptionErrorSkippedCredential)) {
                            filtered.push(credential);
                        }
                    }
                    else Iif (filter === FILTERS.STRENGTH_GOOD) {
                        if (CredentialFilterService.hasGoodStrength(credential) && !this.checkAndReportEncryptionError(credential, onEncryptionErrorSkippedCredential)) {
                            filtered.push(credential);
                        }
                    }
                    else Iif (filter === FILTERS.EXPIRED) {
                        if (CredentialFilterService.isExpired(credential)) {
                            filtered.push(credential);
                        }
                    }
                }
            }
            else {
                // cover encryption error handling of failed/negative matchesFilterText() or matchesFilterTags() calls
                this.checkAndReportEncryptionError(credential, onEncryptionErrorSkippedCredential);
            }
        }
        return filtered;
    };
    static getFilterStats = (allCredentials) => {
        const stats = {
            allVisible: 0,
            compromised: 0,
            strengthLow: 0,
            strengthMedium: 0,
            strengthGood: 0,
            expired: 0,
            deleted: 0,
            encryptionBroken: 0
        };
        const options = {
            translations: zxcvbnEnPackage.translations,
            graphs: zxcvbnCommonPackage.adjacencyGraphs,
            dictionary: {}, // do not use dictionaries for a faster evaluation
        };
        core_1.zxcvbnOptions.setOptions(options);
        const now = Date.now();
        for (const credential of allCredentials) {
            if (!CredentialFilterService.isHidden(credential)) {
                if (CredentialFilterService.isDeleted(credential)) {
                    stats.deleted++;
                }
                else {
                    stats.allVisible++;
                    if (CredentialFilterService.isCompromised(credential)) {
                        stats.compromised++;
                    }
                    if (CredentialFilterService.isExpired(credential, now)) {
                        stats.expired++;
                    }
                    if (typeof credential.password === 'string') {
                        // do not categorize credential by strength if it has no primary password set
                        // zxcvbn will fail for non-string passwords (null/undefined/corrupt decrypt)
                        const zxcvbnResult = (0, core_1.zxcvbn)(credential.password);
                        if (zxcvbnResult.score >= 3) {
                            stats.strengthGood++;
                        }
                        else if (zxcvbnResult.score == 2) {
                            stats.strengthMedium++;
                        }
                        else {
                            // use strength low also as fallback, if zxcvbnResult is < 0
                            stats.strengthLow++;
                        }
                    }
                }
            }
            // count all types of credentials (also hidden), if its encryption is already known to be broken
            if (credential.hasUnspecifiedEncryptionError()) {
                stats.encryptionBroken++;
            }
        }
        return stats;
    };
    static isHidden(credential) {
        return credential.hidden;
    }
    static isDeleted(credential) {
        return credential.delete_time !== null && credential.delete_time > 0;
    }
    static isCompromised(credential) {
        // since we have unclear truthiness for compromised, we need to do the negative checks
        return credential.compromised !== undefined && credential.compromised !== null && credential.compromised != false;
    }
    static isExpired(credential, now) {
        if (now === undefined) {
            now = Date.now();
        }
        return credential.expire_time !== 0 && credential.expire_time * 1000 <= now;
    }
    static hasLowStrength(credential) {
        Eif (typeof credential.password !== 'string') {
            // do not categorize credential by strength if it has no primary password set
            return false;
        }
        const zxcvbnResult = (0, core_1.zxcvbn)(credential.password);
        return zxcvbnResult.score <= 1;
    }
    static hasMediumStrength(credential) {
        Eif (typeof credential.password !== 'string') {
            // do not categorize credential by strength if it has no primary password set
            return false;
        }
        const zxcvbnResult = (0, core_1.zxcvbn)(credential.password);
        return zxcvbnResult.score == 2;
    }
    static hasGoodStrength(credential) {
        Eif (typeof credential.password !== 'string') {
            // do not categorize credential by strength if it has no primary password set
            return false;
        }
        const zxcvbnResult = (0, core_1.zxcvbn)(credential.password);
        return zxcvbnResult.score >= 3;
    }
    static matchesFilterText(credential, filterText) {
        filterText = filterText.toLowerCase();
        Iif (typeof credential.label === 'string' && credential.label.toLowerCase().includes(filterText)) {
            return true;
        }
        if (typeof credential.description === 'string' && credential.description.toLowerCase().includes(filterText)) {
            return true;
        }
        return false;
    }
    static matchesFilterTags(credential, filterTags) {
        Eif (!Array.isArray(credential.tags)) {
            return false;
        }
        return credential.tags.filter(tag => tag.text ? filterTags.includes(tag.text) : false).length > 0;
    }
}
exports.CredentialFilterService = CredentialFilterService;