[rule]
id = "java-authorized-annotation"
languages = ["java"]
category = "rbac"
confidence = "high"
description = "OpenMRS-style @Authorized({\"PRIV\"}) annotation"
query = """
[
(annotation
name: (identifier) @anno_name
arguments: (annotation_argument_list
(string_literal (string_fragment) @role_value)))
(annotation
name: (identifier) @anno_name
arguments: (annotation_argument_list
(element_value_array_initializer
(string_literal (string_fragment) @role_value))))
] @match
"""
[rule.predicates.anno_name]
eq = "Authorized"
[rule.rego_template]
template = """
default allow := false
allow if {
"{{role_value}}" in input.user.privileges
}
"""
[rule.cedar_template]
template = """
permit (
principal,
action,
resource
)
when {
principal.role == "{{role_value}}"
};
"""
[[rule.tests]]
input = """
public class UserService {
@Authorized("Manage Users")
public void delete(User u) { }
}
"""
expect_match = true
[[rule.tests]]
input = """
public class UserService {
@Authorized({"Manage Users", "Edit Users"})
public void update(User u) { }
}
"""
expect_match = true
[[rule.tests]]
input = """
public class UserService {
@Override
public void delete(User u) { }
}
"""
expect_match = false
[[rule.tests]]
input = """
public class UserService {
@Transactional
public void update(User u) { }
}
"""
expect_match = false