Skip to content

Commit f2fe8d9

Browse files
committed
delete useless code & verify servername
1 parent 590cdef commit f2fe8d9

File tree

3 files changed

+5
-119
lines changed

3 files changed

+5
-119
lines changed

lib/stream.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,9 @@ module.exports.secureStream = function secureStream(connection) {
3232
return
3333
}
3434
try {
35-
connection.stream.startTls({});
35+
// Configuration of TLS will not work in Cloudflare Workers because startTls doesn't have the corresponding options.
36+
// See https://developers.cloudflare.com/workers/runtime-apis/tcp-sockets/#opportunistic-tls-starttls
37+
connection.stream.startTls({expectedServerHostname: connection.config.host});
3638
}catch (err) {
3739
// SSL negotiation error are fatal
3840
err.code = 'HANDSHAKE_SSL_ERROR';

lib/utils/nodecrypto.js

Lines changed: 1 addition & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,6 @@
44

55
const nodeCrypto = require('crypto')
66

7-
function md5(string) {
8-
return nodeCrypto.createHash('md5').update(string, 'utf-8').digest('hex')
9-
}
10-
11-
// See AuthenticationMD5Password at https://www.postgresql.org/docs/current/static/protocol-flow.html
12-
function postgresMd5PasswordHash(user, password, salt) {
13-
const inner = md5(password + user);
14-
const outer = md5(Buffer.concat([Buffer.from(inner), salt]));
15-
return `md5${outer}`
16-
}
17-
18-
function sha256(text) {
19-
return nodeCrypto.createHash('sha256').update(text).digest()
20-
}
21-
227
async function sha1(msg,msg1,msg2) {
238
const hash = nodeCrypto.createHash('sha1');
249
hash.update(msg);
@@ -31,39 +16,6 @@ async function sha1(msg,msg1,msg2) {
3116
return hash.digest();
3217
}
3318

34-
function xorRotating(a, seed) {
35-
const result = Buffer.allocUnsafe(a.length);
36-
const seedLen = seed.length;
37-
38-
for (let i = 0; i < a.length; i++) {
39-
result[i] = a[i] ^ seed[i % seedLen];
40-
}
41-
return result;
42-
}
43-
44-
function encrypt(password, scramble, key) {
45-
const stage1 = xorRotating(
46-
Buffer.from(`${password}\0`, 'utf8'),
47-
scramble
48-
);
49-
return nodeCrypto.publicEncrypt(key, stage1);
50-
}
51-
52-
function hmacSha256(key, msg) {
53-
return nodeCrypto.createHmac('sha256', key).update(msg).digest()
54-
}
55-
56-
function deriveKey(password, salt, iterations) {
57-
return nodeCrypto.pbkdf2Sync(password, salt, iterations, 32, 'sha256')
58-
}
59-
6019
module.exports = {
61-
postgresMd5PasswordHash,
62-
randomBytes: nodeCrypto.randomBytes,
63-
deriveKey,
64-
sha256,
65-
hmacSha256,
66-
md5,
67-
sha1,
68-
encrypt,
20+
sha1
6921
}

lib/utils/webcrypto.js

Lines changed: 1 addition & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -15,75 +15,13 @@ const webCrypto = nodeCrypto.webcrypto || globalThis.crypto
1515
const subtleCrypto = webCrypto.subtle
1616
const textEncoder = new TextEncoder()
1717

18-
/**
19-
*
20-
* @param {*} length
21-
* @returns
22-
*/
23-
function randomBytes(length) {
24-
return webCrypto.getRandomValues(Buffer.alloc(length))
25-
}
26-
27-
async function md5(string) {
28-
try {
29-
return nodeCrypto.createHash('md5').update(string, 'utf-8').digest('hex')
30-
} catch (e) {
31-
// `createHash()` failed so we are probably not in Node.js, use the WebCrypto API instead.
32-
// Note that the MD5 algorithm on WebCrypto is not available in Node.js.
33-
// This is why we cannot just use WebCrypto in all environments.
34-
const data = typeof string === 'string' ? textEncoder.encode(string) : string
35-
const hash = await subtleCrypto.digest('MD5', data)
36-
return Array.from(new Uint8Array(hash))
37-
.map(b => b.toString(16).padStart(2, '0'))
38-
.join('')
39-
}
40-
}
41-
42-
// See AuthenticationMD5Password at https://www.postgresql.org/docs/current/static/protocol-flow.html
43-
async function postgresMd5PasswordHash(user, password, salt) {
44-
const inner = await md5(password + user);
45-
const outer = await md5(Buffer.concat([Buffer.from(inner), salt]));
46-
return `md5${outer}`
47-
}
48-
49-
/**
50-
* Create a SHA-256 digest of the given data
51-
* @param {Buffer} data
52-
*/
53-
async function sha256(text) {
54-
return await subtleCrypto.digest('SHA-256', text)
55-
}
56-
57-
/**
58-
* Sign the message with the given key
59-
* @param {ArrayBuffer} keyBuffer
60-
* @param {string} msg
61-
*/
62-
async function hmacSha256(keyBuffer, msg) {
63-
const key = await subtleCrypto.importKey('raw', keyBuffer, { name: 'HMAC', hash: 'SHA-256' }, false, ['sign'])
64-
return await subtleCrypto.sign('HMAC', key, textEncoder.encode(msg))
65-
}
66-
67-
/**
68-
* Derive a key from the password and salt
69-
* @param {string} password
70-
* @param {Uint8Array} salt
71-
* @param {number} iterations
72-
*/
73-
async function deriveKey(password, salt, iterations) {
74-
const key = await subtleCrypto.importKey('raw', textEncoder.encode(password), 'PBKDF2', false, ['deriveBits'])
75-
const params = { name: 'PBKDF2', hash: 'SHA-256', salt: salt, iterations: iterations }
76-
return await subtleCrypto.deriveBits(params, key, 32 * 8, ['deriveBits'])
77-
}
78-
7918
function concatenateBuffers(buffer1, buffer2) {
8019
const combined = new Uint8Array(buffer1.length + buffer2.length);
8120
combined.set(new Uint8Array(buffer1), 0);
8221
combined.set(new Uint8Array(buffer2), buffer1.length);
8322
return combined;
8423
}
8524

86-
8725
async function sha1(msg,msg1,msg2) {
8826
let concatenatedData = typeof msg === 'string' ? textEncoder.encode(msg) : msg;
8927
if (msg1) {
@@ -97,11 +35,5 @@ async function sha1(msg,msg1,msg2) {
9735
}
9836

9937
module.exports = {
100-
postgresMd5PasswordHash,
101-
randomBytes,
102-
deriveKey,
103-
sha256,
104-
hmacSha256,
105-
md5,
106-
sha1,
38+
sha1
10739
}

0 commit comments

Comments
 (0)