use super::super::{CertRule, RuleViolation};
use crate::manifest::{RuleCategory, Severity};
use crate::utility::cert_c::ast_utils::get_node_text;
use tree_sitter::Node;
#[derive(Debug)]
pub struct Fio06C;
impl Fio06C {
#[allow(dead_code)]
pub fn new() -> Self {
Fio06C
}
fn is_file_creating_mode(&self, mode: &str) -> bool {
mode.contains('w') || mode.contains('a') || mode.contains('+')
}
fn check_fopen_call(&self, node: &Node, source: &str, violations: &mut Vec<RuleViolation>) {
if node.kind() != "call_expression" {
return;
}
if let Some(function) = node.child_by_field_name("function") {
let func_name = get_node_text(&function, source);
if func_name != "fopen" {
return;
}
} else {
return;
}
if let Some(arguments) = node.child_by_field_name("arguments") {
let mut cursor = arguments.walk();
let mut mode_arg = None;
for child in arguments.children(&mut cursor) {
if child.kind() == "string_literal" || child.kind() == "identifier" {
if mode_arg.is_none() {
mode_arg = Some(child);
} else {
let mode_text = get_node_text(&child, source);
if self.is_file_creating_mode(mode_text) {
violations.push(RuleViolation {
rule_id: "FIO06-C".to_string(),
severity: Severity::Medium,
line: node.start_position().row + 1,
column: node.start_position().column + 1,
message: "fopen() cannot explicitly specify file permissions; use open() for security-sensitive files".to_string(),
file_path: String::new(),
suggestion: Some(
"Use open(filename, O_CREAT | O_WRONLY, mode) to explicitly specify file permissions".to_string(),
),
requires_manual_review: Some(false),
});
}
break;
}
}
}
}
}
fn has_o_creat_flag(&self, flags_node: &Node, source: &str) -> bool {
let flags_text = get_node_text(flags_node, source);
flags_text.contains("O_CREAT")
}
fn count_open_arguments(&self, arguments: &Node) -> usize {
let mut count = 0;
let mut cursor = arguments.walk();
for child in arguments.children(&mut cursor) {
if child.kind() == "(" || child.kind() == ")" || child.kind() == "," {
continue;
}
count += 1;
}
count
}
fn check_open_call(&self, node: &Node, source: &str, violations: &mut Vec<RuleViolation>) {
if node.kind() != "call_expression" {
return;
}
if let Some(function) = node.child_by_field_name("function") {
let func_name = get_node_text(&function, source);
if func_name != "open" {
return;
}
} else {
return;
}
if let Some(arguments) = node.child_by_field_name("arguments") {
let arg_count = self.count_open_arguments(&arguments);
if arg_count == 2 {
let mut cursor = arguments.walk();
let mut args_found = 0;
for child in arguments.children(&mut cursor) {
if child.kind() == "(" || child.kind() == ")" || child.kind() == "," {
continue;
}
args_found += 1;
if args_found == 2 {
if self.has_o_creat_flag(&child, source) {
violations.push(RuleViolation {
rule_id: "FIO06-C".to_string(),
severity: Severity::High,
line: node.start_position().row + 1,
column: node.start_position().column + 1,
message: "open() called with O_CREAT but missing mode argument".to_string(),
file_path: String::new(),
suggestion: Some(
"Add a third argument to specify file permissions, e.g., open(filename, O_CREAT | flags, 0600)".to_string(),
),
requires_manual_review: Some(false),
});
}
break;
}
}
}
}
}
fn traverse(&self, node: &Node, source: &str, violations: &mut Vec<RuleViolation>) {
if node.kind() == "call_expression" {
self.check_fopen_call(node, source, violations);
self.check_open_call(node, source, violations);
}
let mut cursor = node.walk();
for child in node.children(&mut cursor) {
self.traverse(&child, source, violations);
}
}
}
impl CertRule for Fio06C {
fn rule_id(&self) -> &'static str {
"FIO06-C"
}
fn description(&self) -> &'static str {
"Create files with appropriate access permissions"
}
fn category(&self) -> RuleCategory {
RuleCategory::Rule
}
fn severity(&self) -> Severity {
Severity::Medium
}
fn cert_id(&self) -> &'static str {
"FIO06-C"
}
fn check(&self, root: &Node, source: &str) -> Vec<RuleViolation> {
let mut violations = Vec::new();
self.traverse(root, source, &mut violations);
violations
}
}