Skip to content

Commit d52392f

Browse files
committed
MOBILE-3458 core: Validate custom URLs and QR
1 parent 8df8c24 commit d52392f

File tree

3 files changed

+65
-0
lines changed

3 files changed

+65
-0
lines changed

src/lang/en.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@
102102
"errorsomedatanotdownloaded": "If you downloaded this activity, please notice that some data isn't downloaded during the download process for performance and data usage reasons.",
103103
"errorsync": "An error occurred while synchronising. Please try again.",
104104
"errorsyncblocked": "This {{$a}} cannot be synchronised right now because of an ongoing process. Please try again later. If the problem persists, try restarting the app.",
105+
"errorurlschemeinvalidsite": "This site URL cannot be opened in this app.",
105106
"explanationdigitalminor": "This information is required to determine if your age is over the digital age of consent. This is the age when an individual can consent to terms and conditions and their data being legally stored and processed.",
106107
"favourites": "Starred",
107108
"filename": "Filename",

src/providers/urlschemes.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import { CoreSitePluginsProvider } from '@core/siteplugins/providers/siteplugins
2929
import { CoreConfigConstants } from '../configconstants';
3030
import { CoreConstants } from '@core/constants';
3131
import { makeSingleton } from '@singletons/core.singletons';
32+
import { CoreUrl } from '@singletons/url';
3233

3334
/**
3435
* All params that can be in a custom URL scheme.
@@ -166,6 +167,12 @@ export class CoreCustomURLSchemesProvider {
166167
}
167168

168169
try {
170+
const isValid = await this.isInFixedSiteUrls(data.siteUrl);
171+
172+
if (!isValid) {
173+
throw this.translate.instant('core.errorurlschemeinvalidsite');
174+
}
175+
169176
if (data.redirect && data.redirect.match(/^https?:\/\//) && data.redirect.indexOf(data.siteUrl) == -1) {
170177
// Redirect URL must belong to the same site. Reject.
171178
throw this.translate.instant('core.contentlinks.errorredirectothersite');
@@ -540,6 +547,38 @@ export class CoreCustomURLSchemesProvider {
540547
this.domUtils.showErrorModalDefault(error.error, this.translate.instant('core.login.invalidsite'));
541548
}
542549
}
550+
551+
/**
552+
* Check if a site URL is one of the fixed sites for the app (in case there are fixed sites).
553+
*
554+
* @param siteUrl Site URL to check.
555+
* @return Promise resolved with boolean: whether is one of the fixed sites.
556+
*/
557+
protected async isInFixedSiteUrls(siteUrl: string): Promise<boolean> {
558+
if (this.loginHelper.isFixedUrlSet()) {
559+
560+
return CoreUrl.sameDomainAndPath(siteUrl, <string> this.loginHelper.getFixedSites());
561+
} else if (this.loginHelper.hasSeveralFixedSites()) {
562+
const sites = <any[]> this.loginHelper.getFixedSites();
563+
564+
const site = sites.find((site) => {
565+
return CoreUrl.sameDomainAndPath(siteUrl, site.url);
566+
});
567+
568+
return !!site;
569+
} else if (CoreConfigConstants.multisitesdisplay == 'sitefinder' && CoreConfigConstants.onlyallowlistedsites) {
570+
// Call the sites finder to validate the site.
571+
const result = await this.sitesProvider.findSites(siteUrl.replace(/^https?\:\/\/|\.\w{2,3}\/?$/g, ''));
572+
573+
const site = result && result.find((site) => {
574+
return CoreUrl.sameDomainAndPath(siteUrl, site.url);
575+
});
576+
577+
return !!site;
578+
}
579+
580+
return true;
581+
}
543582
}
544583

545584
/**

src/singletons/url.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15+
import { CoreTextUtils } from '@providers/utils/text';
16+
1517
/**
1618
* Parts contained within a url.
1719
*/
@@ -172,4 +174,27 @@ export class CoreUrl {
172174
static removeProtocol(url: string): string {
173175
return url.replace(/^[a-zA-Z]+:\/\//i, '');
174176
}
177+
178+
/**
179+
* Check if two URLs have the same domain and path.
180+
*
181+
* @param urlA First URL.
182+
* @param urlB Second URL.
183+
* @return Whether they have same domain and path.
184+
*/
185+
static sameDomainAndPath(urlA: string, urlB: string): boolean {
186+
// Add protocol if missing, the parse function requires it.
187+
if (!urlA.match(/^[^\/:\.\?]*:\/\//)) {
188+
urlA = `https://${urlA}`;
189+
}
190+
if (!urlB.match(/^[^\/:\.\?]*:\/\//)) {
191+
urlB = `https://${urlB}`;
192+
}
193+
194+
const partsA = CoreUrl.parse(urlA);
195+
const partsB = CoreUrl.parse(urlB);
196+
197+
return partsA.domain == partsB.domain &&
198+
CoreTextUtils.instance.removeEndingSlash(partsA.path) == CoreTextUtils.instance.removeEndingSlash(partsB.path);
199+
}
175200
}

0 commit comments

Comments
 (0)