aboutsummaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/alaveteli_text_masker.rb21
-rw-r--r--lib/attachment_to_html/adapter.rb2
-rw-r--r--lib/mail_handler/backends/mail_backend.rb2
-rw-r--r--lib/normalize_string.rb23
4 files changed, 36 insertions, 12 deletions
diff --git a/lib/alaveteli_text_masker.rb b/lib/alaveteli_text_masker.rb
index 3c2bcf825..49dd15ae5 100644
--- a/lib/alaveteli_text_masker.rb
+++ b/lib/alaveteli_text_masker.rb
@@ -8,6 +8,21 @@ module AlaveteliTextMasker
'image/bmp',
'application/zip' ]
+ TextMask = [ 'text/css',
+ 'text/csv',
+ 'text/html',
+ 'text/plain',
+ 'text/rfc822-headers',
+ 'text/rtf',
+ 'text/tab-separated-values',
+ 'text/x-c',
+ 'text/x-diff',
+ 'text/x-fortran',
+ 'text/x-mail',
+ 'text/xml',
+ 'text/x-pascal',
+ 'text/x-vcard' ]
+
# Replaces all email addresses in (possibly binary) data
# Also applies custom masks and censor items
def apply_masks!(text, content_type, options = {})
@@ -19,7 +34,7 @@ module AlaveteliTextMasker
case content_type
when *DoNotBinaryMask
# do nothing
- when 'text/html'
+ when *TextMask
apply_text_masks!(text, options)
when 'application/pdf'
apply_pdf_masks!(text, options)
@@ -79,7 +94,7 @@ module AlaveteliTextMasker
# Replace text in place
def apply_binary_masks!(text, options = {})
# Keep original size, so can check haven't resized it
- orig_size = text.mb_chars.size
+ orig_size = text.size
# Replace ASCII email addresses...
text.gsub!(MySociety::Validate.email_find_regexp) do |email|
@@ -114,7 +129,7 @@ module AlaveteliTextMasker
# Replace censor items
censor_rules = options[:censor_rules] || []
censor_rules.each{ |censor_rule| censor_rule.apply_to_binary!(text) }
- raise "internal error in apply_binary_masks!" if text.mb_chars.size != orig_size
+ raise "internal error in apply_binary_masks!" if text.size != orig_size
return text
end
diff --git a/lib/attachment_to_html/adapter.rb b/lib/attachment_to_html/adapter.rb
index 058fb2a01..ac8a16411 100644
--- a/lib/attachment_to_html/adapter.rb
+++ b/lib/attachment_to_html/adapter.rb
@@ -61,7 +61,7 @@ module AttachmentToHTML
end
def attachment_body
- @attachment_body ||= attachment.body
+ @attachment_body ||= attachment.default_body
end
end
end
diff --git a/lib/mail_handler/backends/mail_backend.rb b/lib/mail_handler/backends/mail_backend.rb
index 34fbc91ab..19f502275 100644
--- a/lib/mail_handler/backends/mail_backend.rb
+++ b/lib/mail_handler/backends/mail_backend.rb
@@ -68,7 +68,7 @@ module MailHandler
part_file_name = part_file_name.nil? ? nil : part_file_name.dup
if part_file_name
part_file_name = CGI.unescape(part_file_name)
- part_file_name = convert_string_to_utf8(part_file_name, part.charset)
+ part_file_name = convert_string_to_utf8(part_file_name, part.charset).string
end
part_file_name
end
diff --git a/lib/normalize_string.rb b/lib/normalize_string.rb
index d850d7e05..69853fd6e 100644
--- a/lib/normalize_string.rb
+++ b/lib/normalize_string.rb
@@ -73,18 +73,27 @@ def convert_string_to_utf8_or_binary(s, suggested_character_encoding=nil)
result
end
+class StringConversionResult < Struct.new(:string, :scrubbed)
+ alias_method :scrubbed?, :scrubbed
+end
+
def convert_string_to_utf8(s, suggested_character_encoding=nil)
begin
result = normalize_string_to_utf8 s, suggested_character_encoding
+ StringConversionResult.new(result, false)
rescue EncodingNormalizationError
- result = s
- if String.method_defined?(:encode)
- result = s.force_encoding("utf-8").encode("utf-8", :invalid => :replace,
- :undef => :replace,
- :replace => "")
- end
+ result = scrub(s)
+ StringConversionResult.new(result, true)
+ end
+end
+
+def scrub(string)
+ if String.method_defined?(:encode)
+ string = string.force_encoding("utf-8")
+ string.valid_encoding? ? string : string.encode("utf-16le", :invalid => :replace, :replace => "").encode("utf-8")
+ else
+ Iconv.conv('UTF-8//IGNORE', 'UTF-8', string)
end
- result
end
def log_text_details(message, text)