Decode a signed cookie in rails? - ruby-on-rails

So I have a signed cookie that has the following value
IjVvVGdIOW1pUU44Qkk5NFZZUl9Udnci--a3c3b748fd207ba1c537b590dd458b4855677146
I need to decode it and get the following value
5oTgH9miQN8BI94VYR_Tvw
I tried something like
Base64.decode64(cookie_value.split('--').first)
but it gives me the wrong value, it adds these damn slashes in the string so I end up with
"\"5oTgH9miQN8BI94VYR_Tvw\""

but it gives me the wrong value, it adds these damn slashes in the
string so I end up with
"\"5oTgH9miQN8BI94VYR_Tvw\""
Its not adding any slashes. The issue here is your returned string is included between double quotes ". \" here is escape character.
Here:
Base64.decode64 "IjVvVGdIOW1pUU44Qkk5NFZZUl9Udnci"
# => "\"5oTgH9miQN8BI94VYR_Tvw\""
puts Base64.decode64 "IjVvVGdIOW1pUU44Qkk5NFZZUl9Udnci"
# "5oTgH9miQN8BI94VYR_Tvw"
As the problem is unwanted "s. You can remove them as follows:
Base64.decode64(cookie_value.split('--').first).chomp('"').reverse.chomp('"').reverse
# => "5oTgH9miQN8BI94VYR_Tvw"

Please try this
require 'rack'
puts Rack::Session::Cookie::Base64::Marshal.new.decode("IjVvVGdIOW1pUU44Qkk5NFZZUl9Udnci")
Also you can decrypt it.
Marshal.load(ActiveSupport::Base64.decode64(the_cookie_value.split("--").first)

I ended up using the following:
MultiJson.load(Base64.decode64(cookie_value.split('--').first))
probably works only with rails 4.1 +, although I am not sure

Related

How to get the hostname from a url with accented letters inside in Ruby

I have the following url inside a field of model:
https://www.reddit.com/r/italy/comments/i6ix3x/trenitalia_sostiene_che_potrà_non_rispettare_il/?sort=new
Inside the URL there is an accented letter (à). If I use URI.parse to get hostname gives me the following error:
URI::InvalidURIError: URI must be ascii only "https://www.reddit.com/r/italy/comments/i6ix3x/trenitalia_sostiene_che_potr\u00E0_non_rispettare_il/?sort=new"
The method URL.encode resolves the problem, but I discover that the URL.encode is obsolete and should not be used.
Which method should I use for replacing URI.encode?
this is encoding issue and you need to do it as below
first lets encode your URI first
encoded_url = URI.encode('https://www.reddit.com/r/italy/comments/i6ix3x/trenitalia_sostiene_che_potrà_non_rispettare_il/?sort=new')
And then parse it
URI.parse(encoded_url)
good luck
The only solution that I find uses the gem Addressable(https://github.com/sporkmonger/addressable):
Addressable::URI.parse('https://www.reddit.com/r/italy/comments/i6ix3x/trenitalia_sostiene_che_potrà_non_rispettare_il/?sort=new').host
Perhaps this could be an inelegant solution:
URI.parse(URI.extract(target.url).first)
# => #<URI::HTTPS https://www.reddit.com/r/italy/comments/i6ix3x/trenitalia_sostiene_che_potr>
Then I use the method host
URI.parse(URI.extract(target.url).first).host
# => "www.reddit.com"

Remove SubString from String base on two strings or parameters in Ruby

Say I have the following string:
<p>Apple.</p><remove>Lettuce.</remove><span>Orange.</span>
I would like the output to be:
<p>Apple.</p><span>Orange.</span>
So I am trying to build a method like this:
def remove_this_block('<remove','/remove>')
# some code here
end
I've tried gsub, strip_tags, etc... Nothing seems to work.
Please help.
String#gsub would do:
"<p>Apple.</p><remove>Lettuce.</remove><span>Orange.</span>".
gsub /<remove.*?\/remove>/, ''
#⇒ "<p>Apple.</p><span>Orange.</span>"

Ruby on Rails 4 on Heroku, Environment variables with backslash escape

I have an environment variable along the lines of:
MY_VALUE: "EFINbA\u003d\u003d\n"
When I read it through ruby it is returned as:
ENV['MY_VALUE']
=> "EFINbA\\u003d\\u003d\\n"
... but only on Heroku, not on Mac (where it was set through a local_env.yml file, admittedly)
So first of all, I just don't understand why it is doing that.
Secondly, when I attempt to remove the \ and replace them with \, I have found nothing that works.
Although:
ENV['MY_VALUE'].gsub("\","x")
=> "EFINbAxu003dxu003dxn"
This:
ENV['MY_VALUE'].gsub("\","\")
... doesn't work because the last double-quote is escaped, while:
ENV['MY_VALUE'].gsub("\\","\\")
... effectively does nothing at all.
Evidently I am missing something basic here, and it's too late in the day for me to spot it.
Thanks.
You can try YAML's unescape
require 'yaml'
def unescape(s)
YAML.load(%Q(---\n"#{s}"\n))
end
unescape(ENV['MY_VALUE'])
or if you don't bring in the yaml module you can use eval
def unescape(s)
eval %Q{"#{s}"}
end
The advantage to YAML over eval is that it is presumably safer.
YAML.safe_load sometimes changes characters in the env string - so it is not an optimal solution.
As eval is also not the solution as it is not that safe, .undump was the answer I was looking for.

Avoid using # encoding: UTF-8

I ran into a problem with a Rails controller where it choked on a Unicode string:
syntax error, unexpected $end, expecting ']'
...conditions => ['url like ?', "%日本%"])
The solution to this problem was to set the encoding at the top of the controller file using
# encoding: UTF-8
Is there any way to set this globally? I keep on getting into trouble by forgetting to set it in files. Alternatively, is there a default somewhere that will make sure that all strings are thought of as Unicode? Are there any problems with setting everything to be Unicode?
In less than a month, Ruby 2.0 will be released, which will have UTF-8 as the default encoding. Then, you will not need to do that any more.
You can try setting environment variable RUBYOPT to -Ku value:
export RUBYOPT="-Ku"

How can I output an "&" in RoR without getting "&"?

Controller code:
class HelpController<ApplicationController
def index
#url = "https://example.com/auth?user_id=1234&redirect_to=http://google.ru"
end
end
View code:
<script>location.href='<%=#url%>';</script>
And it redirects to THIS:
example.com/auth?user_id=1234&redirect_to=http://google.ru
This:
http://example.com/auth?user_id=1234 & amp; redirect_to=http://google.ru
(without spaces)
In Rails 3, you can call the .html_safe method to tell rails that you have verified the content is safe to send unescaped.
See http://yehudakatz.com/2010/02/01/safebuffers-and-rails-3-0/ for an explanation of the motivation for the default escaping behavior.
The idiom is reversed from older versions of rails, where you had to explicitly call .h (.html_escape).
No Ruby expert, but I think escaping is the default behavior. You have to force it to output as an unescaped string by wrapping the thing in raw().

Resources