fp_fuzzer.cpp (2098B)
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 "mozilla/BasePrincipal.h" 9 #include "mozilla/dom/Feature.h" 10 #include "mozilla/dom/FeaturePolicyParser.h" 11 #include "nsNetUtil.h" 12 #include "nsStringFwd.h" 13 #include "nsTArray.h" 14 15 using namespace mozilla; 16 using namespace mozilla::dom; 17 18 constinit static nsCOMPtr<nsIPrincipal> selfURIPrincipal; 19 constinit static nsCOMPtr<nsIURI> selfURI; 20 21 static int LVVMFuzzerInitTest(int* argc, char*** argv) { 22 nsresult ret; 23 ret = NS_NewURI(getter_AddRefs(selfURI), "http://selfuri.com"); 24 if (ret != NS_OK) { 25 MOZ_CRASH("NS_NewURI failed."); 26 } 27 28 mozilla::OriginAttributes attrs; 29 selfURIPrincipal = 30 mozilla::BasePrincipal::CreateContentPrincipal(selfURI, attrs); 31 if (!selfURIPrincipal) { 32 MOZ_CRASH("CreateContentPrincipal failed."); 33 } 34 return 0; 35 } 36 37 static int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { 38 if (!size) { 39 return 0; 40 } 41 nsTArray<Feature> parsedFeatures; 42 43 NS_ConvertASCIItoUTF16 policy(reinterpret_cast<const char*>(data), size); 44 if (!policy.get()) return 0; 45 46 FeaturePolicyParser::ParseString(policy, nullptr, selfURIPrincipal, 47 selfURIPrincipal, parsedFeatures); 48 49 for (const Feature& feature : parsedFeatures) { 50 nsTArray<nsCOMPtr<nsIPrincipal>> list; 51 feature.GetAllowList(list); 52 53 for (nsIPrincipal* principal : list) { 54 nsAutoCString originNoSuffix; 55 nsresult rv = principal->GetOriginNoSuffix(originNoSuffix); 56 if (NS_WARN_IF(NS_FAILED(rv))) { 57 return 0; 58 } 59 printf("%s - %s\n", NS_ConvertUTF16toUTF8(feature.Name()).get(), 60 originNoSuffix.get()); 61 } 62 } 63 return 0; 64 } 65 66 MOZ_FUZZING_INTERFACE_RAW(LVVMFuzzerInitTest, LLVMFuzzerTestOneInput, 67 FeaturePolicyParser);