1/* 2 * Copyright (C) 2022 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17import {globalConfig} from 'common/global_config'; 18 19export class OriginAllowList { 20 private static readonly ALLOW_LIST_PROD = [ 21 new RegExp('^https://([^\\/]*\\.)*googleplex\\.com$'), 22 new RegExp('^https://([^\\/]*\\.)*google\\.com$'), 23 ]; 24 25 private static readonly ALLOW_LIST_DEV = [ 26 ...OriginAllowList.ALLOW_LIST_PROD, 27 new RegExp('^(http|https)://localhost:8081$'), // remote tool mock 28 ]; 29 30 static isAllowed(originUrl: string, mode = globalConfig.MODE): boolean { 31 const list = OriginAllowList.getList(mode); 32 33 for (const regex of list) { 34 if (regex.test(originUrl)) { 35 return true; 36 } 37 } 38 39 return false; 40 } 41 42 private static getList(mode: typeof globalConfig.MODE): RegExp[] { 43 switch (mode) { 44 case 'DEV': 45 return OriginAllowList.ALLOW_LIST_DEV; 46 case 'PROD': 47 return OriginAllowList.ALLOW_LIST_PROD; 48 default: 49 throw new Error(`Unhandled mode: ${globalConfig.MODE}`); 50 } 51 } 52} 53