From 64d118cc850874b3822bdb513ae86334cf137ffc Mon Sep 17 00:00:00 2001 From: Jeff Dallien Date: Tue, 21 Feb 2012 19:22:56 -0500 Subject: [PATCH] Add Chrome/Chromium cookie extractor. Same as the Firefox one, but with different table and field names. Will refactor them both to remove duplication. --- .../chrome_cookie_extractor.rb | 43 +++++++ spec/chrome_cookie_extractor_spec.rb | 106 ++++++++++++++++++ 2 files changed, 149 insertions(+) create mode 100644 lib/cookie_extractor/chrome_cookie_extractor.rb create mode 100644 spec/chrome_cookie_extractor_spec.rb diff --git a/lib/cookie_extractor/chrome_cookie_extractor.rb b/lib/cookie_extractor/chrome_cookie_extractor.rb new file mode 100644 index 0000000..ba94331 --- /dev/null +++ b/lib/cookie_extractor/chrome_cookie_extractor.rb @@ -0,0 +1,43 @@ +require 'sqlite3' + +module CookieExtractor + class ChromeCookieExtractor + + def initialize(cookie_file) + @cookie_file = cookie_file + end + + def extract + db = SQLite3::Database.new @cookie_file + db.results_as_hash = true + @result = [] + db.execute("SELECT * FROM cookies") do |row| + @result << [ row['host_key'], + true_false_word(is_domain_wide(row['host_key'])), + row['path'], + true_false_word(row['secure']), + row['expires_utc'], + row['name'], + row['value'] + ].join("\t") + end + @result + end + + private + + def is_domain_wide(hostname) + hostname[0..0] == "." + end + + def true_false_word(value) + if value == "1" || value == 1 || value == true + "TRUE" + elsif value == "0" || value == 0 || value == false + "FALSE" + else + raise "Invalid value passed to true_false_word: #{value.inspect}" + end + end + end +end diff --git a/spec/chrome_cookie_extractor_spec.rb b/spec/chrome_cookie_extractor_spec.rb new file mode 100644 index 0000000..98c343f --- /dev/null +++ b/spec/chrome_cookie_extractor_spec.rb @@ -0,0 +1,106 @@ +require File.join(File.dirname(__FILE__), "spec_helper") + +describe CookieExtractor::ChromeCookieExtractor do + before :each do + @fake_cookie_db = double("cookie database", :results_as_hash= => true) + SQLite3::Database.should_receive(:new). + with('filename'). + and_return(@fake_cookie_db) + end + + describe "with a cookie that has a host starting with a dot" do + before :each do + @fake_cookie_db.should_receive(:execute).and_yield( + { 'host_key' => '.dallien.net', + 'path' => '/', + 'secure' => '0', + 'expires_utc' => '1234567890', + 'name' => 'NAME', + 'value' => 'VALUE'}) + @extractor = CookieExtractor::ChromeCookieExtractor.new('filename') + @result = @extractor.extract + end + + it "should return one cookie string" do + @result.size.should == 1 + end + + it "should put TRUE in the domain wide field" do + cookie_string = @result.first + cookie_string.split("\t")[1].should == "TRUE" + end + + it "should build the correct cookie string" do + cookie_string = @result.first + cookie_string.should == + ".dallien.net\tTRUE\t/\tFALSE\t1234567890\tNAME\tVALUE" + end + end + + describe "with a cookie that has a host not starting with a dot" do + before :each do + @fake_cookie_db.should_receive(:execute).and_yield( + { 'host_key' => 'jeff.dallien.net', + 'path' => '/path', + 'secure' => '1', + 'expires_utc' => '1234567890', + 'name' => 'NAME', + 'value' => 'VALUE'}) + @extractor = CookieExtractor::ChromeCookieExtractor.new('filename') + @result = @extractor.extract + end + + it "should return one cookie string" do + @result.size.should == 1 + end + + it "should put FALSE in the domain wide field" do + cookie_string = @result.first + cookie_string.split("\t")[1].should == "FALSE" + end + + it "should build the correct cookie string" do + cookie_string = @result.first + cookie_string.should == + "jeff.dallien.net\tFALSE\t/path\tTRUE\t1234567890\tNAME\tVALUE" + end + end + + describe "with a cookie that is not marked as secure" do + before :each do + @fake_cookie_db.should_receive(:execute).and_yield( + { 'host_key' => '.dallien.net', + 'path' => '/', + 'secure' => '0', + 'expires_utc' => '1234567890', + 'name' => 'NAME', + 'value' => 'VALUE'}) + @extractor = CookieExtractor::ChromeCookieExtractor.new('filename') + @result = @extractor.extract + end + + it "should put FALSE in the secure field" do + cookie_string = @result.first + cookie_string.split("\t")[3].should == "FALSE" + end + end + + describe "with a cookie that is marked as secure" do + before :each do + @fake_cookie_db.should_receive(:execute).and_yield( + { 'host_key' => '.dallien.net', + 'path' => '/', + 'secure' => '1', + 'expires_utc' => '1234567890', + 'name' => 'NAME', + 'value' => 'VALUE'}) + @extractor = CookieExtractor::ChromeCookieExtractor.new('filename') + @result = @extractor.extract + end + + it "should put TRUE in the secure field" do + cookie_string = @result.first + cookie_string.split("\t")[3].should == "TRUE" + end + end +end