ruby 无效的下一个编译错误

2hh7jdfx  于 2023-06-22  发布在  Ruby
关注(0)|答案(1)|浏览(161)

我有一个方法,扫描包含错误的网站的URL:

def begin_vulnerability_check
  info("Checking if sites are vulnerable.")
  IO.read("#{PATH}/temp/SQL_sites_to_check.txt").each_line do |parse|
    Timeout::timeout(10) do
      parsing = Nokogiri::HTML(RestClient.get("#{parse.chomp}"))
      info("Parsing page for SQL syntax error: #{parse.chomp}")
      if parsing.css('html')[0].to_s[/You have an error in your SQL syntax/]
        successful = parse
        success("URL: #{parse.chomp} returned SQL syntax error, dumped to SQL_VULN.txt")
        File.open("#{PATH}/lib/SQL_VULN.txt", "a+"){|s| s.puts(parse)}
        sleep(1)
      else
        err("URL: #{parse.chomp} returned and error, dumped to non_exploitable.txt")
        File.open("#{PATH}/lib/non_exploitable.txt", "a+"){|s| s.puts(parse)}
        sleep(1)
      end
    end
  end
end

在测试过程中,我扫描了以下URL列表:

http://www.bible.com/subcat.php?id=2'
http://www.cidko.com/pro_con.php?id=3'
http://www.slavsandtars.com/about.php?id=25'
http://www.police.gov/content.php?id=275'
http://www.icdprague.org/index.php?id=10'
http://huawei.com/en/plugin.php?id=hwdownload'
https://huawei.com/en/plugin.php?id=unlock'
https://facebook.com/profile.php?id'
http://www.footballclub.com.au/index.php?id=43'
http://www.mesrs.gouv/index.php?id=1525'

我还有一个救援块,它假设捕获异常Timeout::Error并移动到列表中的下一个URL:

begin
  begin_vulnerability_check
rescue Timeout::Error
   if Timeout::Error 
     warn("Page timed out, this is usually cause by the page returning a white page, or being non-existent, skipping.")
     next
  end
end

然而,当我试图运行这个程序时,我得到了以下错误:

whitewidow.rb:130: Invalid next
whitewidow.rb: compile error (SyntaxError)

第130行:

rescue Timeout::Error
   if Timeout::Error 
     warn("Page timed out, this is usually cause by the page returning a white page, or being non-existent, skipping.")
     next #<= HERE
  end
end

我的问题是,我是否在错误的意义上使用了next?在我看来,下一步应该是,如果这发生了,转到下一行,我这样想是不是错了?我如何重构它才能工作?

hs1rzwqc

hs1rzwqc1#

您可以使用next从块返回。你不能像你想的那样在街区外使用。
但是您甚至不需要next,因为当您挽救超时错误时,迭代将自动继续下一行。您只需将rescue移动到each_line迭代中。
你的代码应该是这样的:

def begin_vulnerability_check
  IO.read("#{PATH}/temp/SQL_sites_to_check.txt").each_line do |parse|
    begin
      Timeout::timeout(10) do
        ...
      end
    rescue Timeout::Error
      # Will automatically continue with next line after this
    end
  end
end

相关问题