summaryrefslogtreecommitdiffstats
path: root/caps/tests/gtest/TestPrincipalSerialization.cpp
blob: 2e7d5afd0dbdab3707a62e06c24f47c77f04a0b0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "gtest/gtest.h"
#include "mozilla/Base64.h"
#include "mozilla/BasePrincipal.h"
#include "mozilla/ContentPrincipal.h"
#include "mozilla/NullPrincipal.h"
#include "mozilla/SystemPrincipal.h"
#include "mozilla/ExpandedPrincipal.h"

using mozilla::BasePrincipal;
using mozilla::ContentPrincipal;
using mozilla::NullPrincipal;
using mozilla::SystemPrincipal;

// None of these tests work in debug due to assert guards
#ifndef MOZ_DEBUG

// calling toJSON() twice with the same string arg
// (ensure that we truncate correctly where needed)
TEST(PrincipalSerialization, ReusedJSONArgument)
{
  nsCOMPtr<nsIScriptSecurityManager> ssm =
      nsScriptSecurityManager::GetScriptSecurityManager();

  nsAutoCString spec("https://mozilla.com");
  nsCOMPtr<nsIPrincipal> principal;
  nsresult rv =
      ssm->CreateContentPrincipalFromOrigin(spec, getter_AddRefs(principal));
  ASSERT_EQ(rv, NS_OK);

  nsAutoCString JSON;
  rv = BasePrincipal::Cast(principal)->ToJSON(JSON);
  ASSERT_EQ(rv, NS_OK);
  ASSERT_TRUE(JSON.EqualsLiteral("{\"1\":{\"0\":\"https://mozilla.com/\"}}"));

  nsAutoCString spec2("https://example.com");
  nsCOMPtr<nsIPrincipal> principal2;
  rv = ssm->CreateContentPrincipalFromOrigin(spec2, getter_AddRefs(principal2));
  ASSERT_EQ(rv, NS_OK);

  // Reuse JSON without truncation to check the code is doing this
  rv = BasePrincipal::Cast(principal2)->ToJSON(JSON);
  ASSERT_EQ(rv, NS_OK);
  ASSERT_TRUE(JSON.EqualsLiteral("{\"1\":{\"0\":\"https://example.com/\"}}"));
}

// Double check that if we have two valid principals in a serialized JSON that
// nullptr is returned
TEST(PrincipalSerialization, TwoKeys)
{
  // Sanity check that this returns a system principal
  nsCOMPtr<nsIPrincipal> systemPrincipal =
      BasePrincipal::FromJSON("{\"3\":{}}"_ns);
  ASSERT_EQ(BasePrincipal::Cast(systemPrincipal)->Kind(),
            BasePrincipal::eSystemPrincipal);

  // Sanity check that this returns a content principal
  nsCOMPtr<nsIPrincipal> contentPrincipal =
      BasePrincipal::FromJSON("{\"1\":{\"0\":\"https://mozilla.com\"}}"_ns);
  ASSERT_EQ(BasePrincipal::Cast(contentPrincipal)->Kind(),
            BasePrincipal::eContentPrincipal);

  // Check both combined don't return a principal
  nsCOMPtr<nsIPrincipal> combinedPrincipal = BasePrincipal::FromJSON(
      "{\"1\":{\"0\":\"https://mozilla.com\"},\"3\":{}}"_ns);
  ASSERT_EQ(nullptr, combinedPrincipal);
}

#endif  // ifndef MOZ_DEBUG

