rails_cve 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 7710d41cfcbd020a20e7e80c182d9ca3dfc1833d
4
- data.tar.gz: c2fd962891aa4517b740ffb40976385d28e5c485
3
+ metadata.gz: ee33e9208ca227aaa6caceead83033303a54fc79
4
+ data.tar.gz: c3631d55f3581dc8998df794fe81541b08796c26
5
5
  SHA512:
6
- metadata.gz: c9a4fe45b865081da707e6c6fc582ab87fec7540e53e76b8947c18af9bd45780881a4322b9e3a23a8bb4d0fcc360c7aa2fd372f9b89e319cbbe9dd0ad89592a5
7
- data.tar.gz: 557546b6e29aeaf568f33d434df27708b2fd4cf9b621bc61592af3dbbd6317b48dbd90c96cfdb9df90d2496761a22ceddcbffecb958b0c2b7c94b1699ff3a378
6
+ metadata.gz: 73903c5d2356ad202bdd45dbdb5c76ab879ee380e8255bd2dca97f97dab7e45c97b099a06a22143b52582fd44459e750745bb030d09717b653428ff2015cc2d4
7
+ data.tar.gz: 1dd6b542fb355d3a06d3b97051efc1ee9168c02dd7f4bf259719bcb9ea821912d71eb0f109e0fbabf86dcba1d5a9e17eaace7848fce185cdade3865dfdef0354
data/README.md CHANGED
@@ -9,51 +9,82 @@ tasks, notably searching the CVE descriptions:
9
9
  ```ruby
10
10
  # in `rails console`
11
11
 
12
- list = RailsCVE::Entry.search 'ruby'
13
- # SELECT "rails_cve_entries".* FROM "rails_cve_entries"
14
- # WHERE (to_tsvector('english', description) @@ to_tsquery('ruby'))
15
- # ORDER BY ts_rank(to_tsvector(description), plainto_tsquery('ruby')) DESC
12
+ list = RailsCVE::Entry.search_plain 'ruby gem'
13
+ #=> SELECT "rails_cve_entries".*
14
+ # FROM "rails_cve_entries"
15
+ # WHERE (to_tsvector('english', description) @@ plainto_tsquery('ruby gem'))
16
+ # ORDER BY "rails_cve_entries"."id" ASC
16
17
 
17
18
  list.count
18
- #=> 165
19
+ #=> 41
19
20
 
20
- list.last
21
- # #<RailsCVE::Entry
22
- # id: 29523,
23
- # name: "CVE-2007-6183",
24
- # descri ption: "Format string vulnerability in the mdiag_initialize...",
21
+ list.first
22
+ #=> #<RailsCVE::Entry
23
+ # id: 48583,
24
+ # name: "CVE-2011-0739",
25
+ # description: "The deliver function in the sendmail delivery agent...",
25
26
  # references: [
26
- # "BUGTRAQ:20071127 Ruby/Gnome2 0.16.0 Format String Vulnerability",
27
- # "URL:http://www.securityfocus.com/archive/1/archive/1/484240/100/0/threaded",
28
- # "MISC:http://em386.blogspot.com/2007/11/your-favorite-better-than-c-scripting.html",
29
- # "MISC:https://bugzilla.redhat.com/show_bug.cgi?id=402871",
30
- # …,
31
- # "XF:rubygnome2-mdiaginitialize-format-string(38757)",
32
- # "URL:http://xforce.iss.net/xforce/xfdb/38757"
27
+ # "MISC:https://github.com/mikel/mail/raw/master/patches/20110126_sendmail.patch",
28
+ # "CONFIRM:http://groups.google.com/group/mail-ruby/browse_thread/thread/e93bbd05706478dd?pli=1",
29
+ # "BID:46021",
30
+ # "URL:http://www.securityfocus.com/bid/46021",
31
+ # "OSVDB:70667",
32
+ # "URL:http://osvdb.org/70667",
33
+ # "SECUNIA:43077",
34
+ # "URL:http://secunia.com/advisories/43077",
35
+ # "VUPEN:ADV-2011-0233",
36
+ # "URL:http://www.vupen.com/english/advisories/2011/0233",
37
+ # "XF:ruby-mail-deliver-command-execution(65010)",
38
+ # "URL:http://xforce.iss.net/xforce/xfdb/65010"
33
39
  # ]>
40
+
41
+ list.first.reference_uris
42
+ #=> [
43
+ # "https://github.com/mikel/mail/raw/master/patches/20110126_sendmail.patch",
44
+ # "http://groups.google.com/group/mail-ruby/browse_thread/thread/e93bbd05706478dd?pli=1",
45
+ # "http://www.securityfocus.com/bid/46021",
46
+ # "http://osvdb.org/70667",
47
+ # "http://secunia.com/advisories/43077",
48
+ # "http://www.vupen.com/english/advisories/2011/0233",
49
+ # "http://xforce.iss.net/xforce/xfdb/65010"
50
+ # ]
34
51
  ```
35
52
 
36
53
  ## Full text search capabilities
37
54
 
38
- As you can see in the example above, we take advantage of PostgreSQL's text
39
- search features. Hence, you will need to migrate to PostgreSQL unless you
40
- haven't already.
41
55
 
