api: make IP addresses in the API.

Make the following fields visible in API responses:

* ip_bans.ip_addr
* ip_geolocations.ip_addr
* ip_geolocations.network
* users.last_ip_addr (mod only)
* user_sessions.ip_addr
* api_keys.last_ip_address
* api_keys.permitted_ip_addresses

Before IP addresses were globally hidden in API responses because IPs were
present in a lot of tables and we didn't want to accidentally leak them.
Now that we've gotten rid of IPs from most tables, it's safe to unhide them.
This commit is contained in:
evazion
2022-09-24 00:09:36 -05:00
parent 7bf824f0dd
commit adba70a0de
9 changed files with 34 additions and 32 deletions

View File

@@ -6,11 +6,11 @@
module Danbooru
class IpAddress
attr_reader :ip_address
delegate :ipv4?, :ipv6?, :loopback?, :link_local?, :unique_local?, :private?, :to_string, :prefix, :multicast?, :unspecified?, to: :ip_address
delegate :ipv4?, :ipv6?, :loopback?, :link_local?, :unique_local?, :private?, :to_string, :network, :prefix, :multicast?, :unspecified?, to: :ip_address
delegate :ip_info, :is_proxy?, to: :ip_lookup
def initialize(string)
@ip_address = ::IPAddress.parse(string.to_s)
@ip_address = ::IPAddress.parse(string.to_s.strip)
end
def ip_lookup
@@ -39,9 +39,13 @@ module Danbooru
ip_address.include?(other.ip_address)
end
def as_json
to_s
end
# "1.2.3.4/24" if the address is a subnet, "1.2.3.4" otherwise.
def to_s
ip_address.size > 1 ? ip_address.to_string : ip_address.to_s
ip_address.size > 1 ? "#{network}/#{prefix}" : ip_address.to_s
end
def inspect

View File

@@ -18,6 +18,8 @@ class IpAddressType < ActiveRecord::ConnectionAdapters::PostgreSQL::OID::Inet
def cast(value)
return nil if value.blank?
super(Danbooru::IpAddress.new(value))
rescue ArgumentError
nil
end
# Serialize a Danbooru::IpAddress to a String for the database.