Is there a way to find out if there is binary in an uploaded file or textarea in ruby?

Is there a way to prevent users from uploading files or entering binary data in the text area in the interface. I can't find a method in ruby ​​or javascript to check for binary in a user-made view on my website.

  var binary_count = 0;
  var isAscii = true;
  //binary check
  for (var i=0, len=submission.length; i<len; i++){
        if (submission[i] > 127){ 
          isAscii=false; 
          binary_count += 1;
          $('.error').html('Binary code is not allowed in submission.').show();
          break;
          }
    }

      

This method doesn't work in javascript and I also can't find a way to test it in rails. Can anyone guide me in this case?

+3


source to share


1 answer


You cannot validate this user view correctly in javascript because it is always possible to submit data without javascript validation. Therefore, you must check it in the rails. Your validation might be something like this (not validated).

class YourModel < ActiveRecord::Base
   validate :proper_string
   def proper_string
     errors.add(:submission, "Only text allowed") unless submission.force_encoding("UTF-8").valid_encoding? 
   end
end

      



With javascript you can only check for usability reasons, but it looks more like a hack attempt, but not user error.

+1


source







All Articles