Skip to content

Instantly share code, notes, and snippets.

@jonatas
Created Mar 31, 2021
Embed
What would you like to do?
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