xref: /aosp_15_r20/external/perfetto/ui/src/frontend/post_message_handler_unittest.ts (revision 6dbdd20afdafa5e3ca9b8809fa73465d530080dc)
1// Copyright (C) 2024 The Android Open Source Project
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//      http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15import {isTrustedOrigin} from './post_message_handler';
16
17describe('postMessageHandler', () => {
18  test('baked-in trusted origins are trusted', () => {
19    expect(isTrustedOrigin('https://chrometto.googleplex.com')).toBeTruthy();
20    expect(isTrustedOrigin('https://uma.googleplex.com')).toBeTruthy();
21    expect(
22      isTrustedOrigin('https://android-build.googleplex.com'),
23    ).toBeTruthy();
24    expect(isTrustedOrigin('https://html5zombo.com')).toBeFalsy();
25  });
26
27  test('user trusted origins in local storage are trusted', () => {
28    try {
29      expect(isTrustedOrigin('https://html5zombo.com')).toBeFalsy();
30      window.localStorage['trustedOrigins'] = '["https://html5zombo.com"]';
31      expect(isTrustedOrigin('https://html5zombo.com')).toBeTruthy();
32    } finally {
33      window.localStorage.clear();
34    }
35  });
36
37  test('developer hostnames are trusted', () => {
38    expect(isTrustedOrigin('https://google.com')).toBeFalsy();
39    expect(isTrustedOrigin('https://broccoliman.corp.google.com')).toBeTruthy();
40    expect(isTrustedOrigin('http://broccoliman.corp.google.com')).toBeTruthy();
41    expect(isTrustedOrigin('https://broccoliman.c.googlers.com')).toBeTruthy();
42    expect(isTrustedOrigin('http://broccoliman.c.googlers.com')).toBeTruthy();
43    expect(isTrustedOrigin('https://broccolimancorp.google.com')).toBeFalsy();
44    expect(isTrustedOrigin('https://broccolimanc.googlers.com')).toBeFalsy();
45    expect(isTrustedOrigin('https://localhost')).toBeTruthy();
46    expect(isTrustedOrigin('http://localhost')).toBeTruthy();
47    expect(isTrustedOrigin('https://127.0.0.1')).toBeTruthy();
48    expect(isTrustedOrigin('http://127.0.0.1')).toBeTruthy();
49    // IPv6 localhost
50    expect(isTrustedOrigin('https://[::1]')).toBeTruthy();
51    expect(isTrustedOrigin('http://[::1]')).toBeTruthy();
52  });
53});
54