Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/app/concerns/mdm/workspace/boundary_range.rb
Views: 11784
module Mdm::Workspace::BoundaryRange1extend ActiveSupport::Concern23included do4#5# Validations6#78validate :boundary_must_be_ip_range910#11# Instance Methods12#1314# If {#limit_to_network} is disabled, this will always return `true`.15# Otherwise, return `true` only if all of the given IPs are within the16# project {#boundary boundaries}.1718#19# @param ips [String] IP range(s)20# @return [true] if actions on ips are allowed.21# @return [false] if actions are not allowed on ips.22def allow_actions_on?(ips)23return true unless limit_to_network24return true unless boundary25return true if boundary.empty?26boundaries = Shellwords.split(boundary)27return true if boundaries.empty? # It's okay if there is no boundary range after all28given_range = Rex::Socket::RangeWalker.new(ips)29return false unless given_range # Can't do things to nonexistant IPs30allowed = false31boundaries.each do |boundary_range|32ok_range = Rex::Socket::RangeWalker.new(boundary)33allowed = true if ok_range.include_range? given_range34end35return allowed36end3738# Validates that {#boundary} is {#valid_ip_or_range? a valid IP address or39# IP address range}. Due to this not being tested before it was moved here40# from Mdm, the default workspace does not validate. We always validate boundaries41# and a workspace may have a blank default boundary.42#43# @return [void]44def boundary_must_be_ip_range45unless boundary.blank?46begin47boundaries = Shellwords.split(boundary)48rescue ArgumentError49boundaries = []50end5152boundaries.each do |range|53unless valid_ip_or_range?(range)54errors.add(:boundary, "must be a valid IP range")55end56end57end58end5960# Returns an array of addresses ranges61#62# @return [Array<String>]63def addresses64(boundary || "").split("\n")65end6667private6869# Returns whether `string` is a valid IP address or IP address range.70#71# @return [true] if valid IP address or IP address range.72# @return [false] otherwise.73def valid_ip_or_range?(string)74range = Rex::Socket::RangeWalker.new(string)75range && range.ranges && range.ranges.any?76end7778end798081end828384