TEST(PrincipalSerialization, ExpandedPrincipal)
{
  // Check basic Expandedprincipal works without OA
  nsCOMPtr<nsIScriptSecurityManager> ssm =
      nsScriptSecurityManager::GetScriptSecurityManager();

  uint32_t length = 2;
  nsTArray<nsCOMPtr<nsIPrincipal> > allowedDomains(length);
  allowedDomains.SetLength(length);

  nsAutoCString spec("https://mozilla.com");
  nsCOMPtr<nsIPrincipal> principal;
  nsresult rv =
      ssm->CreateContentPrincipalFromOrigin(spec, getter_AddRefs(principal));
  ASSERT_EQ(rv, NS_OK);
  ASSERT_EQ(BasePrincipal::Cast(principal)->Kind(),
            BasePrincipal::eContentPrincipal);
  allowedDomains[0] = principal;

  nsAutoCString spec2("https://mozilla.org");
  nsCOMPtr<nsIPrincipal> principal2;
  rv = ssm->CreateContentPrincipalFromOrigin(spec2, getter_AddRefs(principal2));
  ASSERT_EQ(rv, NS_OK);
  ASSERT_EQ(BasePrincipal::Cast(principal2)->Kind(),
            BasePrincipal::eContentPrincipal);
  allowedDomains[1] = principal2;

  mozilla::OriginAttributes attrs;
  RefPtr<ExpandedPrincipal> result =
      ExpandedPrincipal::Create(allowedDomains, attrs);
  ASSERT_EQ(BasePrincipal::Cast(result)->Kind(),
            BasePrincipal::eExpandedPrincipal);

  nsAutoCString JSON;
  rv = BasePrincipal::Cast(result)->ToJSON(JSON);
  ASSERT_EQ(rv, NS_OK);
  ASSERT_STREQ(JSON.get(),
               "{\"2\":{\"0\":[{\"1\":{\"0\":\"https://mozilla.com/"
               "\"}},{\"1\":{\"0\":\"https://mozilla.org/\"}}]}}");

  nsCOMPtr<nsIPrincipal> returnedPrincipal = BasePrincipal::FromJSON(JSON);
  auto outPrincipal = BasePrincipal::Cast(returnedPrincipal);
  ASSERT_EQ(outPrincipal->Kind(), BasePrincipal::eExpandedPrincipal);

  ASSERT_TRUE(outPrincipal->FastSubsumesIgnoringFPD(principal));
  ASSERT_TRUE(outPrincipal->FastSubsumesIgnoringFPD(principal2));

  nsAutoCString specDev("https://mozilla.dev");
  nsCOMPtr<nsIPrincipal> principalDev;
  rv = ssm->CreateContentPrincipalFromOrigin(specDev,
                                             getter_AddRefs(principalDev));
  ASSERT_EQ(rv, NS_OK);
  ASSERT_EQ(BasePrincipal::Cast(principalDev)->Kind(),
            BasePrincipal::eContentPrincipal);

  ASSERT_FALSE(outPrincipal->FastSubsumesIgnoringFPD(principalDev));
}

TEST(PrincipalSerialization, ExpandedPrincipalOA)
{
  // Check Expandedprincipal works with top level OA
  nsCOMPtr<nsIScriptSecurityManager> ssm =
      nsScriptSecurityManager::GetScriptSecurityManager();

  uint32_t length = 2;
  nsTArray<nsCOMPtr<nsIPrincipal> > allowedDomains(length);
  allowedDomains.SetLength(length);

  nsAutoCString spec("https://mozilla.com");
  nsCOMPtr<nsIPrincipal> principal;
  nsresult rv =
      ssm->CreateContentPrincipalFromOrigin(spec, getter_AddRefs(principal));
  ASSERT_EQ(rv, NS_OK);
  ASSERT_EQ(BasePrincipal::Cast(principal)->Kind(),
            BasePrincipal::eContentPrincipal);
  allowedDomains[0] = principal;

  nsAutoCString spec2("https://mozilla.org");
  nsCOMPtr<nsIPrincipal> principal2;
  rv = ssm->CreateContentPrincipalFromOrigin(spec2, getter_AddRefs(principal2));
  ASSERT_EQ(rv, NS_OK);
  ASSERT_EQ(BasePrincipal::Cast(principal2)->Kind(),
            BasePrincipal::eContentPrincipal);
  allowedDomains[1] = principal2;

  mozilla::OriginAttributes attrs;
  nsAutoCString suffix("^userContextId=1");
  bool ok = attrs.PopulateFromSuffix(suffix);
  ASSERT_TRUE(ok);

  RefPtr<ExpandedPrincipal> result =
      ExpandedPrincipal::Create(allowedDomains, attrs);
  ASSERT_EQ(BasePrincipal::Cast(result)->Kind(),
            BasePrincipal::eExpandedPrincipal);

  nsAutoCString JSON;
  rv = BasePrincipal::Cast(result)->ToJSON(JSON);
  ASSERT_EQ(rv, NS_OK);
  ASSERT_STREQ(JSON.get(),
               "{\"2\":{\"0\":[{\"1\":{\"0\":\"https://mozilla.com/"
               "\"}},{\"1\":{\"0\":\"https://mozilla.org/"
               "\"}}],\"1\":\"^userContextId=1\"}}");

  nsCOMPtr<nsIPrincipal> returnedPrincipal = BasePrincipal::FromJSON(JSON);
  auto outPrincipal = BasePrincipal::Cast(returnedPrincipal);
  ASSERT_EQ(outPrincipal->Kind(), BasePrincipal::eExpandedPrincipal);

  ASSERT_TRUE(outPrincipal->FastSubsumesIgnoringFPD(principal));
  ASSERT_TRUE(outPrincipal->FastSubsumesIgnoringFPD(principal2));

  nsAutoCString specDev("https://mozilla.dev");
  nsCOMPtr<nsIPrincipal> principalDev;
  rv = ssm->CreateContentPrincipalFromOrigin(specDev,
                                             getter_AddRefs(principalDev));
  ASSERT_EQ(rv, NS_OK);
  ASSERT_EQ(BasePrincipal::Cast(principalDev)->Kind(),
            BasePrincipal::eContentPrincipal);

  ASSERT_FALSE(outPrincipal->FastSubsumesIgnoringFPD(principalDev));
}