Skip to content

Commit cf7a423

Browse files
committed
fix the bad url detection #634
1 parent 66953a3 commit cf7a423

File tree

2 files changed

+58
-26
lines changed

2 files changed

+58
-26
lines changed

build/src/api/core/orgcheck-api-codescanner.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const REGEX_XML_COMMENTS_AND_NEWLINES = new RegExp('(<!--[\\s\\S]*?-->|\\n)', 'g
99
const REGEX_HARDCODEDURLS = new RegExp("([A-Za-z0-9-]{1,63}\\.)+[A-Za-z]{2,6}", 'ig');
1010
const REGEX_HARDCODEDIDS = new RegExp("[,\"'\\s][a-zA-Z0-9]{5}0[a-zA-Z0-9]{9}([a-zA-Z0-9]{3})?[,\"'\\s]", 'ig');
1111
const SALESFORCE_DOMAINS = [ 'salesforce.com', '.force.' ];
12-
const SALESFORCE_MY_DOMAIN = '.my.salesforce.com';
12+
const SALESFORCE_WITH_INSTANCE = new RegExp("(^|[^A-Za-z])[A-Za-z]{1,4}[0-9]{1,4}S{0,1}\.", 'ig');
1313

1414
/**
1515
* @description Code Scanner class
@@ -72,7 +72,7 @@ export class CodeScanner {
7272
?.filter((domain) => SALESFORCE_DOMAINS.findIndex((sfdomain) => domain.indexOf(sfdomain) >= 0) >= 0) // filter only the salesforce domains
7373
.sort() // sorting the domains (if any)
7474
.filter((e, i, s) => i === s.indexOf(e)) // unique domains
75-
.filter((domain) => domain.indexOf(SALESFORCE_MY_DOMAIN) < 0); // remove the my.salesforce.com domains
75+
.filter((domain) => domain?.match(SALESFORCE_WITH_INSTANCE) !== null || false); // finally only the url with instances
7676
}
7777

7878
/**

build/test/api/unit/orgcheck-api-codescanner.unit.test.js

Lines changed: 56 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ describe('tests.api.unit.CodeScanner', () => {
3434
});
3535

3636
describe('Test the feature "IsInterface"', () => {
37-
3837
it('checks if the source code is an interface', () => {
3938
expect(CodeScanner.IsInterfaceFromApexCode('public interface MyInterface {')).toBe(true);
4039
expect(CodeScanner.IsInterfaceFromApexCode('global interface MyInterface {')).toBe(true);
@@ -48,7 +47,6 @@ describe('tests.api.unit.CodeScanner', () => {
4847
});
4948

5049
describe('Test the feature "IsEnum"', () => {
51-
5250
it('checks if the source code is an enum', () => {
5351
expect(CodeScanner.IsEnumFromApexCode('public enum MyEnum {')).toBe(true);
5452
expect(CodeScanner.IsEnumFromApexCode('global enum MyEnum {')).toBe(true);
@@ -73,32 +71,68 @@ describe('tests.api.unit.CodeScanner', () => {
7371
expect(hardCodedUrls.length).toBe(0);
7472
});
7573

76-
it('checks if the hard coded url is detected in a code that contains only one sfdc url', () => {
77-
const hardCodedUrls = CodeScanner.FindHardCodedURLs('String url = "https://www.salesforce.com";');
78-
expect(hardCodedUrls).toBeDefined();
79-
expect(hardCodedUrls.length).toBe(1);
80-
expect(hardCodedUrls[0]).toBe('www.salesforce.com');
74+
it('checks if generic salesforce urls are not detected and tagged as BAD', () => {
75+
[
76+
'lightning.force.com',
77+
'file.force.com',
78+
'www.salesforce.com',
79+
'salesforce.com',
80+
'test.salesforce.com',
81+
'login.salesforce.com',
82+
'visual.force.com',
83+
].forEach((domain) => {
84+
const hardCodedUrls = CodeScanner.FindHardCodedURLs(`String url = "https://${domain}";`);
85+
expect(hardCodedUrls).toBeDefined();
86+
if (hardCodedUrls.length > 0) {
87+
console.error(`Found: domain: ${domain}, length: ${hardCodedUrls.length} and first url: ${hardCodedUrls[0]}`);
88+
}
89+
expect(hardCodedUrls.length).toBe(0);
90+
});
8191
});
8292

83-
it('checks if the hard coded url is detected in a code that contains only one sfdc url with instance', () => {
84-
const hardCodedUrls = CodeScanner.FindHardCodedURLs('String url = "https://na1.salesforce.com";');
85-
expect(hardCodedUrls).toBeDefined();
86-
expect(hardCodedUrls.length).toBe(1);
87-
expect(hardCodedUrls[0]).toBe('na1.salesforce.com');
93+
it('checks if "my" salesforce urls are not detected and tagged as BAD', () => {
94+
[
95+
'xyz.file.force.com',
96+
'xyz.lightning.force.com',
97+
'xyz.my.salesforce.com',
98+
'xyz--abc.sandbox.my.salesforce.com',
99+
'orgcheck.my.salesforce.com'
100+
].forEach((domain) => {
101+
const hardCodedUrls = CodeScanner.FindHardCodedURLs(`String url = "https://${domain}";`);
102+
expect(hardCodedUrls).toBeDefined();
103+
if (hardCodedUrls.length > 0) {
104+
console.error(`Found: domain: ${domain}, length: ${hardCodedUrls.length} and first url: ${hardCodedUrls[0]}`);
105+
}
106+
expect(hardCodedUrls.length).toBe(0);
107+
});
88108
});
89109

90-
it('checks if the hard coded url is NOT detected in a code that contains only one sfdc my-domain url', () => {
91-
const hardCodedUrls = CodeScanner.FindHardCodedURLs('String url = "https://orgcheck.my.salesforce.com";');
92-
expect(hardCodedUrls).toBeDefined();
93-
expect(hardCodedUrls.length).toBe(0);
110+
it('checks if real hard coded salesforce url are detected and tagged as BAD', () => {
111+
[
112+
'na1.salesforce.com',
113+
'na2.salesforce.com',
114+
'na3.salesforce.com',
115+
'eu1.salesforce.com',
116+
'eu2.salesforce.com',
117+
'eu3.salesforce.com',
118+
'xyz--c.na1.content.force.com',
119+
'xyz--c.na2.content.force.com',
120+
'xyz--c.eu3.content.force.com',
121+
'xyz--c.na89.content.force.com'
122+
].forEach((domain) => {
123+
const hardCodedUrls = CodeScanner.FindHardCodedURLs(`String url = "https://${domain}";`);
124+
expect(hardCodedUrls).toBeDefined();
125+
expect(hardCodedUrls.length).toBe(1);
126+
expect(hardCodedUrls[0]).toBe(domain);
127+
});
94128
});
95129

96-
it('checks if the hard coded url is detected in a code that contains two sfdc urls', () => {
97-
const hardCodedUrls = CodeScanner.FindHardCodedURLs('String url1 = "https://www.salesforce.com"; String url2 = "https://na1.salesforce.com";');
130+
it('checks if the hard coded url is detected in a code that contains two bad sfdc urls', () => {
131+
const hardCodedUrls = CodeScanner.FindHardCodedURLs('String url1 = "https://xyz--c.na89.content.force.com"; String url2 = "https://na1.salesforce.com";');
98132
expect(hardCodedUrls).toBeDefined();
99133
expect(hardCodedUrls.length).toBe(2);
100134
expect(hardCodedUrls[0]).toBe('na1.salesforce.com'); // array is alphabetically sorted!!
101-
expect(hardCodedUrls[1]).toBe('www.salesforce.com');
135+
expect(hardCodedUrls[1]).toBe('xyz--c.na89.content.force.com');
102136
});
103137

104138
it('checks if the hard coded url is detected in a code that contains two sfdc urls the first one being a my domain', () => {
@@ -109,18 +143,16 @@ describe('tests.api.unit.CodeScanner', () => {
109143
});
110144

111145
it('checks if the hard coded url is detected in a code that contains two sfdc urls the second one being a my domain', () => {
112-
const hardCodedUrls = CodeScanner.FindHardCodedURLs('String url1 = "https://www.salesforce.com"; String url2 = "https://orgcheck.my.salesforce.com";');
146+
const hardCodedUrls = CodeScanner.FindHardCodedURLs('String url1 = "https://na1.salesforce.com"; String url2 = "https://orgcheck.my.salesforce.com";');
113147
expect(hardCodedUrls).toBeDefined();
114148
expect(hardCodedUrls.length).toBe(1);
115-
expect(hardCodedUrls[0]).toBe('www.salesforce.com');
149+
expect(hardCodedUrls[0]).toBe('na1.salesforce.com');
116150
});
117151

118152
it('checks if the hard coded url is detected in a code that contains multiple urls with one force domain and on salesforce domain', () => {
119153
const hardCodedUrls = CodeScanner.FindHardCodedURLs('String url1 = "https://abc.force.com"; String url2 = "https://www.salesforce.com";');
120154
expect(hardCodedUrls).toBeDefined();
121-
expect(hardCodedUrls.length).toBe(2);
122-
expect(hardCodedUrls[0]).toBe('abc.force.com');
123-
expect(hardCodedUrls[1]).toBe('www.salesforce.com');
155+
expect(hardCodedUrls.length).toBe(0);
124156
});
125157
});
126158

0 commit comments

Comments
 (0)