-
-
Save jonatas/7af006a3eddb630cae3b4e2aaba528f9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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