package lelu

import rego.v1

# Argument-aware rules. When REGO_POLICY_PATH is set, this file REPLACES the
# YAML rules for agent requests, so it has to carry its own default deny.

default authz := {"allowed": false, "reason": "default deny (rego)"}

# True when any string anywhere in args carries a destructive SQL keyword.
destructive_arg if {
	walk(input.args, [_, value])
	is_string(value)
	regex.match(`(?i)\b(drop|truncate|delete)\b`, value)
}

# True when the action reads a file that looks like secrets, whatever the action is called.
secret_path if {
	some key in ["path", "file"]
	regex.match(`(^|/)\.env($|\.)|id_rsa|credentials`, input.resource[key])
}

authz := {"allowed": false, "reason": "argument contains a destructive SQL keyword"} if {
	destructive_arg
} else := {"allowed": false, "requires_human_review": true, "reason": "resource path looks like a secrets file"} if {
	secret_path
} else := {"allowed": false, "requires_human_review": true, "reason": "outbound email needs human sign-off"} if {
	startswith(input.action, "send_")
} else := {"allowed": true, "reason": "read/create with clean arguments"} if {
	regex.match(`^(read|get|list|create)_`, input.action)
}
