/src/mozilla-central/dom/security/fuzztest/csp_fuzzer.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
2 | | /* vim: set ts=8 sts=2 et sw=2 tw=80: */ |
3 | | /* This Source Code Form is subject to the terms of the Mozilla Public |
4 | | * License, v. 2.0. If a copy of the MPL was not distributed with this |
5 | | * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ |
6 | | |
7 | | #include "FuzzingInterface.h" |
8 | | #include "nsCSPContext.h" |
9 | | #include "nsNetUtil.h" |
10 | | #include "nsStringFwd.h" |
11 | | |
12 | | static int |
13 | | LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) |
14 | 5.77k | { |
15 | 5.77k | nsresult ret; |
16 | 5.77k | nsCOMPtr<nsIURI> selfURI; |
17 | 5.77k | ret = NS_NewURI(getter_AddRefs(selfURI), "http://selfuri.com"); |
18 | 5.77k | if (ret != NS_OK) |
19 | 0 | return 0; |
20 | 5.77k | |
21 | 5.77k | mozilla::OriginAttributes attrs; |
22 | 5.77k | nsCOMPtr<nsIPrincipal> selfURIPrincipal = |
23 | 5.77k | mozilla::BasePrincipal::CreateCodebasePrincipal(selfURI, attrs); |
24 | 5.77k | if (!selfURIPrincipal) |
25 | 0 | return 0; |
26 | 5.77k | |
27 | 5.77k | nsCOMPtr<nsIContentSecurityPolicy> csp = |
28 | 5.77k | do_CreateInstance(NS_CSPCONTEXT_CONTRACTID, &ret); |
29 | 5.77k | if (ret != NS_OK) |
30 | 0 | return 0; |
31 | 5.77k | |
32 | 5.77k | ret = csp->SetRequestContext(nullptr, selfURIPrincipal); |
33 | 5.77k | if (ret != NS_OK) |
34 | 0 | return 0; |
35 | 5.77k | |
36 | 5.77k | NS_ConvertASCIItoUTF16 policy(reinterpret_cast<const char*>(data), size); |
37 | 5.77k | if (!policy.get()) |
38 | 0 | return 0; |
39 | 5.77k | csp->AppendPolicy(policy, false, false); |
40 | 5.77k | |
41 | 5.77k | return 0; |
42 | 5.77k | } |
43 | | |
44 | | MOZ_FUZZING_INTERFACE_RAW(nullptr, LLVMFuzzerTestOneInput, ContentSecurityPolicyParser); |
45 | | |