How to cgi escape ruby credentials - ruby

I am running the command bundle install and keep getting the following error
Please CGI escape your usernames and passwords before setting them for authentication.
I am unsure how I could go about CGI escaping my credentials- any ideas? Thanks

You can do this in irb with Ruby's CGI::Util module:
$ irb
irb(main):001:0> require "cgi"
=> true
irb(main):002:0> CGI.escape "foo#example.com"
=> "foo%40example.com"

Related

Ruby unescape HTML string

Any idea how I can unescape the following string in Ruby?
C:\inetpub\wwwroot\adminWeb
to
C:\inetpub\wwwroot\adminWeb
or to
C%3A%5Cinetpub%5Cwwwroot%5CadminWeb
Tried with URI.decode with no success.
The CGI library is one option:
require 'cgi'
CGI.unescapeHTML('C:\inetpub\wwwroot\adminWeb')
# => "C:\\inetpub\\wwwroot\\adminWeb"
One more variant is HTMLEntities
HTMLEntities.new.decode "C:\inetpub\wwwroot\adminWeb"
# => "C:\\inetpub\\wwwroot\\adminWeb"
I prefer to use it because it deals with rare cases aså and — which CGI.unescapeHTML does not
An alternative is using the standard lib's URI module:
require 'uri'
URI.unescape "C%3A%5Cinetpub%5Cwwwroot%5CadminWeb" # => "C:\\inetpub\\wwwroot\\adminWeb"

awesome_print not printing in glorious color multiline layout?

I'm experiencing an issue wherein awesome_print is not displaying output in it's gorgeous colorized multiline format. What I find most curious is that while the gem is installed:
$ gem install awesome_print
Successfully installed awesome_print-1.6.1
1 gem installed
It returns a false upon require in IRB:
>> require 'awesome_print'
false
Any idea as to what may be causing this? I am not quite sure how to tackle this since gem installation seems to work fine and I can even use ap "test" in IRB with no error, except there is no colorization or proper printing with multiple lines and seems to simply fall back to some other method for printing.
No ~/.aprc changes evoke any changes either.
Pass the options ap object, options = {:plain => false, :multiline => true} or you can add it to the config file.
create an ~/.irbc file with the following content
require "awesome_print"
AwesomePrint.irb!
:multiline => true, # Display in multiple lines.
:plain => false
I had the same error,although require was returning false but awesome print was working, try to print something using awesome_print(ap), like
ap data = {foo: "bar"}

Avoid executing system command with Ruby secure open

In terminal, typing:
> irb
> require('open-uri')
> open("| curl http://www.haosou.com").read
can execute a system command. How can I avoid this?
Executing this kind of command is a serious security issue.
You can use a regex to validate the format:
/^(http|https):\/\/[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(:[0-9]{1,5})?(\/.*)?$/ix
The validation can be done in a model:
validates_format_of :url, :with => /^(http|https):\/\/[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(:[0-9]{1,5})?(\/.*)?$/ix
Or elsewhere:
if url =~ /^(http|https):\/\/[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(:[0-9]{1,5})?(\/.*)?$/ix
require('open-uri')
open(url).read
end

Ruby error Webrick or CGI?

I have using Webrick + CGI and when I instantiate, returns an error: (offline mode: enter name=value pairs on standard input)
irb(main):001:0> require 'cgi'
=> true
irb(main):002:0> cgi = CGI.new
(offline mode: enter name=value pairs on standard input)
Nope, not an error. That's the way it works.
From the ruby-docs CGI documentation
If the CGI object is not created in a standard CGI call environment (that is, it can’t locate REQUEST_METHOD in its environment), then it will run in “offline” mode. In this mode, it reads its parameters from the command line or (failing that) from standard input
In the irb console, after the (offline mode: enter name=value pairs on standard input) message, the console is waiting for you to enter the values. Enter key value pairs followed by Ctrld to finish entering data.
irb(main):001:0> require 'cgi'
=> true
irb(main):002:0> cgi = CGI.new
(offline mode: enter name=value pairs on standard input)
name=Prakash
number=432
Ctrld
=> #<CGI:0x007fa4eb2abd30 #options={:accept_charset=>"UTF-8"}, #accept_charset="UTF-8", #multipart=false, #params={"name"=>["Prakash"], "number"=>["432"]}, #cookies={}, #output_cookies=nil, #output_hidden=nil>
irb(main):003:0>
Refer to CGI Programming Documentation on PLEAC-Ruby for further code examples of working with CGI in ruby.

Do ruby and irb use different module search paths?

I have a Ruby script that is trying to require the restclient module. When I reduce it down to just this one line, it still fails:
#!/usr/bin/env ruby
require 'restclient'
When I run it, I get the following error:
./test.rb:3:in `require': no such file to load -- restclient (LoadError)
from ./test2.rb:3
When I run irb, the module loads fine:
$ irb
>> require "restclient"
=> true
>>
As far as I can tell, it looks like both the script and irb have the same module paths:
$ ruby -e "puts $:"
/Library/Ruby/Site/1.8
/Library/Ruby/Site/1.8/powerpc-darwin10.0
/Library/Ruby/Site/1.8/universal-darwin10.0
/Library/Ruby/Site
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/vendor_ruby/1.8
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/vendor_ruby/1.8/universal-darwin10.0
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/vendor_ruby
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/powerpc-darwin10.0
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/universal-darwin10.0
.
$ irb
>> puts $:
/Library/Ruby/Site/1.8
/Library/Ruby/Site/1.8/powerpc-darwin10.0
/Library/Ruby/Site/1.8/universal-darwin10.0
/Library/Ruby/Site
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/vendor_ruby/1.8
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/vendor_ruby/1.8/universal-darwin10.0
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/vendor_ruby
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/powerpc-darwin10.0
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/universal-darwin10.0
.
=> nil
>>
What would cause a module to load through irb, but not when run directly through Ruby?
One other confusing detail is that the restclient gem doesn't seem to be in my path to start with. How is irb finding it?
$ locate restclient | grep gems
/Library/Ruby/Gems/1.8/gems/rest-client-1.6.1/bin/restclient
/Library/Ruby/Gems/1.8/gems/rest-client-1.6.1/lib/restclient
/Library/Ruby/Gems/1.8/gems/rest-client-1.6.1/lib/restclient/abstract_response.rb
/Library/Ruby/Gems/1.8/gems/rest-client-1.6.1/lib/restclient/exceptions.rb
/Library/Ruby/Gems/1.8/gems/rest-client-1.6.1/lib/restclient/net_http_ext.rb
/Library/Ruby/Gems/1.8/gems/rest-client-1.6.1/lib/restclient/payload.rb
/Library/Ruby/Gems/1.8/gems/rest-client-1.6.1/lib/restclient/raw_response.rb
/Library/Ruby/Gems/1.8/gems/rest-client-1.6.1/lib/restclient/request.rb
/Library/Ruby/Gems/1.8/gems/rest-client-1.6.1/lib/restclient/resource.rb
/Library/Ruby/Gems/1.8/gems/rest-client-1.6.1/lib/restclient/response.rb
/Library/Ruby/Gems/1.8/gems/rest-client-1.6.1/lib/restclient.rb
/Library/Ruby/Gems/1.8/gems/rest-client-1.6.1/spec/restclient_spec.rb
Thanks - Marc
Try
require "rubygems"
in the source code file, or starting the ruby program with ruby -rubygems filename.rb.

Resources