Skip to content

Commit a5ffb95

Browse files
authored
refactor: remove deprecated url.parse() method (#7751)
1 parent a43638f commit a5ffb95

File tree

9 files changed

+74
-36
lines changed

9 files changed

+74
-36
lines changed

spec/AuthenticationAdapters.spec.js

+18
Original file line numberDiff line numberDiff line change
@@ -1707,6 +1707,24 @@ describe('Apple Game Center Auth adapter', () => {
17071707
expect(e.message).toBe('Apple Game Center - invalid publicKeyUrl: invalid.com');
17081708
}
17091709
});
1710+
1711+
it('validateAuthData invalid public key http url', async () => {
1712+
const authData = {
1713+
id: 'G:1965586982',
1714+
publicKeyUrl: 'http://static.gc.apple.com/public-key/gc-prod-4.cer',
1715+
timestamp: 1565257031287,
1716+
signature: '1234',
1717+
salt: 'DzqqrQ==',
1718+
bundleId: 'cloud.xtralife.gamecenterauth',
1719+
};
1720+
1721+
try {
1722+
await gcenter.validateAuthData(authData);
1723+
fail();
1724+
} catch (e) {
1725+
expect(e.message).toBe('Apple Game Center - invalid publicKeyUrl: http://static.gc.apple.com/public-key/gc-prod-4.cer');
1726+
}
1727+
});
17101728
});
17111729

