All files / lib/esm/Service CredentialFilterService.js

0% Statements 0/107
0% Branches 0/112
0% Functions 0/15
0% Lines 0/105

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                                                                                                                                                                                                                                                                                                                                                                                                                                                                   
import { zxcvbn, zxcvbnOptions } from '@zxcvbn-ts/core';
import * as zxcvbnEnPackage from "@zxcvbn-ts/language-en";
import * as zxcvbnCommonPackage from "@zxcvbn-ts/language-common";
export 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 || (FILTERS = {}));
export 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)))) {
                if (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) {
                        if (CredentialFilterService.isCompromised(credential) && !this.checkAndReportEncryptionError(credential, onEncryptionErrorSkippedCredential)) {
                            filtered.push(credential);
                        }
                    }
                    else if (filter === FILTERS.STRENGTH_LOW) {
                        if (CredentialFilterService.hasLowStrength(credential) && !this.checkAndReportEncryptionError(credential, onEncryptionErrorSkippedCredential)) {
                            filtered.push(credential);
                        }
                    }
                    else if (filter === FILTERS.STRENGTH_MEDIUM) {
                        if (CredentialFilterService.hasMediumStrength(credential) && !this.checkAndReportEncryptionError(credential, onEncryptionErrorSkippedCredential)) {
                            filtered.push(credential);
                        }
                    }
                    else if (filter === FILTERS.STRENGTH_GOOD) {
                        if (CredentialFilterService.hasGoodStrength(credential) && !this.checkAndReportEncryptionError(credential, onEncryptionErrorSkippedCredential)) {
                            filtered.push(credential);
                        }
                    }
                    else if (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
        };
        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 = 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) {
        if (typeof credential.password !== 'string') {
            // do not categorize credential by strength if it has no primary password set
            return false;
        }
        const zxcvbnResult = zxcvbn(credential.password);
        return zxcvbnResult.score <= 1;
    }
    static hasMediumStrength(credential) {
        if (typeof credential.password !== 'string') {
            // do not categorize credential by strength if it has no primary password set
            return false;
        }
        const zxcvbnResult = zxcvbn(credential.password);
        return zxcvbnResult.score == 2;
    }
    static hasGoodStrength(credential) {
        if (typeof credential.password !== 'string') {
            // do not categorize credential by strength if it has no primary password set
            return false;
        }
        const zxcvbnResult = zxcvbn(credential.password);
        return zxcvbnResult.score >= 3;
    }
    static matchesFilterText(credential, filterText) {
        filterText = filterText.toLowerCase();
        if (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) {
        if (!Array.isArray(credential.tags)) {
            return false;
        }
        return credential.tags.filter(tag => tag.text ? filterTags.includes(tag.text) : false).length > 0;
    }
}