summaryrefslogtreecommitdiffstats
path: root/comm/mail/extensions/openpgp/content/modules/OpenPGPAlias.jsm
blob: f480b727f6bd3f94c1d8014ed311924ef2af63ba (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
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

const EXPORTED_SYMBOLS = ["OpenPGPAlias"];

var OpenPGPAlias = {
  _aliasDomains: null,
  _aliasEmails: null,

  _loaded() {
    return this._aliasDomains && this._aliasEmails;
  },

  async load() {
    let path = Services.prefs.getStringPref(
      "mail.openpgp.alias_rules_file",
      ""
    );

    if (!path) {
      this._clear();
      return;
    }

    await this._loadFromFile(path);
  },

  _clear() {
    this._aliasDomains = new Map();
    this._aliasEmails = new Map();
  },

  _hasExpectedKeysStructure(keys) {
    try {
      for (let entry of keys) {
        if (!("id" in entry) && !("fingerprint" in entry)) {
          return false;
        }
      }
      // all entries passed the test
      return true;
    } catch (ex) {
      return false;
    }
  },

  async _loadFromFile(src) {
    this._clear();

    let aliasRules;
    let jsonData;
    if (src.startsWith("file://")) {
      let response = await fetch(src);
      jsonData = await response.json();
    } else if (src.includes("/") || src.includes("\\")) {
      throw new Error(`Invalid alias rules src: ${src}`);
    } else {
      let spec = PathUtils.join(
        Services.dirsvc.get("ProfD", Ci.nsIFile).path,
        src
      );
      let response = await fetch(PathUtils.toFileURI(spec));
      jsonData = await response.json();
    }
    if (!("rules" in jsonData)) {
      throw new Error(
        "alias file contains invalid JSON data, no rules element found"
      );
    }
    aliasRules = jsonData.rules;

    for (let entry of aliasRules) {
      if (!("keys" in entry) || !entry.keys || !entry.keys.length) {
        console.log("Ignoring invalid alias rule without keys");
        continue;
      }
      if ("email" in entry && "domain" in entry) {
        console.log("Ignoring invalid alias rule with both email and domain");
        continue;
      }
      // Ignore duplicate rules, only use first rule per key.
      // Require email address contains @, and domain doesn't contain @.
      if ("email" in entry) {
        let email = entry.email.toLowerCase();
        if (!email.includes("@")) {
          console.log("Ignoring invalid email alias rule: " + email);
          continue;
        }
        if (this._aliasEmails.get(email)) {
          console.log("Ignoring duplicate email alias rule: " + email);
          continue;
        }
        if (!this._hasExpectedKeysStructure(entry.keys)) {
          console.log(
            "Ignoring alias rule with invalid key entries for email " + email
          );
          continue;
        }
        this._aliasEmails.set(email, entry.keys);
      } else if ("domain" in entry) {
        let domain = entry.domain.toLowerCase();
        if (domain.includes("@")) {
          console.log("Ignoring invalid domain alias rule: " + domain);
          continue;
        }
        if (this._aliasDomains.get(domain)) {
          console.log("Ignoring duplicate domain alias rule: " + domain);
          continue;
        }
        if (!this._hasExpectedKeysStructure(entry.keys)) {
          console.log(
            "Ignoring alias rule with invalid key entries for domain " + domain
          );
          continue;
        }
        this._aliasDomains.set(domain, entry.keys);
      } else {
        console.log(
          "Ignoring invalid alias rule without domain and without email"
        );
      }
    }
  },

  getDomainAliasKeyList(email) {
    if (!this._loaded()) {
      return null;
    }

    let lastAt = email.lastIndexOf("@");
    if (lastAt == -1) {
      return null;
    }

    let domain = email.substr(lastAt + 1);
    if (!domain) {
      return null;
    }

    return this._aliasDomains.get(domain.toLowerCase());
  },

  getEmailAliasKeyList(email) {
    if (!this._loaded()) {
      return null;
    }
    return this._aliasEmails.get(email.toLowerCase());
  },

  hasAliasDefinition(email) {
    if (!this._loaded()) {
      return false;
    }
    email = email.toLowerCase();
    let hasEmail = this._aliasEmails.has(email);
    if (hasEmail) {
      return true;
    }

    let lastAt = email.lastIndexOf("@");
    if (lastAt == -1) {
      return false;
    }

    let domain = email.substr(lastAt + 1);
    if (!domain) {
      return false;
    }

    return this._aliasDomains.has(domain);
  },
};