siteguard_lite-custom_signature 0.3.0

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 620a9a897b60fad5a8d1a5c220e77b0fa839ec29ccc7b396b63f855462105829
4
+ data.tar.gz: 37dd3fcaeacf86b82e579a8c6dd6e92a0faab701f96f5d94a98260bff280bc7d
5
+ SHA512:
6
+ metadata.gz: 824194ec96956d8aed714a3d3017e4eae3766ab5d7c843c690d9e6a38bb6c3f4bc809a284dff4cece8897cf61336a15627105ed38a9d68fc4ee41a083b849c97
7
+ data.tar.gz: f62f14b812d333dc7cf91db36efecc4bc6301a55ae8844900c6e49a05d0a6bc6d1c7a29d21da8bcc5227b3d7956f62a08d43fabd2f15756bb15bbb96cff6946d
data/.gitignore ADDED
@@ -0,0 +1,12 @@
1
+ /Gemfile.lock
2
+ /.bundle/
3
+ /.yardoc
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+
11
+ # rspec failure tracking
12
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.5.1
5
+ before_install: gem install bundler -v 1.16.1
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in siteguard_lite-custom_signature.gemspec
6
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 GMO Pepabo, Inc.
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,83 @@
1
+ # SiteguardLite::CustomSignature
2
+
3
+ このライブラリはSiteGuard Liteのカスタム・シグネチャファイル(`sig_custom.txt`)を扱うためのライブラリです。
4
+
5
+ このファイルはタブ区切りフォーマットですが、このライブラリではYAMLフォーマットで扱えるようにします。
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'siteguard_lite-custom_signature'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install siteguard_lite-custom_signature
22
+
23
+ ## Usage
24
+
25
+ ```ruby
26
+ require 'siteguard_lite/custom_signature'
27
+
28
+ # Load yaml format (This gem's format)
29
+ rules = SiteguardLite::CustomSignature.load_yaml(File.read('sig_custom.yml'))
30
+
31
+ # Generate text format (SiteGuard Lite format)
32
+ SiteguardLite::CustomSignature.dump_text(rules)
33
+
34
+ # Load text format
35
+ rules = SiteguardLite::CustomSignature.load_text(File.read('sig_custom.txt'))
36
+
37
+ # Generate yaml format
38
+ SiteguardLite::CustomSignature.dump_yaml(rules)
39
+ ```
40
+
41
+ ## バリデーション
42
+
43
+ カスタム・シグネチャファイルには文字数やバイト数の制限を持つ項目があります。`dump_text()`メソッドではこの制限をチェックして違反している場合は例外を投げます。
44
+
45
+ ## 注意
46
+
47
+ `dump_yaml()`メソッドで生成されるYAMLにはフォーマット上の制限があります。
48
+
49
+ ### 配列のインデント
50
+
51
+ このgemでは以下のようなYAMLが生成されます。
52
+
53
+ ```yaml
54
+ rules:
55
+ - name: exclude-foo
56
+ comment: fooを除外する
57
+ ```
58
+
59
+ 以下のようにインデントされません。
60
+
61
+ ```yaml
62
+ rules:
63
+ - name: exclude-foo
64
+ comment: fooを除外する
65
+ ```
66
+
67
+ ### アンカーとエイリアス
68
+
69
+ YAMLでは`&`と`*`を使ったアンカーとエイリアスを使うと重複した記述を避けることができて便利ですが、生成されるYAMLはそのようになりません。
70
+
71
+ ## Development
72
+
73
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
74
+
75
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags and no pushing the `.gem` file to [rubygems.org](https://rubygems.org).
76
+
77
+ ## Contributing
78
+
79
+ Bug reports and pull requests are welcome on GitHub at https://github.com/pepabo/siteguard_lite-custom_signature.
80
+
81
+ ## License
82
+
83
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "siteguard_lite/custom_signature"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,33 @@
1
+ require "active_model"
2
+ require "yaml"
3
+
4
+ require "siteguard_lite/custom_signature/version"
5
+ require "siteguard_lite/custom_signature/bytesize_validator"
6
+ require "siteguard_lite/custom_signature/rule"
7
+ require "siteguard_lite/custom_signature/condition"
8
+ require "siteguard_lite/custom_signature/yaml_dumper"
9
+ require "siteguard_lite/custom_signature/yaml_loader"
10
+ require "siteguard_lite/custom_signature/text_dumper"
11
+ require "siteguard_lite/custom_signature/text_loader"
12
+
13
+ module SiteguardLite
14
+ module CustomSignature
15
+ class << self
16
+ def load_yaml(yaml)
17
+ YamlLoader.load(yaml)
18
+ end
19
+
20
+ def load_text(text)
21
+ TextLoader.load(text)
22
+ end
23
+
24
+ def dump_yaml(rules)
25
+ YamlDumper.dump(rules)
26
+ end
27
+
28
+ def dump_text(rules)
29
+ TextDumper.dump(rules)
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,7 @@
1
+ class BytesizeValidator < ActiveModel::EachValidator
2
+ def validate_each(record, attribute, value)
3
+ if value.bytesize > options[:maximum]
4
+ record.errors.add(attribute, :too_long, count: options[:maximum])
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,56 @@
1
+ module SiteguardLite
2
+ module CustomSignature
3
+ class Condition
4
+ include ActiveModel::Validations
5
+
6
+ attr_reader :key, :value, :comparison_methods
7
+
8
+ validates :value, bytesize: { maximum: 1999 }
9
+
10
+ def initialize(k, v, comparison_methods)
11
+ @key = k
12
+ @value = v
13
+ @comparison_methods = comparison_methods
14
+ end
15
+
16
+ # siteguardlite-320-0_nginx.pdf, p.52
17
+ # [有効・無効]<タブ>[動作]<タブ><タブ>[シグネチャ名]<タブ>[検査対象] <タブ>[比較方法]<タブ> [検査文字列]<タブ><タブ>[コメント]
18
+ def to_text(rule, last: false)
19
+ validate!
20
+
21
+ [
22
+ rule.enable_str,
23
+ 'NONE',
24
+ '',
25
+ rule.name,
26
+ @key,
27
+ comparison_str(rule, last),
28
+ @value,
29
+ '',
30
+ rule.comment,
31
+ ].join("\t")
32
+ end
33
+
34
+ def to_hash
35
+ {
36
+ key: @key,
37
+ value: @value,
38
+ comparison_methods: @comparison_methods,
39
+ }
40
+ end
41
+
42
+ private
43
+
44
+ def comparison_str(rule, last)
45
+ if last
46
+ [
47
+ @comparison_methods,
48
+ "#{rule.exclusion_action}(#{rule.signature})"
49
+ ].flatten.join(',')
50
+ else
51
+ @comparison_methods.join(',')
52
+ end
53
+ end
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,53 @@
1
+ module SiteguardLite
2
+ module CustomSignature
3
+ class Rule
4
+ include ActiveModel::Validations
5
+
6
+ attr_reader :name, :comment, :enable, :conditions
7
+ attr_accessor :exclusion_action, :signature
8
+
9
+ validates :name, bytesize: { maximum: 29 }
10
+ validates :signature, bytesize: { maximum: 999 }
11
+
12
+ def initialize(args)
13
+ @name = args[:name]
14
+ @comment = args[:comment]
15
+ @exclusion_action = args[:exclusion_action]
16
+ @signature = args[:signature]
17
+
18
+ @enable = true
19
+
20
+ @conditions = []
21
+ end
22
+
23
+ def add_condition(k, v, comparison_methods)
24
+ @conditions << SiteguardLite::CustomSignature::Condition.new(k, v, comparison_methods)
25
+ end
26
+
27
+ def enable_str
28
+ @enable ? 'ON' : 'OFF'
29
+ end
30
+
31
+ def to_text
32
+ validate!
33
+
34
+ texts = []
35
+ last_idx = @conditions.length - 1
36
+ @conditions.each_with_index do |condition, idx|
37
+ texts << condition.to_text(self, last: idx == last_idx)
38
+ end
39
+ texts.join("\n")
40
+ end
41
+
42
+ def to_hash
43
+ {
44
+ name: @name,
45
+ comment: @comment,
46
+ exclusion_action: @exclusion_action,
47
+ signature: @signature,
48
+ conditions: @conditions.map { |c| c.to_hash },
49
+ }
50
+ end
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,9 @@
1
+ module SiteguardLite
2
+ module CustomSignature
3
+ class TextDumper
4
+ def self.dump(rules)
5
+ rules.map { |r| r.to_text }.join("\n") + "\n"
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,65 @@
1
+ module SiteguardLite
2
+ module CustomSignature
3
+ class TextLineParser
4
+ # siteguardlite-320-0_nginx.pdf, p.52
5
+ # [有効・無効]<タブ>[動作]<タブ><タブ>[シグネチャ名]<タブ>[検査対象] <タブ>[比較方法]<タブ> [検査文字列]<タブ><タブ>[コメント]
6
+ def parse(line)
7
+ fields = line.split("\t")
8
+ {
9
+ enable: enable(fields[0]),
10
+ action: fields[1],
11
+ name: fields[3],
12
+ comment: fields[8],
13
+ exclusion_action: exclusion_action(fields[5]),
14
+ signature: signature(fields[5]),
15
+ condition: {
16
+ key: fields[4],
17
+ comparison_methods: comparison_methods(fields[5]),
18
+ value: fields[6],
19
+ }
20
+ }
21
+ end
22
+
23
+ private
24
+
25
+ def enable(str)
26
+ str == 'ON'
27
+ end
28
+
29
+ def exclusion_action(comparison_str)
30
+ parse_comparison_str(comparison_str)[:exclusion_action]
31
+ end
32
+
33
+ def signature(comparison_str)
34
+ parse_comparison_str(comparison_str)[:signature]
35
+ end
36
+
37
+ def comparison_methods(comparison_str)
38
+ parse_comparison_str(comparison_str)[:comparison_methods]
39
+ end
40
+
41
+ def parse_comparison_str(str)
42
+ return @parsed_comarison_str if @parsed_comparison_str
43
+
44
+ result = {
45
+ comparison_methods: [],
46
+ exclusion_action: nil,
47
+ signature: nil,
48
+ }
49
+
50
+ # TODO: Make it possible to handle other comparison methods and exclusion actions
51
+ str.split(',').each do |part|
52
+ case part
53
+ when /\AEXCLUDE_OFFICIAL\((.+)\)\z/
54
+ result[:exclusion_action] = 'EXCLUDE_OFFICIAL'
55
+ result[:signature] = Regexp.last_match(1)
56
+ else
57
+ result[:comparison_methods] << part
58
+ end
59
+ end
60
+
61
+ @parsed_comarison_str = result
62
+ end
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,41 @@
1
+ require "siteguard_lite/custom_signature/text_line_parser"
2
+
3
+ module SiteguardLite
4
+ module CustomSignature
5
+ class TextLoader
6
+ def self.load(text)
7
+ rules = []
8
+ rule = nil
9
+
10
+ line_parser = TextLineParser.new
11
+ text.split("\n").each do |line|
12
+ parsed = line_parser.parse(line)
13
+
14
+ # collect each line into the rules
15
+ if rule&.name == parsed[:name]
16
+ # This is the continuation from the previous line
17
+ rule.add_condition(*parsed[:condition].fetch_values(:key, :value, :comparison_methods))
18
+ rule.exclusion_action = parsed[:exclusion_action]
19
+ rule.signature = parsed[:signature]
20
+ else
21
+ # This line is a new rule
22
+ rules << rule if rule
23
+ rule = Rule.new(
24
+ enable: parsed[:enable],
25
+ action: parsed[:action],
26
+ name: parsed[:name],
27
+ comment: parsed[:comment],
28
+ exclusion_action: parsed[:exclusion_action],
29
+ signature: parsed[:signature],
30
+ )
31
+ rule.add_condition(*parsed[:condition].fetch_values(:key, :value, :comparison_methods))
32
+ end
33
+ end
34
+
35
+ rules << rule if rule
36
+
37
+ rules
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,5 @@
1
+ module SiteguardLite
2
+ module CustomSignature
3
+ VERSION = "0.3.0"
4
+ end
5
+ end
@@ -0,0 +1,13 @@
1
+ require "active_support/core_ext/hash/indifferent_access"
2
+
3
+ module SiteguardLite
4
+ module CustomSignature
5
+ class YamlDumper
6
+ def self.dump(rules)
7
+ {
8
+ rules: rules.map { |r| r.to_hash }
9
+ }.with_indifferent_access.deep_stringify_keys.to_hash.to_yaml
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,23 @@
1
+ module SiteguardLite
2
+ module CustomSignature
3
+ class YamlLoader
4
+ def self.load(yaml)
5
+ y = ::YAML.load(yaml)
6
+ rules = []
7
+ y['rules'].each do |r|
8
+ rule = SiteguardLite::CustomSignature::Rule.new(
9
+ name: r['name'],
10
+ comment: r['comment'],
11
+ exclusion_action: r['exclusion_action'],
12
+ signature: r['signature']
13
+ )
14
+ r['conditions'].each do |c|
15
+ rule.add_condition(c['key'], c['value'], c['comparison_methods'])
16
+ end
17
+ rules << rule
18
+ end
19
+ rules
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,30 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "siteguard_lite/custom_signature/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "siteguard_lite-custom_signature"
8
+ spec.version = SiteguardLite::CustomSignature::VERSION
9
+ spec.authors = ["Takatoshi Ono"]
10
+ spec.email = ["takatoshi.ono@gmail.com"]
11
+
12
+ spec.summary = %q{This library load and dump SiteGuard Lite custom signature file.}
13
+ spec.description = %q{This library load and dump SiteGuard Lite custom signature file.}
14
+ spec.homepage = "https://github.com/pepabo/siteguard_lite-custom_signature"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
18
+ f.match(%r{^(test|spec|features)/})
19
+ end
20
+ spec.bindir = "exe"
21
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
+ spec.require_paths = ["lib"]
23
+
24
+ spec.add_dependency "activemodel"
25
+ spec.add_dependency "activesupport"
26
+
27
+ spec.add_development_dependency "bundler", "~> 1.16"
28
+ spec.add_development_dependency "rake", "~> 10.0"
29
+ spec.add_development_dependency "rspec", "~> 3.0"
30
+ end
metadata ADDED
@@ -0,0 +1,134 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: siteguard_lite-custom_signature
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.3.0
5
+ platform: ruby
6
+ authors:
7
+ - Takatoshi Ono
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-07-24 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activemodel
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: activesupport
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.16'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.16'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '3.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '3.0'
83
+ description: This library load and dump SiteGuard Lite custom signature file.
84
+ email:
85
+ - takatoshi.ono@gmail.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - ".rspec"
92
+ - ".travis.yml"
93
+ - Gemfile
94
+ - LICENSE.txt
95
+ - README.md
96
+ - Rakefile
97
+ - bin/console
98
+ - bin/setup
99
+ - lib/siteguard_lite/custom_signature.rb
100
+ - lib/siteguard_lite/custom_signature/bytesize_validator.rb
101
+ - lib/siteguard_lite/custom_signature/condition.rb
102
+ - lib/siteguard_lite/custom_signature/rule.rb
103
+ - lib/siteguard_lite/custom_signature/text_dumper.rb
104
+ - lib/siteguard_lite/custom_signature/text_line_parser.rb
105
+ - lib/siteguard_lite/custom_signature/text_loader.rb
106
+ - lib/siteguard_lite/custom_signature/version.rb
107
+ - lib/siteguard_lite/custom_signature/yaml_dumper.rb
108
+ - lib/siteguard_lite/custom_signature/yaml_loader.rb
109
+ - siteguard_lite-custom_signature.gemspec
110
+ homepage: https://github.com/pepabo/siteguard_lite-custom_signature
111
+ licenses:
112
+ - MIT
113
+ metadata: {}
114
+ post_install_message:
115
+ rdoc_options: []
116
+ require_paths:
117
+ - lib
118
+ required_ruby_version: !ruby/object:Gem::Requirement
119
+ requirements:
120
+ - - ">="
121
+ - !ruby/object:Gem::Version
122
+ version: '0'
123
+ required_rubygems_version: !ruby/object:Gem::Requirement
124
+ requirements:
125
+ - - ">="
126
+ - !ruby/object:Gem::Version
127
+ version: '0'
128
+ requirements: []
129
+ rubyforge_project:
130
+ rubygems_version: 2.7.6
131
+ signing_key:
132
+ specification_version: 4
133
+ summary: This library load and dump SiteGuard Lite custom signature file.
134
+ test_files: []