-
Notifications
You must be signed in to change notification settings - Fork 25
/
plugin.js
234 lines (215 loc) · 7.83 KB
/
plugin.js
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
/**
* KeeWeb plugin: haveibeenpwned
* @author Olivier LEVILLAIN
* @license MIT
*/
const Logger = require('util/logger');
// change log level here.
const LogLevel = Logger.Level.Info;
const DetailsView = require('views/details/details-view');
const InputFx = require('util/input-fx');
const Kdbxweb = require('kdbxweb');
const _ = require('_');
const Tip = require('util/tip');
const detailsViewFieldChanged = DetailsView.prototype.fieldChanged;
let _seen = [];
class HIBPUtils {
constructor() {
_seen = [];
this.checkPwnedPwd = true;
this.checkPwnedName = true;
this.blockPwnedPwd = false;
this.blockPwnedName = false;
this.logger = new Logger('HaveIBeenPwned');
this.logger.setLevel(LogLevel);
};
replacer(key, value) {
if (value != null && typeof value === 'object') {
if (_seen.indexOf(value) >= 0) {
return;
}
_seen.push(value);
}
return value;
};
stringify(obj) {
const ret = JSON.stringify(obj, this.replacer);
_seen = [];
return ret;
};
xhrcall (config) {
const xhr = new XMLHttpRequest();
if (config.responseType) {
xhr.responseType = config.responseType;
}
const statuses = config.statuses || [200];
xhr.addEventListener('load', () => {
if (statuses.indexOf(xhr.status) >= 0) {
return config.success && config.success(xhr.response, xhr);
} else {
return config.error && config.error('http status ' + xhr.status, xhr);
}
});
xhr.addEventListener('error', () => {
const err = xhr.response && xhr.response.error || new Error('Network error');
this.logger.error('HaveIBeenPwned API error', 'GET', xhr.status, err);
err.status = xhr.status;
return err;
});
xhr.addEventListener('timeout', () => {
return config.error && config.error('timeout', xhr);
});
xhr.open(config.method || 'GET', config.url);
if (config.headers) {
config.headers.forEach((value, key) => {
xhr.setRequestHeader(key, value);
});
};
xhr.send(config.data);
};
hex (buffer) {
const hexCodes = [];
const view = new DataView(buffer);
for (let i = 0; i < view.byteLength; i += 4) {
// Using getUint32 reduces the number of iterations needed (we process 4 bytes each time)
const value = view.getUint32(i);
// toString(16) will give the hex representation of the number without padding
const stringValue = value.toString(16);
// We use concatenation and slice for padding
const padding = '00000000';
const paddedValue = (padding + stringValue).slice(-padding.length);
hexCodes.push(paddedValue);
}
// Join all the hex strings into one
return hexCodes.join('');
};
digest(algo, str) {
const buffer = Kdbxweb.ByteUtils.stringToBytes(str);
const subtle = window.crypto.subtle || window.crypto.webkitSubtle;
const _self = this;
return subtle.digest(algo, buffer).then(hash => {
return _self.hex(hash);
});
};
sha1(str) {
return this.digest('SHA-1', str);
};
sha256(str) {
return this.digest('SHA-256', str);
};
alert (el, msg) {
// Alerts.info({ body: msg, title: 'HaveIBeenPwned' });
el.focus();
el.addClass('input--error');
el.addClass('hibp-pwned');
Tip.createTip(el, { title: msg, placement: 'bottom' });
InputFx.shake(el);
};
passed(el, msg) {
hibp.logger.info(msg);
el.removeClass('input--error');
el.removeClass('hibp-pwned');
}
}
const hibp = new HIBPUtils();
DetailsView.prototype.checkNamePwned = function (name) {
hibp.logger.info('check hibp name ' + name);
name = encodeURIComponent(name);
const url = `https://haveibeenpwned.com/api/v2/breachedaccount/${name}?truncateResponse=true`;
hibp.logger.debug('url ' + url);
hibp.xhrcall({
url: url,
method: 'GET',
responseType: 'json',
headers: undefined,
data: null,
statuses: [200, 404],
success: (data, xhr) => {
if (data && data.length > 0) {
hibp.logger.debug('found breaches ' + JSON.stringify(data));
let breaches = '';
data.forEach(breach => { breaches += '<li>' + _.escape(breach.Name) + '</li>\n'; });
hibp.alert(this.userEditView.$el, `WARNING! This account has been pawned in the following breaches<br/>\n<ul>\n${breaches}\n</ul>\n<p>Please check on <a href='https://haveibeenpwned.com'>https://haveibeenpwned.com</a>\n`);
} else {
hibp.passed(this.userEditView.$el, 'check pwned user name passed...');
}
}
});
};
DetailsView.prototype.checkPwdPwned = function (passwordHash) {
hibp.logger.info('check hibp pwd (hash) ' + passwordHash);
const prefix = passwordHash.substring(0, 5);
hibp.xhrcall({
url: `https://api.pwnedpasswords.com/range/${prefix}`,
method: 'GET',
responseType: 'text',
headers: undefined,
data: null,
statuses: [200, 404],
success: data => {
if (data) {
hibp.logger.debug('found breaches ' + JSON.stringify(data));
data.split('\r\n').forEach(line => {
const h = line.split(':');
const suffix = h[0];
if (prefix + suffix === passwordHash) {
const nb = _.escape(h[1]);
hibp.alert(this.passEditView.$el, `WARNING: This password is referenced as pawned ${nb} times on <a href='https://haveibeenpwned.com'>https://haveibeenpwned.com</a>!\n`);
}
});
} else {
hibp.passed(this.userEditView.$el, 'check pwned password passed...');
}
}
});
};
DetailsView.prototype.fieldChanged = function (e) {
detailsViewFieldChanged.apply(this, arguments);
if (e.field) {
hibp.logger.debug('field changed ' + hibp.stringify(e));
if (e.field === '$Password' && hibp.checkPwnedPwd) {
if (this.passEditView.value) {
const pwd = this.passEditView.value.getText();
if (pwd.replace(/\s/, '') !== '' && !pwd.startsWith('{REF:')) {
hibp.sha1(pwd).then(hash => {
this.checkPwdPwned(hash.toUpperCase());
});
}
}
} else if (e.field === '$UserName' && hibp.checkPwnedName) {
this.checkNamePwned(e.val);
}
}
};
module.exports.getSettings = function () {
return [{
name: 'checkPwnedPwd',
label: 'Check passwords against HaveIBeenPwned list',
type: 'checkbox',
value: hibp.checkPwnedPwd
}, {
name: 'checkPwnedName',
label: 'Check user ids against HaveIBeenPwned list',
type: 'checkbox',
value: hibp.checkPwnedName
}, {
name: 'blockPwnedPwd',
label: 'Block pwned passwords if they are in HaveIBeenPwned list',
type: 'checkbox',
value: hibp.blockPwnedPwd
}, {
name: 'blockPwnedName',
label: 'Block pwned names if they are in HaveIBeenPwned list',
type: 'checkbox',
value: hibp.blockPwnedName
}];
};
module.exports.setSettings = function (changes) {
for (const field in changes) {
const ccfield = field.substr(0, 1).toLowerCase() + field.substring(1);
hibp[ccfield] = changes[field];
}
};
module.exports.uninstall = function () {
DetailsView.prototype.fieldChanged = detailsViewFieldChanged;
};