Skip to content

Commit

Permalink
Add Chrome/Chromium cookie extractor. Same as the Firefox one, but wi…
Browse files Browse the repository at this point in the history
…th different table and field names. Will refactor them both to remove duplication.
  • Loading branch information
jdallien committed Feb 22, 2012
1 parent 628780e commit 64d118c
Show file tree
Hide file tree
Showing 2 changed files with 149 additions and 0 deletions.
43 changes: 43 additions & 0 deletions lib/cookie_extractor/chrome_cookie_extractor.rb
Original file line number Diff line number Diff line change
@@ -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
106 changes: 106 additions & 0 deletions spec/chrome_cookie_extractor_spec.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 64d118c

Please sign in to comment.