Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update AST.pm #47

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 38 additions & 22 deletions lib/Zarn/AST.pm
Original file line number Diff line number Diff line change
Expand Up @@ -16,57 +16,71 @@ package Zarn::AST {
"file=s" => \$file,
"rules=s" => \$rules
);



if ($file && $rules) {
my $document = PPI::Document -> new($file);
my $document = PPI::Document -> new($file);

$document -> prune("PPI::Token::Pod");
$document -> prune("PPI::Token::Comment");
$document -> prune("PPI::Token::Pod");
$document -> prune("PPI::Token::Comment");

foreach my $token (@{$document -> find("PPI::Token")}) {
foreach my $rule (@{$rules}) {
#匹配规则各个字段
my @sample = $rule -> {sample} -> @*;
my $category = $rule -> {category};
my $title = $rule -> {name};
my $message = $rule -> {message};

if (grep {my $content = $_; scalar(grep {$content =~ m/$_/xms} @sample)} $token -> content()) {
if (grep {my $content = $_; scalar(grep {$content =~ m/$_/xms} @sample)} $token -> content())
{
my $next_element = $token -> snext_sibling;

# this is a draft source-to-sink function
my $threshold_location;
$threshold_location = $token->location->[0];
if (defined $next_element && ref $next_element && $next_element -> content() =~ /[\$\@\%](\w+)/xms) {
# perform taint analyis
my $var_token = $document -> find_first (
sub {
$_[1] -> isa("PPI::Token::Symbol") and
($_[1] ->content eq "\$$1" or $_[1] -> content eq "\@$1" or $_[1] -> content eq "\%$1")
my $matching_nodes = $document->find(
sub { $_[1] -> isa("PPI::Token::Symbol") and
($_[1] ->content eq "\$$1" or $_[1] -> content eq "\@$1" or $_[1] -> content eq "\%$1")
}
);

if ($var_token && $var_token -> can("parent")) {
my @childrens = $var_token -> parent -> children;

my $pre_node;
#search lastest variable
foreach my $node (@$matching_nodes) {
if($node && $node->location->[0] < $threshold_location)
{
$pre_node = $node;
}
else
{
last;
}
}
if ($pre_node && $pre_node -> can("parent"))
{
my @childrens = $pre_node -> parent -> children;

#grep{code block} lists : for every object,apply for code block

if (grep { # verifyng if the variable is a fixed string or a number
$_ -> isa("PPI::Token::Quote::Double") ||
$_ -> isa("PPI::Token::Quote::Single") ||
$_ -> isa("PPI::Token::Number")
} @childrens) {
next;
}

if ((
$var_token -> parent -> isa("PPI::Token::Operator") ||
$var_token -> parent -> isa("PPI::Statement::Expression")
)) {

if (($pre_node -> parent -> isa("PPI::Token::Operator") ||$pre_node -> parent -> isa("PPI::Statement::Expression")))
{
my ($line_sink, $rowchar_sink) = @{$token -> location};
my ($line_source, $rowchar_source) = @{$var_token -> location};
my ($line_source, $rowchar_source) = @{$pre_node -> location};

push @results, {
category => $category,
file => $file,
title => $title,
message => $message,
line_sink => $line_sink,
line_sink => $line_sink,
rowchar_sink => $rowchar_sink,
line_source => $line_source,
rowchar_source => $rowchar_source
Expand All @@ -83,6 +97,8 @@ package Zarn::AST {

return 0;
}


}

1;