42
- I should mention, that it is **strongly recommended** to change your schema
43
- format from Ruby to SQL, like so:
56
+ There exist two distinct search queries, which both are using Postgres'
57
+ advanced [text search capabilities][2]:
58
+
59
+ - `RailsCVE::Entry.search_plain(str)` and
60
+ - `RailsCVE::Entry.search_ranked(str)`
61
+
62
+ While `#search_plain` does not predefine an order on the result set (except
63
+ from ActiveRecord's default order), `#search_ranked` calculates a matching
64
+ rank and uses this as sort criteria.
65
+
66
+ Keep in mind that creating a ranked search result requires a little more
67
+ processing time and restricts the ActiveRecord query chaining possibilities:
44
68
 
45
69
  ```ruby
46
- # in config/application.rb
47
- config.active_record.schema_format = :sql
48
- ```
70
+ # possible, does not reorder the query:
71
+ RailsCVE::Entry.search_ranked('ruby rails').first
49
72
 
50
- While `RailsCVE::Entry.search` uses a predefined search query, you can, of
51
- course, build your own using Postgres' [text search functions and operators][2]
73
+ # not possible, tries but fails to reorder by ascending rank:
74
+ RailsCVE::Entry.search_ranked('ruby rails').last
52
75
 
76
+ # not possible:
77
+ RailsCVE::Entry.search_ranked('ruby rails').search_ranked('windows')
78
+
79
+ # use this instead:
80
+ RailsCVE::Entry.search_ranked('ruby rails & windows')
81
+ ```
53
82
 
54
83
  ## Installation
55
84
 
56
- Add `rails-cve` to your Gemfile and run `bundle install`
85
+ **This engine requires a PostgreSQL database!**
86
+
87
+ Add `rails-cve` to your Gemfile and run `bundle install`:
57
88
 
58
89
  ```ruby
59
90
  gem 'rails-cve'
@@ -72,6 +103,24 @@ writing):
72
103
  rake rails_cve:seed
73
104
  ```
74
105
 
106
+ Repeat the last step if you'd like to rebuild the entire CVE database.
107
+
108
+
109
+ ## Schema format
110
+
111
+ As you can see in the example above, we take advantage of PostgreSQL's text
112
+ search features. Hence, you will need to migrate to PostgreSQL unless you
113
+ haven't already.
114
+
115
+ I should mention, that it is **strongly recommended** to change your schema
116
+ format from Ruby to SQL, like so:
117
+
118
+ ```ruby
119
+ # in config/application.rb
120
+ config.active_record.schema_format = :sql
121
+ ```
122
+
123
+
75
124
  ## Maintenance
76
125
 
77
126
  You should (at least daily) import the updates. RailsCVE currently uses the
@@ -1,22 +1,35 @@
1
1
  module RailsCVE::Entry::References
2
2
  extend ActiveSupport::Concern
3
3
 
4
+ REF_DELIMITER = ' | '
5
+ REF_URI_RE = begin
6
+ prefix = /(?:ASCEND|CISCO|CONFIRM|MISC|REDHAT|URL|XF)/
7
+ scheme = /(?:https?|ftp|mailto)/
8
+ rest = /.*/
9
+ structure = /\A#{prefix}:(?<uri>#{scheme}:#{rest})\z/
10
+ end
11
+
4
12
  def reference_uris
5
13
  return [] unless references?
6
- references.grep(/^URL:/).map{|r| r.gsub(/^URL:/, '') }
14
+ @reference_uris = references.inject([]) do |refs, r|
15
+ REF_URI_RE.match(r){|m| refs << m[:uri] }
16
+ refs
17
+ end
7
18
  end
8
19
 
9
20
  def references=(refs)
10
- case refs
11
- when Array
12
- references_will_change!
13
- super refs.flatten
14
- when String
15
- references_will_change!
16
- super refs.split(' | ')
17
- else
18
- raise ArgumentError, 'invalid type'
21
+ unless Array === refs || String === refs
22
+ raise ArgumentError, 'invalid type'
19
23
  end
24
+
25
+ _refs = if Array === refs
26
+ refs.flatten
27
+ elsif String === refs
28
+ refs.split REF_DELIMITER
29
+ end
30
+
31
+ references_will_change!
32
+ super _refs.map(&:to_s).map(&:strip)
20
33
  end
21
34
 
22
35
  end
@@ -1,14 +1,21 @@
1
1
  module RailsCVE::Entry::Search
2
2
  extend ActiveSupport::Concern
3
3
 
4
+ TS_QUERY = "to_tsvector('english', description) @@ plainto_tsquery(%{q})"
5
+ TS_RANK = "ts_rank(to_tsvector(description), plainto_tsquery(%{q}))"
6
+
4
7
  included do
5
- scope :search, ->(query){
8
+ scope :search_ranked, ->(query){
6
9
  q = sanitize(query)
10
+ rank = "#{TS_RANK} AS rank" % { q: q }
7
11
 
8
- rank = "ts_rank(to_tsvector(description), plainto_tsquery(%{q}))" % { q: q }
9
- cond = "to_tsvector('english', description) @@ to_tsquery(%{q})" % { q: q }
12
+ select("*, #{rank}")
13
+ .search_plain(query)
14
+ .order("rank DESC")
15
+ }
10
16
 
11
- where(cond).order("#{rank} desc")
17
+ scope :search_plain, ->(query){
18
+ where(TS_QUERY % { q: sanitize(query) })
12
19
  }
13
20
  end
14
21
  end
@@ -1,3 +1,3 @@
1
1
  module RailsCVE
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rails_cve
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dominik Menke