2017-12-06 23:18:35 +01:00
|
|
|
// Copyright 2017 Google Inc. All rights reserved.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package android
|
|
|
|
|
|
|
|
import (
|
2021-10-29 00:14:04 +02:00
|
|
|
"fmt"
|
2017-12-06 23:18:35 +01:00
|
|
|
"path/filepath"
|
|
|
|
"reflect"
|
2020-04-09 15:18:21 +02:00
|
|
|
"regexp"
|
2017-12-06 23:18:35 +01:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/google/blueprint/proptools"
|
|
|
|
)
|
|
|
|
|
|
|
|
// "neverallow" rules for the build system.
|
|
|
|
//
|
|
|
|
// This allows things which aren't related to the build system and are enforced
|
2021-10-28 00:47:06 +02:00
|
|
|
// against assumptions, in progress code refactors, or policy to be expressed in a
|
2017-12-06 23:18:35 +01:00
|
|
|
// straightforward away disjoint from implementations and tests which should
|
|
|
|
// work regardless of these restrictions.
|
|
|
|
//
|
|
|
|
// A module is disallowed if all of the following are true:
|
2019-06-27 15:08:51 +02:00
|
|
|
// - it is in one of the "In" paths
|
|
|
|
// - it is not in one of the "NotIn" paths
|
|
|
|
// - it has all "With" properties matched
|
2017-12-06 23:18:35 +01:00
|
|
|
// - - values are matched in their entirety
|
|
|
|
// - - nil is interpreted as an empty string
|
|
|
|
// - - nested properties are separated with a '.'
|
|
|
|
// - - if the property is a list, any of the values in the list being matches
|
|
|
|
// counts as a match
|
2019-06-27 15:08:51 +02:00
|
|
|
// - it has none of the "Without" properties matched (same rules as above)
|
2017-12-06 23:18:35 +01:00
|
|
|
|
2021-03-31 00:07:52 +02:00
|
|
|
func registerNeverallowMutator(ctx RegisterMutatorsContext) {
|
2017-12-06 23:18:35 +01:00
|
|
|
ctx.BottomUp("neverallow", neverallowMutator).Parallel()
|
|
|
|
}
|
|
|
|
|
2019-06-27 15:08:51 +02:00
|
|
|
var neverallows = []Rule{}
|
2018-10-21 18:19:10 +02:00
|
|
|
|
2019-06-27 15:08:51 +02:00
|
|
|
func init() {
|
2019-07-22 13:13:55 +02:00
|
|
|
AddNeverAllowRules(createIncludeDirsRules()...)
|
2019-06-27 15:08:51 +02:00
|
|
|
AddNeverAllowRules(createTrebleRules()...)
|
|
|
|
AddNeverAllowRules(createJavaDeviceForHostRules()...)
|
2020-04-07 18:50:32 +02:00
|
|
|
AddNeverAllowRules(createCcSdkVariantRules()...)
|
2020-05-20 23:20:28 +02:00
|
|
|
AddNeverAllowRules(createUncompressDexRules()...)
|
2020-07-27 21:59:58 +02:00
|
|
|
AddNeverAllowRules(createMakefileGoalRules()...)
|
2021-06-14 05:03:51 +02:00
|
|
|
AddNeverAllowRules(createInitFirstStageRules()...)
|
2022-04-05 08:29:53 +02:00
|
|
|
AddNeverAllowRules(createProhibitFrameworkAccessRules()...)
|
2022-10-10 16:30:03 +02:00
|
|
|
AddNeverAllowRules(createBp2BuildRule())
|
2019-06-27 15:08:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Add a NeverAllow rule to the set of rules to apply.
|
|
|
|
func AddNeverAllowRules(rules ...Rule) {
|
|
|
|
neverallows = append(neverallows, rules...)
|
2018-10-21 18:19:10 +02:00
|
|
|
}
|
|
|
|
|
2022-10-10 16:30:03 +02:00
|
|
|
func createBp2BuildRule() Rule {
|
|
|
|
return NeverAllow().
|
|
|
|
With("bazel_module.bp2build_available", "true").
|
2022-10-26 09:26:50 +02:00
|
|
|
NotIn("soong_tests"). // only used in tests
|
2022-10-10 16:30:03 +02:00
|
|
|
Because("setting bp2build_available in Android.bp is not " +
|
|
|
|
"supported for custom conversion, use allowlists.go instead.")
|
2022-10-07 11:54:16 +02:00
|
|
|
}
|
|
|
|
|
2019-07-22 13:13:55 +02:00
|
|
|
func createIncludeDirsRules() []Rule {
|
2021-04-27 04:31:07 +02:00
|
|
|
notInIncludeDir := []string{
|
2019-07-22 13:13:55 +02:00
|
|
|
"art",
|
2019-11-06 14:39:46 +01:00
|
|
|
"art/libnativebridge",
|
|
|
|
"art/libnativeloader",
|
2019-07-22 13:13:55 +02:00
|
|
|
"libcore",
|
|
|
|
"libnativehelper",
|
|
|
|
"external/apache-harmony",
|
|
|
|
"external/apache-xml",
|
|
|
|
"external/boringssl",
|
|
|
|
"external/bouncycastle",
|
|
|
|
"external/conscrypt",
|
|
|
|
"external/icu",
|
|
|
|
"external/okhttp",
|
|
|
|
"external/vixl",
|
|
|
|
"external/wycheproof",
|
|
|
|
}
|
2021-04-27 04:31:07 +02:00
|
|
|
noUseIncludeDir := []string{
|
2021-04-27 20:03:14 +02:00
|
|
|
"frameworks/av/apex",
|
|
|
|
"frameworks/av/tools",
|
|
|
|
"frameworks/native/cmds",
|
|
|
|
"system/apex",
|
|
|
|
"system/bpf",
|
|
|
|
"system/gatekeeper",
|
|
|
|
"system/hwservicemanager",
|
|
|
|
"system/libbase",
|
2021-04-27 04:31:07 +02:00
|
|
|
"system/libfmq",
|
2021-04-27 20:03:14 +02:00
|
|
|
"system/libvintf",
|
2021-04-27 04:31:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
rules := make([]Rule, 0, len(notInIncludeDir)+len(noUseIncludeDir))
|
2019-07-22 13:13:55 +02:00
|
|
|
|
2021-04-27 04:31:07 +02:00
|
|
|
for _, path := range notInIncludeDir {
|
2019-07-22 13:13:55 +02:00
|
|
|
rule :=
|
|
|
|
NeverAllow().
|
|
|
|
WithMatcher("include_dirs", StartsWith(path+"/")).
|
|
|
|
Because("include_dirs is deprecated, all usages of '" + path + "' have been migrated" +
|
|
|
|
" to use alternate mechanisms and so can no longer be used.")
|
|
|
|
|
|
|
|
rules = append(rules, rule)
|
|
|
|
}
|
|
|
|
|
2021-04-27 04:31:07 +02:00
|
|
|
for _, path := range noUseIncludeDir {
|
|
|
|
rule := NeverAllow().In(path+"/").WithMatcher("include_dirs", isSetMatcherInstance).
|
|
|
|
Because("include_dirs is deprecated, all usages of them in '" + path + "' have been migrated" +
|
|
|
|
" to use alternate mechanisms and so can no longer be used.")
|
|
|
|
rules = append(rules, rule)
|
|
|
|
}
|
|
|
|
|
2019-07-22 13:13:55 +02:00
|
|
|
return rules
|
|
|
|
}
|
|
|
|
|
2019-06-27 15:08:51 +02:00
|
|
|
func createTrebleRules() []Rule {
|
|
|
|
return []Rule{
|
|
|
|
NeverAllow().
|
|
|
|
In("vendor", "device").
|
|
|
|
With("vndk.enabled", "true").
|
|
|
|
Without("vendor", "true").
|
2020-02-28 07:07:59 +01:00
|
|
|
Without("product_specific", "true").
|
2019-06-27 15:08:51 +02:00
|
|
|
Because("the VNDK can never contain a library that is device dependent."),
|
|
|
|
NeverAllow().
|
|
|
|
With("vndk.enabled", "true").
|
|
|
|
Without("vendor", "true").
|
|
|
|
Without("owner", "").
|
|
|
|
Because("a VNDK module can never have an owner."),
|
2018-10-21 18:19:10 +02:00
|
|
|
|
|
|
|
// TODO(b/67974785): always enforce the manifest
|
2019-06-27 15:08:51 +02:00
|
|
|
NeverAllow().
|
2020-02-11 02:21:32 +01:00
|
|
|
Without("name", "libhidlbase-combined-impl").
|
|
|
|
Without("name", "libhidlbase").
|
2019-06-27 15:08:51 +02:00
|
|
|
With("product_variables.enforce_vintf_manifest.cflags", "*").
|
|
|
|
Because("manifest enforcement should be independent of ."),
|
2018-10-21 18:19:10 +02:00
|
|
|
|
|
|
|
// TODO(b/67975799): vendor code should always use /vendor/bin/sh
|
2019-06-27 15:08:51 +02:00
|
|
|
NeverAllow().
|
|
|
|
Without("name", "libc_bionic_ndk").
|
|
|
|
With("product_variables.treble_linker_namespaces.cflags", "*").
|
|
|
|
Because("nothing should care if linker namespaces are enabled or not"),
|
2018-10-21 18:19:10 +02:00
|
|
|
|
|
|
|
// Example:
|
2019-06-27 15:08:51 +02:00
|
|
|
// *NeverAllow().with("Srcs", "main.cpp"))
|
2018-10-21 18:19:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-27 15:08:51 +02:00
|
|
|
func createJavaDeviceForHostRules() []Rule {
|
2020-06-11 20:32:11 +02:00
|
|
|
javaDeviceForHostProjectsAllowedList := []string{
|
2021-07-14 06:52:04 +02:00
|
|
|
"development/build",
|
2019-04-11 23:07:38 +02:00
|
|
|
"external/guava",
|
2022-10-18 23:09:28 +02:00
|
|
|
"external/kotlinx.coroutines",
|
2019-03-06 00:06:16 +01:00
|
|
|
"external/robolectric-shadows",
|
2022-08-25 19:29:50 +02:00
|
|
|
"external/robolectric",
|
2021-09-23 13:38:08 +02:00
|
|
|
"frameworks/layoutlib",
|
2019-03-06 00:06:16 +01:00
|
|
|
}
|
|
|
|
|
2019-06-27 15:08:51 +02:00
|
|
|
return []Rule{
|
|
|
|
NeverAllow().
|
2020-06-11 20:32:11 +02:00
|
|
|
NotIn(javaDeviceForHostProjectsAllowedList...).
|
2019-06-27 15:08:51 +02:00
|
|
|
ModuleType("java_device_for_host", "java_host_for_device").
|
2020-06-11 20:32:11 +02:00
|
|
|
Because("java_device_for_host can only be used in allowed projects"),
|
2019-03-06 00:06:16 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-07 18:50:32 +02:00
|
|
|
func createCcSdkVariantRules() []Rule {
|
2020-06-11 20:32:11 +02:00
|
|
|
sdkVersionOnlyAllowedList := []string{
|
2020-04-07 18:50:32 +02:00
|
|
|
// derive_sdk_prefer32 has stem: "derive_sdk" which conflicts with the derive_sdk.
|
|
|
|
// This sometimes works because the APEX modules that contain derive_sdk and
|
|
|
|
// derive_sdk_prefer32 suppress the platform installation rules, but fails when
|
|
|
|
// the APEX modules contain the SDK variant and the platform variant still exists.
|
2020-05-27 19:25:23 +02:00
|
|
|
"packages/modules/SdkExtensions/derive_sdk",
|
2020-04-20 23:46:47 +02:00
|
|
|
// These are for apps and shouldn't be used by non-SDK variant modules.
|
|
|
|
"prebuilts/ndk",
|
|
|
|
"tools/test/graphicsbenchmark/apps/sample_app",
|
|
|
|
"tools/test/graphicsbenchmark/functional_tests/java",
|
2020-04-20 23:46:47 +02:00
|
|
|
"vendor/xts/gts-tests/hostsidetests/gamedevicecert/apps/javatests",
|
2021-06-18 16:04:50 +02:00
|
|
|
"external/libtextclassifier/native",
|
2020-04-07 18:50:32 +02:00
|
|
|
}
|
|
|
|
|
2020-06-11 20:32:11 +02:00
|
|
|
platformVariantPropertiesAllowedList := []string{
|
2020-04-07 18:50:32 +02:00
|
|
|
// android_native_app_glue and libRSSupport use native_window.h but target old
|
|
|
|
// sdk versions (minimum and 9 respectively) where libnativewindow didn't exist,
|
|
|
|
// so they can't add libnativewindow to shared_libs to get the header directory
|
|
|
|
// for the platform variant. Allow them to use the platform variant
|
|
|
|
// property to set shared_libs.
|
|
|
|
"prebuilts/ndk",
|
|
|
|
"frameworks/rs",
|
|
|
|
}
|
|
|
|
|
|
|
|
return []Rule{
|
|
|
|
NeverAllow().
|
2020-06-11 20:32:11 +02:00
|
|
|
NotIn(sdkVersionOnlyAllowedList...).
|
2020-04-07 18:50:32 +02:00
|
|
|
WithMatcher("sdk_variant_only", isSetMatcherInstance).
|
2020-06-11 20:32:11 +02:00
|
|
|
Because("sdk_variant_only can only be used in allowed projects"),
|
2020-04-07 18:50:32 +02:00
|
|
|
NeverAllow().
|
2020-06-11 20:32:11 +02:00
|
|
|
NotIn(platformVariantPropertiesAllowedList...).
|
2020-04-07 18:50:32 +02:00
|
|
|
WithMatcher("platform.shared_libs", isSetMatcherInstance).
|
2020-06-11 20:32:11 +02:00
|
|
|
Because("platform variant properties can only be used in allowed projects"),
|
2020-04-07 18:50:32 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-20 23:20:28 +02:00
|
|
|
func createUncompressDexRules() []Rule {
|
|
|
|
return []Rule{
|
|
|
|
NeverAllow().
|
|
|
|
NotIn("art").
|
|
|
|
WithMatcher("uncompress_dex", isSetMatcherInstance).
|
|
|
|
Because("uncompress_dex is only allowed for certain jars for test in art."),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-27 21:59:58 +02:00
|
|
|
func createMakefileGoalRules() []Rule {
|
2022-07-25 05:32:32 +02:00
|
|
|
allowlist := []string{
|
|
|
|
// libwifi_hal uses makefile_goal for its dependencies
|
|
|
|
"frameworks/opt/net/wifi/libwifi_hal",
|
|
|
|
}
|
2020-07-27 21:59:58 +02:00
|
|
|
return []Rule{
|
|
|
|
NeverAllow().
|
|
|
|
ModuleType("makefile_goal").
|
|
|
|
WithoutMatcher("product_out_path", Regexp("^boot[0-9a-zA-Z.-]*[.]img$")).
|
2022-07-25 05:32:32 +02:00
|
|
|
NotIn(allowlist...).
|
|
|
|
Because("Only boot images may be imported as a makefile goal if not in allowed projects"),
|
2020-07-27 21:59:58 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-14 05:03:51 +02:00
|
|
|
func createInitFirstStageRules() []Rule {
|
|
|
|
return []Rule{
|
|
|
|
NeverAllow().
|
|
|
|
Without("name", "init_first_stage").
|
|
|
|
With("install_in_root", "true").
|
|
|
|
Because("install_in_root is only for init_first_stage."),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-05 08:29:53 +02:00
|
|
|
func createProhibitFrameworkAccessRules() []Rule {
|
|
|
|
return []Rule{
|
|
|
|
NeverAllow().
|
|
|
|
With("libs", "framework").
|
|
|
|
WithoutMatcher("sdk_version", Regexp("(core_.*|^$)")).
|
|
|
|
Because("framework can't be used when building against SDK"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-06 23:18:35 +01:00
|
|
|
func neverallowMutator(ctx BottomUpMutatorContext) {
|
|
|
|
m, ok := ctx.Module().(Module)
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
dir := ctx.ModuleDir() + "/"
|
|
|
|
properties := m.GetProperties()
|
|
|
|
|
2019-07-26 11:48:06 +02:00
|
|
|
osClass := ctx.Module().Target().Os.Class
|
|
|
|
|
2019-08-07 16:31:07 +02:00
|
|
|
for _, r := range neverallowRules(ctx.Config()) {
|
2019-06-27 15:08:51 +02:00
|
|
|
n := r.(*rule)
|
2017-12-06 23:18:35 +01:00
|
|
|
if !n.appliesToPath(dir) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2019-03-06 00:06:16 +01:00
|
|
|
if !n.appliesToModuleType(ctx.ModuleType()) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-12-23 16:05:38 +01:00
|
|
|
if !n.appliesToProperties(properties) {
|
2017-12-06 23:18:35 +01:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2019-07-26 11:48:06 +02:00
|
|
|
if !n.appliesToOsClass(osClass) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2019-07-25 16:41:09 +02:00
|
|
|
if !n.appliesToDirectDeps(ctx) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2017-12-06 23:18:35 +01:00
|
|
|
ctx.ModuleErrorf("violates " + n.String())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-12 15:12:49 +02:00
|
|
|
type ValueMatcher interface {
|
2021-12-23 16:05:38 +01:00
|
|
|
Test(string) bool
|
2019-07-12 15:12:49 +02:00
|
|
|
String() string
|
|
|
|
}
|
|
|
|
|
|
|
|
type equalMatcher struct {
|
|
|
|
expected string
|
|
|
|
}
|
|
|
|
|
2021-12-23 16:05:38 +01:00
|
|
|
func (m *equalMatcher) Test(value string) bool {
|
2019-07-12 15:12:49 +02:00
|
|
|
return m.expected == value
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *equalMatcher) String() string {
|
|
|
|
return "=" + m.expected
|
|
|
|
}
|
|
|
|
|
|
|
|
type anyMatcher struct {
|
|
|
|
}
|
|
|
|
|
2021-12-23 16:05:38 +01:00
|
|
|
func (m *anyMatcher) Test(value string) bool {
|
2019-07-12 15:12:49 +02:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *anyMatcher) String() string {
|
|
|
|
return "=*"
|
|
|
|
}
|
|
|
|
|
|
|
|
var anyMatcherInstance = &anyMatcher{}
|
|
|
|
|
2019-07-22 13:13:55 +02:00
|
|
|
type startsWithMatcher struct {
|
|
|
|
prefix string
|
|
|
|
}
|
|
|
|
|
2021-12-23 16:05:38 +01:00
|
|
|
func (m *startsWithMatcher) Test(value string) bool {
|
2019-07-22 13:13:55 +02:00
|
|
|
return strings.HasPrefix(value, m.prefix)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *startsWithMatcher) String() string {
|
|
|
|
return ".starts-with(" + m.prefix + ")"
|
|
|
|
}
|
|
|
|
|
2020-04-09 15:18:21 +02:00
|
|
|
type regexMatcher struct {
|
|
|
|
re *regexp.Regexp
|
|
|
|
}
|
|
|
|
|
2021-12-23 16:05:38 +01:00
|
|
|
func (m *regexMatcher) Test(value string) bool {
|
2020-04-09 15:18:21 +02:00
|
|
|
return m.re.MatchString(value)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *regexMatcher) String() string {
|
|
|
|
return ".regexp(" + m.re.String() + ")"
|
|
|
|
}
|
|
|
|
|
2020-06-05 22:14:03 +02:00
|
|
|
type notInListMatcher struct {
|
|
|
|
allowed []string
|
|
|
|
}
|
|
|
|
|
2021-12-23 16:05:38 +01:00
|
|
|
func (m *notInListMatcher) Test(value string) bool {
|
2020-06-05 22:14:03 +02:00
|
|
|
return !InList(value, m.allowed)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *notInListMatcher) String() string {
|
|
|
|
return ".not-in-list(" + strings.Join(m.allowed, ",") + ")"
|
|
|
|
}
|
|
|
|
|
2020-04-07 18:50:32 +02:00
|
|
|
type isSetMatcher struct{}
|
|
|
|
|
2021-12-23 16:05:38 +01:00
|
|
|
func (m *isSetMatcher) Test(value string) bool {
|
2020-04-07 18:50:32 +02:00
|
|
|
return value != ""
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *isSetMatcher) String() string {
|
|
|
|
return ".is-set"
|
|
|
|
}
|
|
|
|
|
|
|
|
var isSetMatcherInstance = &isSetMatcher{}
|
|
|
|
|
2017-12-06 23:18:35 +01:00
|
|
|
type ruleProperty struct {
|
2019-07-12 15:12:49 +02:00
|
|
|
fields []string // e.x.: Vndk.Enabled
|
|
|
|
matcher ValueMatcher
|
2017-12-06 23:18:35 +01:00
|
|
|
}
|
|
|
|
|
2021-10-29 00:14:04 +02:00
|
|
|
func (r *ruleProperty) String() string {
|
|
|
|
return fmt.Sprintf("%q matches: %s", strings.Join(r.fields, "."), r.matcher)
|
|
|
|
}
|
|
|
|
|
|
|
|
type ruleProperties []ruleProperty
|
|
|
|
|
|
|
|
func (r ruleProperties) String() string {
|
|
|
|
var s []string
|
|
|
|
for _, r := range r {
|
|
|
|
s = append(s, r.String())
|
|
|
|
}
|
|
|
|
return strings.Join(s, " ")
|
|
|
|
}
|
|
|
|
|
2019-06-27 15:08:51 +02:00
|
|
|
// A NeverAllow rule.
|
|
|
|
type Rule interface {
|
|
|
|
In(path ...string) Rule
|
|
|
|
|
|
|
|
NotIn(path ...string) Rule
|
|
|
|
|
2019-07-25 16:41:09 +02:00
|
|
|
InDirectDeps(deps ...string) Rule
|
|
|
|
|
2019-07-26 11:48:06 +02:00
|
|
|
WithOsClass(osClasses ...OsClass) Rule
|
|
|
|
|
2019-06-27 15:08:51 +02:00
|
|
|
ModuleType(types ...string) Rule
|
|
|
|
|
|
|
|
NotModuleType(types ...string) Rule
|
|
|
|
|
|
|
|
With(properties, value string) Rule
|
|
|
|
|
2019-07-22 13:13:55 +02:00
|
|
|
WithMatcher(properties string, matcher ValueMatcher) Rule
|
|
|
|
|
2019-06-27 15:08:51 +02:00
|
|
|
Without(properties, value string) Rule
|
|
|
|
|
2019-07-22 13:13:55 +02:00
|
|
|
WithoutMatcher(properties string, matcher ValueMatcher) Rule
|
|
|
|
|
2019-06-27 15:08:51 +02:00
|
|
|
Because(reason string) Rule
|
|
|
|
}
|
|
|
|
|
2017-12-06 23:18:35 +01:00
|
|
|
type rule struct {
|
|
|
|
// User string for why this is a thing.
|
|
|
|
reason string
|
|
|
|
|
|
|
|
paths []string
|
|
|
|
unlessPaths []string
|
|
|
|
|
2019-07-25 16:41:09 +02:00
|
|
|
directDeps map[string]bool
|
|
|
|
|
2019-07-26 11:48:06 +02:00
|
|
|
osClasses []OsClass
|
|
|
|
|
2019-03-06 00:06:16 +01:00
|
|
|
moduleTypes []string
|
|
|
|
unlessModuleTypes []string
|
|
|
|
|
2021-10-29 00:14:04 +02:00
|
|
|
props ruleProperties
|
|
|
|
unlessProps ruleProperties
|
2020-06-05 22:14:03 +02:00
|
|
|
|
|
|
|
onlyBootclasspathJar bool
|
2017-12-06 23:18:35 +01:00
|
|
|
}
|
|
|
|
|
2019-06-27 15:08:51 +02:00
|
|
|
// Create a new NeverAllow rule.
|
|
|
|
func NeverAllow() Rule {
|
2019-07-25 16:41:09 +02:00
|
|
|
return &rule{directDeps: make(map[string]bool)}
|
2017-12-06 23:18:35 +01:00
|
|
|
}
|
2019-03-06 00:06:16 +01:00
|
|
|
|
2021-10-29 00:14:04 +02:00
|
|
|
// In adds path(s) where this rule applies.
|
2019-06-27 15:08:51 +02:00
|
|
|
func (r *rule) In(path ...string) Rule {
|
2017-12-06 23:18:35 +01:00
|
|
|
r.paths = append(r.paths, cleanPaths(path)...)
|
|
|
|
return r
|
|
|
|
}
|
2019-03-06 00:06:16 +01:00
|
|
|
|
2021-10-29 00:14:04 +02:00
|
|
|
// NotIn adds path(s) to that this rule does not apply to.
|
2019-06-27 15:08:51 +02:00
|
|
|
func (r *rule) NotIn(path ...string) Rule {
|
2017-12-06 23:18:35 +01:00
|
|
|
r.unlessPaths = append(r.unlessPaths, cleanPaths(path)...)
|
|
|
|
return r
|
|
|
|
}
|
2019-03-06 00:06:16 +01:00
|
|
|
|
2021-10-29 00:14:04 +02:00
|
|
|
// InDirectDeps adds dep(s) that are not allowed with this rule.
|
2019-07-25 16:41:09 +02:00
|
|
|
func (r *rule) InDirectDeps(deps ...string) Rule {
|
|
|
|
for _, d := range deps {
|
|
|
|
r.directDeps[d] = true
|
|
|
|
}
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
2021-10-29 00:14:04 +02:00
|
|
|
// WithOsClass adds osClass(es) that this rule applies to.
|
2019-07-26 11:48:06 +02:00
|
|
|
func (r *rule) WithOsClass(osClasses ...OsClass) Rule {
|
|
|
|
r.osClasses = append(r.osClasses, osClasses...)
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
2021-10-29 00:14:04 +02:00
|
|
|
// ModuleType adds type(s) that this rule applies to.
|
2019-06-27 15:08:51 +02:00
|
|
|
func (r *rule) ModuleType(types ...string) Rule {
|
2019-03-06 00:06:16 +01:00
|
|
|
r.moduleTypes = append(r.moduleTypes, types...)
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
2021-10-29 00:14:04 +02:00
|
|
|
// NotModuleType adds type(s) that this rule does not apply to..
|
2019-06-27 15:08:51 +02:00
|
|
|
func (r *rule) NotModuleType(types ...string) Rule {
|
2019-03-06 00:06:16 +01:00
|
|
|
r.unlessModuleTypes = append(r.unlessModuleTypes, types...)
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
2021-10-29 00:14:04 +02:00
|
|
|
// With specifies property/value combinations that are restricted for this rule.
|
2019-06-27 15:08:51 +02:00
|
|
|
func (r *rule) With(properties, value string) Rule {
|
2019-07-22 13:13:55 +02:00
|
|
|
return r.WithMatcher(properties, selectMatcher(value))
|
|
|
|
}
|
|
|
|
|
2021-10-29 00:14:04 +02:00
|
|
|
// WithMatcher specifies property/matcher combinations that are restricted for this rule.
|
2019-07-22 13:13:55 +02:00
|
|
|
func (r *rule) WithMatcher(properties string, matcher ValueMatcher) Rule {
|
2017-12-06 23:18:35 +01:00
|
|
|
r.props = append(r.props, ruleProperty{
|
2019-07-12 15:12:49 +02:00
|
|
|
fields: fieldNamesForProperties(properties),
|
2019-07-22 13:13:55 +02:00
|
|
|
matcher: matcher,
|
2017-12-06 23:18:35 +01:00
|
|
|
})
|
|
|
|
return r
|
|
|
|
}
|
2019-03-06 00:06:16 +01:00
|
|
|
|
2021-10-29 00:14:04 +02:00
|
|
|
// Without specifies property/value combinations that this rule does not apply to.
|
2019-06-27 15:08:51 +02:00
|
|
|
func (r *rule) Without(properties, value string) Rule {
|
2019-07-22 13:13:55 +02:00
|
|
|
return r.WithoutMatcher(properties, selectMatcher(value))
|
|
|
|
}
|
|
|
|
|
2021-10-29 00:14:04 +02:00
|
|
|
// Without specifies property/matcher combinations that this rule does not apply to.
|
2019-07-22 13:13:55 +02:00
|
|
|
func (r *rule) WithoutMatcher(properties string, matcher ValueMatcher) Rule {
|
2017-12-06 23:18:35 +01:00
|
|
|
r.unlessProps = append(r.unlessProps, ruleProperty{
|
2019-07-12 15:12:49 +02:00
|
|
|
fields: fieldNamesForProperties(properties),
|
2019-07-22 13:13:55 +02:00
|
|
|
matcher: matcher,
|
2017-12-06 23:18:35 +01:00
|
|
|
})
|
|
|
|
return r
|
|
|
|
}
|
2019-03-06 00:06:16 +01:00
|
|
|
|
2019-07-12 15:12:49 +02:00
|
|
|
func selectMatcher(expected string) ValueMatcher {
|
|
|
|
if expected == "*" {
|
|
|
|
return anyMatcherInstance
|
|
|
|
}
|
|
|
|
return &equalMatcher{expected: expected}
|
|
|
|
}
|
|
|
|
|
2021-10-29 00:14:04 +02:00
|
|
|
// Because specifies a reason for this rule.
|
2019-06-27 15:08:51 +02:00
|
|
|
func (r *rule) Because(reason string) Rule {
|
2017-12-06 23:18:35 +01:00
|
|
|
r.reason = reason
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *rule) String() string {
|
2021-10-29 00:14:04 +02:00
|
|
|
s := []string{"neverallow requirements. Not allowed:"}
|
|
|
|
if len(r.paths) > 0 {
|
|
|
|
s = append(s, fmt.Sprintf("in dirs: %q", r.paths))
|
2017-12-06 23:18:35 +01:00
|
|
|
}
|
2021-10-29 00:14:04 +02:00
|
|
|
if len(r.moduleTypes) > 0 {
|
|
|
|
s = append(s, fmt.Sprintf("module types: %q", r.moduleTypes))
|
2017-12-06 23:18:35 +01:00
|
|
|
}
|
2021-10-29 00:14:04 +02:00
|
|
|
if len(r.props) > 0 {
|
|
|
|
s = append(s, fmt.Sprintf("properties matching: %s", r.props))
|
2019-03-06 00:06:16 +01:00
|
|
|
}
|
2021-10-29 00:14:04 +02:00
|
|
|
if len(r.directDeps) > 0 {
|
|
|
|
s = append(s, fmt.Sprintf("dep(s): %q", SortedStringKeys(r.directDeps)))
|
2019-03-06 00:06:16 +01:00
|
|
|
}
|
2021-10-29 00:14:04 +02:00
|
|
|
if len(r.osClasses) > 0 {
|
|
|
|
s = append(s, fmt.Sprintf("os class(es): %q", r.osClasses))
|
2017-12-06 23:18:35 +01:00
|
|
|
}
|
2021-10-29 00:14:04 +02:00
|
|
|
if len(r.unlessPaths) > 0 {
|
|
|
|
s = append(s, fmt.Sprintf("EXCEPT in dirs: %q", r.unlessPaths))
|
2019-07-25 16:41:09 +02:00
|
|
|
}
|
2021-10-29 00:14:04 +02:00
|
|
|
if len(r.unlessModuleTypes) > 0 {
|
|
|
|
s = append(s, fmt.Sprintf("EXCEPT module types: %q", r.unlessModuleTypes))
|
2019-07-26 11:48:06 +02:00
|
|
|
}
|
2021-10-29 00:14:04 +02:00
|
|
|
if len(r.unlessProps) > 0 {
|
|
|
|
s = append(s, fmt.Sprintf("EXCEPT properties matching: %q", r.unlessProps))
|
2020-06-05 22:14:03 +02:00
|
|
|
}
|
2017-12-06 23:18:35 +01:00
|
|
|
if len(r.reason) != 0 {
|
2021-10-29 00:14:04 +02:00
|
|
|
s = append(s, " which is restricted because "+r.reason)
|
|
|
|
}
|
|
|
|
if len(s) == 1 {
|
|
|
|
s[0] = "neverallow requirements (empty)"
|
2017-12-06 23:18:35 +01:00
|
|
|
}
|
2021-10-29 00:14:04 +02:00
|
|
|
return strings.Join(s, "\n\t")
|
2017-12-06 23:18:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *rule) appliesToPath(dir string) bool {
|
2020-02-11 16:54:35 +01:00
|
|
|
includePath := len(r.paths) == 0 || HasAnyPrefix(dir, r.paths)
|
|
|
|
excludePath := HasAnyPrefix(dir, r.unlessPaths)
|
2017-12-06 23:18:35 +01:00
|
|
|
return includePath && !excludePath
|
|
|
|
}
|
|
|
|
|
2019-07-25 16:41:09 +02:00
|
|
|
func (r *rule) appliesToDirectDeps(ctx BottomUpMutatorContext) bool {
|
|
|
|
if len(r.directDeps) == 0 {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
matches := false
|
|
|
|
ctx.VisitDirectDeps(func(m Module) {
|
|
|
|
if !matches {
|
|
|
|
name := ctx.OtherModuleName(m)
|
|
|
|
matches = r.directDeps[name]
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
return matches
|
|
|
|
}
|
|
|
|
|
2019-07-26 11:48:06 +02:00
|
|
|
func (r *rule) appliesToOsClass(osClass OsClass) bool {
|
|
|
|
if len(r.osClasses) == 0 {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, c := range r.osClasses {
|
|
|
|
if c == osClass {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2019-03-06 00:06:16 +01:00
|
|
|
func (r *rule) appliesToModuleType(moduleType string) bool {
|
|
|
|
return (len(r.moduleTypes) == 0 || InList(moduleType, r.moduleTypes)) && !InList(moduleType, r.unlessModuleTypes)
|
|
|
|
}
|
|
|
|
|
2021-12-23 16:05:38 +01:00
|
|
|
func (r *rule) appliesToProperties(properties []interface{}) bool {
|
|
|
|
includeProps := hasAllProperties(properties, r.props)
|
|
|
|
excludeProps := hasAnyProperty(properties, r.unlessProps)
|
2017-12-06 23:18:35 +01:00
|
|
|
return includeProps && !excludeProps
|
|
|
|
}
|
|
|
|
|
2019-07-22 13:13:55 +02:00
|
|
|
func StartsWith(prefix string) ValueMatcher {
|
|
|
|
return &startsWithMatcher{prefix}
|
|
|
|
}
|
|
|
|
|
2020-04-09 15:18:21 +02:00
|
|
|
func Regexp(re string) ValueMatcher {
|
|
|
|
r, err := regexp.Compile(re)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return ®exMatcher{r}
|
|
|
|
}
|
|
|
|
|
2020-06-05 22:14:03 +02:00
|
|
|
func NotInList(allowed []string) ValueMatcher {
|
|
|
|
return ¬InListMatcher{allowed}
|
|
|
|
}
|
|
|
|
|
2017-12-06 23:18:35 +01:00
|
|
|
// assorted utils
|
|
|
|
|
|
|
|
func cleanPaths(paths []string) []string {
|
|
|
|
res := make([]string, len(paths))
|
|
|
|
for i, v := range paths {
|
|
|
|
res[i] = filepath.Clean(v) + "/"
|
|
|
|
}
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
|
|
|
func fieldNamesForProperties(propertyNames string) []string {
|
|
|
|
names := strings.Split(propertyNames, ".")
|
|
|
|
for i, v := range names {
|
|
|
|
names[i] = proptools.FieldNameForProperty(v)
|
|
|
|
}
|
|
|
|
return names
|
|
|
|
}
|
|
|
|
|
2021-12-23 16:05:38 +01:00
|
|
|
func hasAnyProperty(properties []interface{}, props []ruleProperty) bool {
|
2017-12-06 23:18:35 +01:00
|
|
|
for _, v := range props {
|
2021-12-23 16:05:38 +01:00
|
|
|
if hasProperty(properties, v) {
|
2017-12-06 23:18:35 +01:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2021-12-23 16:05:38 +01:00
|
|
|
func hasAllProperties(properties []interface{}, props []ruleProperty) bool {
|
2017-12-06 23:18:35 +01:00
|
|
|
for _, v := range props {
|
2021-12-23 16:05:38 +01:00
|
|
|
if !hasProperty(properties, v) {
|
2017-12-06 23:18:35 +01:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2021-12-23 16:05:38 +01:00
|
|
|
func hasProperty(properties []interface{}, prop ruleProperty) bool {
|
2017-12-06 23:18:35 +01:00
|
|
|
for _, propertyStruct := range properties {
|
|
|
|
propertiesValue := reflect.ValueOf(propertyStruct).Elem()
|
|
|
|
for _, v := range prop.fields {
|
|
|
|
if !propertiesValue.IsValid() {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
propertiesValue = propertiesValue.FieldByName(v)
|
|
|
|
}
|
|
|
|
if !propertiesValue.IsValid() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2019-07-12 15:12:49 +02:00
|
|
|
check := func(value string) bool {
|
2021-12-23 16:05:38 +01:00
|
|
|
return prop.matcher.Test(value)
|
2017-12-06 23:18:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if matchValue(propertiesValue, check) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func matchValue(value reflect.Value, check func(string) bool) bool {
|
|
|
|
if !value.IsValid() {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if value.Kind() == reflect.Ptr {
|
|
|
|
if value.IsNil() {
|
|
|
|
return check("")
|
|
|
|
}
|
|
|
|
value = value.Elem()
|
|
|
|
}
|
|
|
|
|
|
|
|
switch value.Kind() {
|
|
|
|
case reflect.String:
|
|
|
|
return check(value.String())
|
|
|
|
case reflect.Bool:
|
|
|
|
return check(strconv.FormatBool(value.Bool()))
|
|
|
|
case reflect.Int:
|
|
|
|
return check(strconv.FormatInt(value.Int(), 10))
|
|
|
|
case reflect.Slice:
|
|
|
|
slice, ok := value.Interface().([]string)
|
|
|
|
if !ok {
|
|
|
|
panic("Can only handle slice of string")
|
|
|
|
}
|
|
|
|
for _, v := range slice {
|
|
|
|
if check(v) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
panic("Can't handle type: " + value.Kind().String())
|
|
|
|
}
|
2019-08-07 16:31:07 +02:00
|
|
|
|
|
|
|
var neverallowRulesKey = NewOnceKey("neverallowRules")
|
|
|
|
|
|
|
|
func neverallowRules(config Config) []Rule {
|
|
|
|
return config.Once(neverallowRulesKey, func() interface{} {
|
|
|
|
// No test rules were set by setTestNeverallowRules, use the global rules
|
|
|
|
return neverallows
|
|
|
|
}).([]Rule)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Overrides the default neverallow rules for the supplied config.
|
|
|
|
//
|
|
|
|
// For testing only.
|
2021-03-31 00:07:52 +02:00
|
|
|
func setTestNeverallowRules(config Config, testRules []Rule) {
|
2019-08-07 16:31:07 +02:00
|
|
|
config.Once(neverallowRulesKey, func() interface{} { return testRules })
|
|
|
|
}
|
2021-03-31 00:07:52 +02:00
|
|
|
|
|
|
|
// Prepares for a test by setting neverallow rules and enabling the mutator.
|
|
|
|
//
|
|
|
|
// If the supplied rules are nil then the default rules are used.
|
|
|
|
func PrepareForTestWithNeverallowRules(testRules []Rule) FixturePreparer {
|
|
|
|
return GroupFixturePreparers(
|
|
|
|
FixtureModifyConfig(func(config Config) {
|
|
|
|
if testRules != nil {
|
|
|
|
setTestNeverallowRules(config, testRules)
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
FixtureRegisterWithContext(func(ctx RegistrationContext) {
|
|
|
|
ctx.PostDepsMutators(registerNeverallowMutator)
|
|
|
|
}),
|
|
|
|
)
|
|
|
|
}
|