summaryrefslogtreecommitdiff
path: root/src/content/KeyRing.ts
blob: 5454f9fec5cae120e891bf1b5495f2059fdadb23 (plain)
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
import * as P from "bluebird";
import { Buffer, KeyFetcher, KeyManager } from "kbpgp";
import { PendingSignerError } from "./PendingSignerError";

const importFromArmoredPgp = P.promisify<KeyManager, { armored: string }>(KeyManager.import_from_armored_pgp, {
    context: KeyManager
});

export default class KeyRing extends KeyFetcher {
    private allKeyIdsForDomainFromKeybase: {
        [kid: string]: {
            keyManager: KeyManager;
            key: any;
            keybaseUser: string;
        };
    } = {};

    private trustedUsers: string[] = [];
    private barredUsers: string[] = [];
    private pendingApproval: string[] = [];

    constructor(private domain: string) {
        super();
    }

    /**
     * Get all the Keybase users that have this domain registered to their account and that
     * we have allowed/denied for this domain before.
     */
    public async getKeybaseUsers(): Promise<string[]> {
        await this.populateKeysForDomain();

        return [
            ...new Set([
                ...Object.values(this.allKeyIdsForDomainFromKeybase).map(k => k.keybaseUser),
                ...(await this.getTrustedUsers()),
                ...(await this.getBarredUsers())
            ])
        ];
    }

    /**
     * Get users that have been previously allowed to run scripts
     * on the `domain` that was used to instantiate this KeyRing.
     */
    public async getTrustedUsers(): Promise<string[]> {
        await this.populateKeysForDomain();

        return this.trustedUsers;
    }

    public addTrustedUser(user: string) {
        this.pendingApproval = this.pendingApproval.filter(v => v !== user);
        this.barredUsers = this.barredUsers.filter(v => v !== user);
        this.trustedUsers.push(user);
        this.storeUsersInStorage();
    }

    /**
     * Get users that have been previously been barred from running scripts
     * on the `domain` that was used to instantiate this KeyRing.
     */
    public async getBarredUsers(): Promise<string[]> {
        await this.populateKeysForDomain();

        return this.barredUsers;
    }

    public addBarredUser(user: string) {
        this.pendingApproval = this.pendingApproval.filter(v => v !== user);
        this.trustedUsers = this.trustedUsers.filter(v => v !== user);
        this.barredUsers.push(user);
        this.storeUsersInStorage();
    }

    public async getPendingApproval(): Promise<string[]> {
        await this.populateKeysForDomain();

        return this.pendingApproval;
    }

    /**
     * Ran by KBPGP to match a key id to a public key. Throws an error if
     * the signer isn't a "trusted" Keybase user
     *
     * @param ids ids to find a public key for
     * @param ops operations that the key should be allowed to run
     * @param cb callback to KBPGP
     */
    public async fetch(ids: Buffer[], ops: any, cb: (err: Error | null, km?: any, keyId?: number) => void) {
        try {
            await this.populateKeysForDomain();
        } catch (e) {
            return cb(e);
        }

        const hexKeyIds = ids.map((f: any) => f.toString("hex"));

        // c-style loop because KBPGP wants an index of `ids` in the callback
        for (let i = 0; i < hexKeyIds.length; i++) {
            const kid = hexKeyIds[i];
            const k = this.allKeyIdsForDomainFromKeybase[kid];

            if (k && k.key && k.key.key) {
                if (this.barredUsers.includes(k.keybaseUser)) {
                    return cb(
                        new Error(`Keybase user ${k.keybaseUser} is barred from signing scripts from ${this.domain}`),
                        k.keyManager,
                        i
                    );
                } else if (this.pendingApproval.includes(k.keybaseUser)) {
                    return cb(
                        new PendingSignerError(`Keybase user ${k.keybaseUser} is not yet approved for script signing on ${this.domain}`),
                        k.keyManager,
                        i
                    );
                } else if (this.trustedUsers.includes(k.keybaseUser) && k.key.key.can_perform(ops)) {
                    console.debug(`Allowing script from ${this.domain} to run as it was signed by ${k.keybaseUser}`);
                    return k.keyManager.fetch(ids, ops, cb);
                }
            }
        }

        return cb(new Error(`Key not found: ${JSON.stringify(hexKeyIds)}`));
    }

    /**
     * Searches Keybase for 'owners' of this domain, asks the user if they'd like
     * to trust them to run scripts if they haven't seen this 'owner' before and then
     * stores them all keyed by pgp key id for easy lookup in `fetch`.
     */
    private async populateKeysForDomain(): Promise<void> {
        if (Object.entries(this.allKeyIdsForDomainFromKeybase).length) {
            return;
        }

        await this.loadUsersFromStorage();

        const res = await fetch(`https://keybase.io/_/api/1.0/user/lookup.json?domain=${this.domain}`);
        const users = await res.json();

        for (const user of users.them) {
            const username = user.basics.username;

            if (!this.hasPreviouslySeenKeybaseUser(username)) {
                this.pendingApproval.push(username);
            }

            // @ts-ignore
            const keyManagers = (await Promise.all(
                user.public_keys.pgp_public_keys.map((armored: any) => importFromArmoredPgp({ armored }))
            )) as KeyManager[];

            for (const km of keyManagers) {
                const keys = km.export_pgp_keys_to_keyring();

                for (const key of keys) {
                    const kid = key.key_material.get_key_id().toString("hex");

                    this.allKeyIdsForDomainFromKeybase[kid] = {
                        key,
                        keyManager: km,
                        keybaseUser: username
                    };
                }
            }
        }

        await this.storeUsersInStorage();
    }

    /**
     * Load previously "seen" Keybase users from local storage.
     */
    private async loadUsersFromStorage() {
        const trustedUsersStorageKey = `kpj_trusted_signers_${this.domain}`;
        const barredUsersStorageKey = `kpj_barred_signers_${this.domain}`;

        const stored = await browser.storage.local.get({
            [trustedUsersStorageKey]: "[]",
            [barredUsersStorageKey]: "[]"
        });

        for (const user of JSON.parse(stored[trustedUsersStorageKey])) {
            if (!this.trustedUsers.includes(user)) {
                this.trustedUsers.push(user);
            }
        }

        for (const user of JSON.parse(stored[barredUsersStorageKey])) {
            if (!this.barredUsers.includes(user)) {
                this.barredUsers.push(user);
            }
        }
    }

    /**
     * Store "seen" Keybase users in storage so we don't have to keep asking the user
     * if they'd like to run scripts from them for this domain.
     */
    private async storeUsersInStorage() {
        const trustedUsersStorageKey = `kpj_trusted_signers_${this.domain}`;
        const barredUsersStorageKey = `kpj_barred_signers_${this.domain}`;

        await browser.storage.local.set({
            [trustedUsersStorageKey]: JSON.stringify(this.trustedUsers),
            [barredUsersStorageKey]: JSON.stringify(this.barredUsers)
        });
    }

    /**
     * Checks if the user has previously trusted/barred scripts from the given
     * Keybase user for this domain before.
     *
     * @param user keybase user to check
     */
    private hasPreviouslySeenKeybaseUser(user: string): boolean {
        return [...this.trustedUsers, ...this.barredUsers].includes(user);
    }
}