17121730
describe('phant auth adapter', () => {

spec/batch.spec.js

+22
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,28 @@ describe('batch', () => {
111111
expect(internalURL).toEqual('/classes/Object');
112112
});
113113

114+
it('should return the proper url with bad url provided', () => {
115+
const originalURL = '/parse/batch';
116+
const internalURL = batch.makeBatchRoutingPathFunction(
117+
originalURL,
118+
'badurl.com',
119+
publicServerURL
120+
)('/parse/classes/Object');
121+
122+
expect(internalURL).toEqual('/classes/Object');
123+
});
124+
125+
it('should return the proper url with bad public url provided', () => {
126+
const originalURL = '/parse/batch';
127+
const internalURL = batch.makeBatchRoutingPathFunction(
128+
originalURL,
129+
serverURLNaked,
130+
'badurl.com'
131+
)('/parse/classes/Object');
132+
133+
expect(internalURL).toEqual('/classes/Object');
134+
});
135+
114136
it('should handle a batch request without transaction', async () => {
115137
spyOn(databaseAdapter, 'createObject').and.callThrough();
116138

src/Adapters/Auth/gcenter.js

+11-8
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,23 @@ const authData = {
1414
const { Parse } = require('parse/node');
1515
const crypto = require('crypto');
1616
const https = require('https');
17-
const url = require('url');
1817

1918
const cache = {}; // (publicKey -> cert) cache
2019

2120
function verifyPublicKeyUrl(publicKeyUrl) {
22-
const parsedUrl = url.parse(publicKeyUrl);
23-
if (parsedUrl.protocol !== 'https:') {
21+
try {
22+
const parsedUrl = new URL(publicKeyUrl);
23+
if (parsedUrl.protocol !== 'https:') {
24+
return false;
25+
}
26+
const hostnameParts = parsedUrl.hostname.split('.');
27+
const length = hostnameParts.length;
28+
const domainParts = hostnameParts.slice(length - 2, length);
29+
const domain = domainParts.join('.');
30+
return domain === 'apple.com';
31+
} catch(error) {
2432
return false;
2533
}
26-
const hostnameParts = parsedUrl.hostname.split('.');
27-
const length = hostnameParts.length;
28-
const domainParts = hostnameParts.slice(length - 2, length);
29-
const domain = domainParts.join('.');
30-
return domain === 'apple.com';
3134
}
3235

3336
function convertX509CertToPEM(X509Cert) {

src/Adapters/Auth/oauth2.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@
5454
*/
5555

5656
const Parse = require('parse/node').Parse;
57-
const url = require('url');
5857
const querystring = require('querystring');
5958
const httpsRequest = require('./httpsRequest');
6059

@@ -112,7 +111,7 @@ function requestTokenInfo(options, access_token) {
112111
if (!options || !options.tokenIntrospectionEndpointUrl) {
113112
throw new Parse.Error(Parse.Error.OBJECT_NOT_FOUND, MISSING_URL);
114113
}
115-
const parsedUrl = url.parse(options.tokenIntrospectionEndpointUrl);
114+
const parsedUrl = new URL(options.tokenIntrospectionEndpointUrl);
116115
const postData = querystring.stringify({
117116
token: access_token,
118117
});

src/Adapters/Storage/Postgres/PostgresConfigParser.js

+4-6
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,16 @@
1-
const url = require('url');
21
const fs = require('fs');
32
function getDatabaseOptionsFromURI(uri) {
43
const databaseOptions = {};
54

6-
const parsedURI = url.parse(uri);
7-
const queryParams = parseQueryParams(parsedURI.query);
8-
const authParts = parsedURI.auth ? parsedURI.auth.split(':') : [];
5+
const parsedURI = new URL(uri);
6+
const queryParams = parseQueryParams(parsedURI.searchParams.toString());
97

108
databaseOptions.host = parsedURI.hostname || 'localhost';
119
databaseOptions.port = parsedURI.port ? parseInt(parsedURI.port) : 5432;
1210
databaseOptions.database = parsedURI.pathname ? parsedURI.pathname.substr(1) : undefined;
1311

14-
databaseOptions.user = authParts.length > 0 ? authParts[0] : '';
15-
databaseOptions.password = authParts.length > 1 ? authParts[1] : '';
12+
databaseOptions.user = parsedURI.username;
13+
databaseOptions.password = parsedURI.password;
1614

1715
if (queryParams.ssl && queryParams.ssl.toLowerCase() === 'true') {
1816
databaseOptions.ssl = true;

src/Controllers/LoggerController.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { Parse } from 'parse/node';
22
import AdaptableController from './AdaptableController';
33
import { LoggerAdapter } from '../Adapters/Logger/LoggerAdapter';
4-
import url from 'url';
54

65
const MILLISECONDS_IN_A_DAY = 24 * 60 * 60 * 1000;
76
const LOG_STRING_TRUNCATE_LENGTH = 1000;
@@ -38,15 +37,16 @@ export class LoggerController extends AdaptableController {
3837
});
3938
}
4039

41-
maskSensitiveUrl(urlString) {
42-
const urlObj = url.parse(urlString, true);
43-
const query = urlObj.query;
40+
maskSensitiveUrl(path) {
41+
const urlString = 'http://localhost' + path; // prepend dummy string to make a real URL
42+
const urlObj = new URL(urlString);
43+
const query = urlObj.searchParams;
4444
let sanitizedQuery = '?';
4545

46-
for (const key in query) {
46+
for (const [key, value] of query) {
4747
if (key !== 'password') {
4848
// normal value
49-
sanitizedQuery += key + '=' + query[key] + '&';
49+
sanitizedQuery += key + '=' + value + '&';
5050
} else {
5151
// password value, redact it
5252
sanitizedQuery += key + '=' + '********' + '&';

src/Controllers/index.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import authDataManager from '../Adapters/Auth';
22
import { ParseServerOptions } from '../Options';
33
import { loadAdapter } from '../Adapters/AdapterLoader';
44
import defaults from '../defaults';
5-
import url from 'url';
65
// Controllers
76
import { LoggerController } from './LoggerController';
87
import { FilesController } from './FilesController';
@@ -220,7 +219,7 @@ export function getAuthDataManager(options: ParseServerOptions) {
220219
export function getDatabaseAdapter(databaseURI, collectionPrefix, databaseOptions) {
221220
let protocol;
222221
try {
223-
const parsedURI = url.parse(databaseURI);
222+
const parsedURI = new URL(databaseURI);
224223
protocol = parsedURI.protocol ? parsedURI.protocol.toLowerCase() : null;
225224
} catch (e) {
226225
/* */

src/ParseServerRESTController.js

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
const Config = require('./Config');
22
const Auth = require('./Auth');
33
const RESTController = require('parse/lib/node/RESTController');
4-
const URL = require('url');
54
const Parse = require('parse/node');
65

76
function getSessionToken(options) {
@@ -38,9 +37,9 @@ function ParseServerRESTController(applicationId, router) {
3837
if (!config) {
3938
config = Config.get(applicationId);
4039
}
41-
const serverURL = URL.parse(config.serverURL);
42-
if (path.indexOf(serverURL.path) === 0) {
43-
path = path.slice(serverURL.path.length, path.length);
40+
const serverURL = new URL(config.serverURL);
41+
if (path.indexOf(serverURL.pathname) === 0) {
42+
path = path.slice(serverURL.pathname.length, path.length);
4443
}
4544

4645
if (path[0] !== '/') {

src/batch.js

+8-8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
const Parse = require('parse/node').Parse;
2-
const url = require('url');
32
const path = require('path');
43
// These methods handle batch requests.
54
const batchPath = '/batch';
@@ -11,11 +10,12 @@ function mountOnto(router) {
1110
});
1211
}
1312

14-
function parseURL(URL) {
15-
if (typeof URL === 'string') {
16-
return url.parse(URL);
13+
function parseURL(urlString) {
14+
try {
15+
return new URL(urlString);
16+
} catch(error) {
17+
return undefined;
1718
}
18-
return undefined;
1919
}
2020

2121
function makeBatchRoutingPathFunction(originalUrl, serverURL, publicServerURL) {
@@ -33,9 +33,9 @@ function makeBatchRoutingPathFunction(originalUrl, serverURL, publicServerURL) {
3333
return path.posix.join('/', requestPath.slice(apiPrefix.length));
3434
};
3535

36-
if (serverURL && publicServerURL && serverURL.path != publicServerURL.path) {
37-
const localPath = serverURL.path;
38-
const publicPath = publicServerURL.path;
36+
if (serverURL && publicServerURL && serverURL.pathname != publicServerURL.pathname) {
37+
const localPath = serverURL.pathname;
38+
const publicPath = publicServerURL.pathname;
3939

4040
// Override the api prefix
4141
apiPrefix = localPath;

0 commit comments

Comments
 (0)