Add Chrome/Chromium cookie extractor. Same as the Firefox one, but with different table and field names. Will refactor them both to remove duplication.
This commit is contained in:
parent
628780e0db
commit
64d118cc85
2 changed files with 149 additions and 0 deletions
43
lib/cookie_extractor/chrome_cookie_extractor.rb
Normal file
43
lib/cookie_extractor/chrome_cookie_extractor.rb
Normal file
|
@ -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
spec/chrome_cookie_extractor_spec.rb
Normal file
106
spec/chrome_cookie_extractor_spec.rb
Normal file
|
@ -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
|
Loading…
Reference in a new issue