Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to find out presence of binary code in an uploaded file or text area in ruby?

Is there a way to stop users from uploading files or entering binary data in text area in front-end. I am unable to find a method in ruby or javascript to check the presence of binary code in the submission made by the user 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 does not work in javascript and I am also unable to find a way to check it in rails too. Can any one guide me in this case?

like image 259
Sahil Avatar asked Jan 21 '26 06:01

Sahil


1 Answers

You can't properly check this user submission in javascript, because there always are possible submit data without javascript checks. So you should validate it in rails. Your validation can be something like this (not tested).

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 validate only for usability reason, but there look more like hacking attempt, but not user mistake.

like image 168
dpa Avatar answered Jan 23 '26 19:01

dpa



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!