OWASP / joomscan

OWASP Joomla Vulnerability Scanner Project https://www.secologist.com/
https://www.secologist.com/open-source-projects
GNU General Public License v3.0
1.06k stars 242 forks source link

improve code quality #17

Open StPanning opened 6 years ago

StPanning commented 6 years ago

The current code is written in a way that makes it hard to contribute:

To make the code easier to maintain I suggest that:

As a first step I would:

I would only change the code that needs to be changed to make the code work, to keep the extensive changes to a minimum.

after this is done, I would proceed with the rest of the code.

some example: modules/robots.pl orig:

dprint("Checking robots.txt existing");
$response=$ua->get("$target/robots.txt");
my $headers  = $response->headers();
my $content_type =$headers->content_type();
if ($response->status_line =~ /200/g and $content_type =~ /text\/plain/g) {
    $source=$response->decoded_content;
    my @lines = split /\n/, $source;
    $probot="";
    foreach my $line( @lines ) { 
        if($line =~ /llow:/g){
            $between=substr($line, index($line, ': ')+2, 99999);
            $probot.="$target$between\n";
        }
    }
    tprint("robots.txt is found\npath : $target/robots.txt \n\nInteresting path found from 
        robots.txt\n$probot");
}else{
     fprint("robots.txt is not found");
}

would become JoomScan/Check/RobotsTXT.pm:


package JoomScan::Check::RobotsTXT;
use warnings;
use strict;
use JoomScan::Logging qw(tprint dprint fprint)

sub check {
  my ($ua, $target) = @_;
  dprint("Checking robots.txt existing");
  my $response = $ua->get("$target/robots.txt");
  my $headers  = $response->headers();
  my $content_type =$headers->content_type();
  my $probot="";
  if ($response->status_line =~ /200/g and 
      $content_type =~ /text\/plain/g) {
    my $source = $response->decoded_content;
    my @lines = split /\n/, $source;
    foreach my $line( @lines ) { 
      if($line =~ /llow:/g){
        my $between=substr($line, index($line, ': ')+2, 99999);
        $probot.="$target$between\n";
      }
    }
    tprint("robots.txt is found\npath : $target/robots.txt \n\nInteresting path found from robots.txt\n$probot");
      }else{
    fprint("robots.txt is not found");
      }
}

1;

I think that code could still be improved, but I would leave that for the next round of refactoring

Ali-Razmjoo commented 6 years ago

Hello,

It's actually a great idea, if we want to focus on rewriting things, we may also write a cleaner code, with self-documentation. I believe it's a great idea to use perlpod too.

despite we must have a release for tomorrow #19, let's just fix simple issues for release 0.0.5 and apply this changes for 0.0.6 release.

@rezasp please review and let us know if you agree.

Best Regards.

rezasp commented 6 years ago

Hello,

Thank you for sharing us your great idea, I agree, I am working on a few enhancements for tomorrow, and let you know when I'm finished.

Regards.

StPanning commented 6 years ago

ok. Great. I will refactor the code. When I'm done I will send a PR. Since I have a day job, this may last some days

Ali-Razmjoo commented 6 years ago

Hey,

thanks for updating us. be in touch (Y).

regards.

StPanning commented 6 years ago

I have sent a PR. The change looks huge, but the code changes are little. all I did is:

I resisted the urge to fix/cleanup more. This way you can easier follow what I did.

Ali-Razmjoo commented 6 years ago

Hello @StPanning,

Thanks for your contribution, I will review your code after BH Arsenal Singapore. the details you shared is very useful.

Best Regards.