Skip to content

Commit 593335a

Browse files
Fix CG for typed-rest-client (#370)
* Fix CG - Rewrited react samples on vite because of react'scripts contains cve and not maintained * Fix CG - Npm audit fix for webpack samples * Fix CG - Fixed CVE in typed-rest client - Migrated package to use node14+ as mocha is not supported in versions below. - Migrated CI to use Node14+ for tests _ Bumped ts to v5 - Bumped typed-rest-client to new major version 2 - Added .vscode folder in .gitignore * Fix CG - Fixed tests for the new version of the typescript * Fix CG - Replaced crypto package(DES-ECB and MD4 algorythms calls) to a packages as they are become legacy in openssl3 which is used new node * Fix CG - Regenerated package.lock files in samples. * Fix CG - Fixed types in tests - Fixed webpack samples * Fix CG - Bumped supported version * Fix CG - Updated README with new information * Fix CG - Updated README with new information
1 parent 4c0c060 commit 593335a

38 files changed

+6190
-29632
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
typings
22
node_modules
33
_build
4+
.vscode
45
lib/*.js
56
samples/*.js
67
samples/*.txt

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,8 @@ set NODE_DEBUG=http
7373

7474
## Node support
7575

76-
The typed-rest-client is built using the latest LTS version of Node 8. We also support the latest LTS for Node 6 and newer.
76+
v2 - [current, maintained] - Supports node 16 and above
77+
v1 - End Of Life, for Node < 16, contains security vulnerabilities, use at your own risk
7778

7879
## Contributing
7980

azure-pipelines.yml

+3-3
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ variables:
1010
parameters:
1111
- name: nodeVersionList
1212
type: object
13-
default: [6, 8, 10, 12, 14, 16]
13+
default: [16, 18, 20]
1414
- name: imageList
1515
type: object
1616
default:
@@ -58,8 +58,8 @@ extends:
5858
steps:
5959
- task: NodeTool@0
6060
inputs:
61-
versionSpec: '8.x'
62-
displayName: Install node 8
61+
versionSpec: '20.x'
62+
displayName: Install node 20.x
6363
- script: npm install
6464
displayName: npm install
6565
- script: npm run build

lib/Util.ts

+7-4
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ function buildParamsStringifyOptions(queryParams: IRequestQueryParams): any {
8686
* @param {string} charset? - optional; defaults to 'utf-8'
8787
* @return {Promise<string>}
8888
*/
89-
export async function decompressGzippedContent(buffer: Buffer, charset?: string): Promise<string> {
89+
export async function decompressGzippedContent(buffer: Buffer, charset?: BufferEncoding): Promise<string> {
9090
return new Promise<string>(async (resolve, reject) => {
9191
zlib.gunzip(buffer, function (error, buffer) {
9292
if (error) {
@@ -128,16 +128,19 @@ export function buildProxyBypassRegexFromEnv(bypass : string) : RegExp {
128128
* @param {IHttpClientResponse} response
129129
* @return {string} - Content Encoding Charset; Default=utf-8
130130
*/
131-
export function obtainContentCharset (response: IHttpClientResponse) : string {
131+
export function obtainContentCharset (response: IHttpClientResponse) : BufferEncoding {
132132
// Find the charset, if specified.
133133
// Search for the `charset=CHARSET` string, not including `;,\r\n`
134134
// Example: content-type: 'application/json;charset=utf-8'
135135
// |__ matches would be ['charset=utf-8', 'utf-8', index: 18, input: 'application/json; charset=utf-8']
136136
// |_____ matches[1] would have the charset :tada: , in our example it's utf-8
137137
// However, if the matches Array was empty or no charset found, 'utf-8' would be returned by default.
138-
const nodeSupportedEncodings = ['ascii', 'utf8', 'utf16le', 'ucs2', 'base64', 'binary', 'hex'];
138+
const nodeSupportedEncodings: BufferEncoding[] = ['ascii', 'utf8', 'utf16le', 'ucs2', 'base64', 'binary', 'hex'];
139139
const contentType: string = response.message.headers['content-type'] || '';
140140
const matches: (RegExpMatchArray|null) = contentType.match(/charset=([^;,\r\n]+)/i);
141+
if (matches && matches[1] && nodeSupportedEncodings.indexOf(matches[1] as BufferEncoding) != -1) {
142+
return matches[1] as BufferEncoding;
143+
}
141144

142-
return (matches && matches[1] && nodeSupportedEncodings.indexOf(matches[1]) != -1) ? matches[1] : 'utf-8';
145+
return 'utf-8';
143146
}

lib/handlers/ntlm.ts

+1
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ export class NtlmCredentialHandler implements ifm.IRequestHandler {
6060
const callbackForResult = function (err: any, res: ifm.IHttpClientResponse) {
6161
if (err) {
6262
reject(err);
63+
return;
6364
}
6465
// We have to readbody on the response before continuing otherwise there is a hang.
6566
res.readBody().then(() => {

lib/opensource/Node-SMB/lib/common.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ function oddpar(buf)
3030
*/
3131
function expandkey(key56)
3232
{
33-
var key64 = new Buffer(8);
33+
var key64 = Buffer.alloc(8);
3434

3535
key64[0] = key56[0] & 0xFE;
3636
key64[1] = ((key56[0] << 7) & 0xFF) | (key56[1] >> 1);
@@ -49,7 +49,7 @@ function expandkey(key56)
4949
*/
5050
function bintohex(bin)
5151
{
52-
var buf = (Buffer.isBuffer(buf) ? buf : new Buffer(bin, 'binary'));
52+
var buf = (Buffer.isBuffer(buf) ? buf : Buffer.from(bin, 'binary'));
5353
var str = buf.toString('hex').toUpperCase();
5454
return zeroextend(str, 32);
5555
}

lib/opensource/Node-SMB/lib/ntlm.js

+15-9
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ var $ = require('./common');
44
var lmhashbuf = require('./smbhash').lmhashbuf;
55
var nthashbuf = require('./smbhash').nthashbuf;
66

7+
var desjs = require("des.js");
78

89
function encodeType1(hostname, ntdomain) {
910
hostname = hostname.toUpperCase();
@@ -12,7 +13,7 @@ function encodeType1(hostname, ntdomain) {
1213
var ntdomainlen = Buffer.byteLength(ntdomain, 'ascii');
1314

1415
var pos = 0;
15-
var buf = new Buffer(32 + hostnamelen + ntdomainlen);
16+
var buf = Buffer.alloc(32 + hostnamelen + ntdomainlen);
1617

1718
buf.write('NTLMSSP', pos, 7, 'ascii'); // byte protocol[8];
1819
pos += 7;
@@ -86,10 +87,10 @@ function encodeType3(username, hostname, ntdomain, nonce, password) {
8687
hostname = hostname.toUpperCase();
8788
ntdomain = ntdomain.toUpperCase();
8889

89-
var lmh = new Buffer(21);
90+
var lmh = Buffer.alloc(21);
9091
lmhashbuf(password).copy(lmh);
9192
lmh.fill(0x00, 16); // null pad to 21 bytes
92-
var nth = new Buffer(21);
93+
var nth = Buffer.alloc(21);
9394
nthashbuf(password).copy(nth);
9495
nth.fill(0x00, 16); // null pad to 21 bytes
9596

@@ -110,7 +111,7 @@ function encodeType3(username, hostname, ntdomain, nonce, password) {
110111

111112
var pos = 0;
112113
var msg_len = 64 + ntdomainlen + usernamelen + hostnamelen + lmrlen + ntrlen;
113-
var buf = new Buffer(msg_len);
114+
var buf = Buffer.alloc(msg_len);
114115

115116
buf.write('NTLMSSP', pos, 7, 'ascii'); // byte protocol[8];
116117
pos += 7;
@@ -189,12 +190,17 @@ function encodeType3(username, hostname, ntdomain, nonce, password) {
189190

190191
function makeResponse(hash, nonce)
191192
{
192-
var out = new Buffer(24);
193+
var out = Buffer.alloc(24);
194+
193195
for (var i = 0; i < 3; i++) {
194196
var keybuf = $.oddpar($.expandkey(hash.slice(i * 7, i * 7 + 7)));
195-
var des = crypto.createCipheriv('DES-ECB', keybuf, '');
196-
var str = des.update(nonce.toString('binary'), 'binary', 'binary');
197-
out.write(str, i * 8, i * 8 + 8, 'binary');
197+
198+
var des = desjs.DES.create({type: 'encrypt', key: keybuf});
199+
var magicKey = Buffer.from(nonce.toString('binary'));
200+
var insertBuff = Buffer.from(des.update(magicKey));
201+
202+
out.fill(insertBuff, i * 8, i * 8 + 8, 'binary');
203+
198204
}
199205
return out;
200206
}
@@ -210,7 +216,7 @@ exports.challengeHeader = function (hostname, domain) {
210216
};
211217

212218
exports.responseHeader = function (res, url, domain, username, password) {
213-
var serverNonce = new Buffer((res.headers['www-authenticate'].match(/^NTLM\s+(.+?)(,|\s+|$)/) || [])[1], 'base64');
219+
var serverNonce = Buffer.from((res.headers['www-authenticate'].match(/^NTLM\s+(.+?)(,|\s+|$)/) || [])[1], 'base64');
214220
var hostname = require('url').parse(url).hostname;
215221
return 'NTLM ' + exports.encodeType3(username, hostname, domain, exports.decodeType2(serverNonce), password).toString('base64')
216222
};

lib/opensource/Node-SMB/lib/smbhash.js

+12-9
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
var crypto = require('crypto');
21
var $ = require('./common');
32

3+
var jsmd4 = require("js-md4");
4+
var desjs = require("des.js");
5+
46
/*
57
* Generate the LM Hash
68
*/
@@ -11,7 +13,7 @@ function lmhashbuf(inputstr)
1113
var xl = Buffer.byteLength(x, 'ascii');
1214

1315
/* null pad to 14 bytes */
14-
var y = new Buffer(14);
16+
var y = Buffer.alloc(14);
1517
y.write(x, 0, xl, 'ascii');
1618
y.fill(0, xl);
1719

@@ -24,12 +26,13 @@ function lmhashbuf(inputstr)
2426
/* DES encrypt magic number "KGS!@#$%" to two
2527
* 8-byte ciphertexts, (ECB, no padding)
2628
*/
27-
var buf = new Buffer(16);
29+
var buf = Buffer.alloc(16);
2830
var pos = 0;
2931
var cts = halves.forEach(function(z) {
30-
var des = crypto.createCipheriv('DES-ECB', z, '');
31-
var str = des.update('KGS!@#$%', 'binary', 'binary');
32-
buf.write(str, pos, pos + 8, 'binary');
32+
var des = desjs.DES.create({type: 'encrypt', key: z});
33+
var magicKey = Buffer.from('KGS!@#$%', 'ascii');
34+
var insertBuff = Buffer.from(des.update(magicKey));
35+
buf.fill(insertBuff, pos, pos + 8, 'binary');
3336
pos += 8;
3437
});
3538

@@ -41,10 +44,10 @@ function lmhashbuf(inputstr)
4144
function nthashbuf(str)
4245
{
4346
/* take MD4 hash of UCS-2 encoded password */
44-
var ucs2 = new Buffer(str, 'ucs2');
45-
var md4 = crypto.createHash('md4');
47+
var ucs2 = Buffer.from(str, 'ucs2');
48+
var md4 = jsmd4.create();
4649
md4.update(ucs2);
47-
return new Buffer(md4.digest('binary'), 'binary');
50+
return Buffer.from(md4.digest('binary'), 'binary');
4851
}
4952

5053
function lmhash(is)

0 commit comments

Comments
 (0)