Skip to content

Instantly share code, notes, and snippets.

@jonatas
Created March 31, 2021 00:18
Show Gist options
  • Save jonatas/369a38e68ff6a4db2fd1b4bda5a261ed to your computer and use it in GitHub Desktop.
Save jonatas/369a38e68ff6a4db2fd1b4bda5a261ed to your computer and use it in GitHub Desktop.
CREATE OR REPLACE FUNCTION classify(anyelement)
RETURNS text[] AS $$
SELECT ARRAY_REMOVE(ARRAY[
CASE WHEN $1 ~ '[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+[.][A-Za-z]+' THEN 'email' END,
CASE WHEN $1 ~ '(https?://|www\\.)' THEN 'link' END
], NULL)
$$
LANGUAGE SQL;
class ObfuscationChecker
class << self
SCANNERS = {
email: /\w+@\w+/, url: /https?:\/\//
}
def scan_all!
target_models_and_columns.each do |model_name, columns|
columns.each do |column|
SCANNERS.each do |label, regex|
results = model_name.constantize.pluck(column).grep(regex)
if results.any?
puts "#{model_name}##{column} has #{label}, example: #{results.first}"
end
end
end
end
end
def target_models_and_columns
load_models! if ApplicationRecord.descendants.empty?
ApplicationRecord.descendants.map do |clazz|
[clazz.name, clazz.columns.select{|c| c.type == :text}.map(&:name)]
end
end
def load_models!
Dir['app/models/*.rb'].each(&method(:load))
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment