liberty-web 0.1.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 +7 -0
- data/.qlty/.gitignore +7 -0
- data/.qlty/qlty.toml +81 -0
- data/.rspec +2 -0
- data/.ruby-version +1 -0
- data/.standard.yml +3 -0
- data/CHANGELOG.md +14 -0
- data/CODE_OF_CONDUCT.md +84 -0
- data/Gemfile +12 -0
- data/Gemfile.lock +167 -0
- data/LICENSE.txt +21 -0
- data/README.md +147 -0
- data/Rakefile +31 -0
- data/assets/liberty.png +0 -0
- data/lib/liberty/web/config.rb +63 -0
- data/lib/liberty/web/content_type_options.rb +22 -0
- data/lib/liberty/web/endpoint.rb +31 -0
- data/lib/liberty/web/errors.rb +12 -0
- data/lib/liberty/web/redirect.rb +31 -0
- data/lib/liberty/web/request_logger.rb +26 -0
- data/lib/liberty/web/stack.rb +56 -0
- data/lib/liberty/web/testing.rb +25 -0
- data/lib/liberty/web/version.rb +7 -0
- data/lib/liberty/web.rb +29 -0
- data/liberty-web.gemspec +35 -0
- metadata +126 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 1a3a232c3de7bcab974b91d8e7c9370aa085c93ba3133a0416aa743ddbb0dfc0
|
|
4
|
+
data.tar.gz: 97f424f9249fc77134937f1829ec376d2ac360c9fa33c7f481627033464956b9
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 3cfd53425ff26efedafac9b24e209e0b36178785192f614d37971a3476a81454c8fd386ecb29362c43960827168da440a2202f7fffbf7da97fac7f8691bf2ffd
|
|
7
|
+
data.tar.gz: acfc63ed0be078cb61d2cf4af3512f391e734eab28dc12cbf54dee7e8658d25527361ecc935837bbdfb7e7cd674cb26d8bd546106a32f2fc7fd06189165ce562
|
data/.qlty/.gitignore
ADDED
data/.qlty/qlty.toml
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
# This file was automatically generated by `qlty init`.
|
|
2
|
+
# You can modify it to suit your needs.
|
|
3
|
+
# We recommend you to commit this file to your repository.
|
|
4
|
+
#
|
|
5
|
+
# This configuration is used by both Qlty CLI and Qlty Cloud.
|
|
6
|
+
#
|
|
7
|
+
# Qlty CLI -- Code quality toolkit for developers
|
|
8
|
+
# Qlty Cloud -- Fully automated Code Health Platform
|
|
9
|
+
#
|
|
10
|
+
# Try Qlty Cloud: https://qlty.sh
|
|
11
|
+
#
|
|
12
|
+
# For a guide to configuration, visit https://qlty.sh/d/config
|
|
13
|
+
# Or for a full reference, visit https://qlty.sh/d/qlty-toml
|
|
14
|
+
config_version = "0"
|
|
15
|
+
|
|
16
|
+
exclude_patterns = [
|
|
17
|
+
"*_min.*",
|
|
18
|
+
"*-min.*",
|
|
19
|
+
"*.min.*",
|
|
20
|
+
"**/.yarn/**",
|
|
21
|
+
"**/*.d.ts",
|
|
22
|
+
"**/assets/**",
|
|
23
|
+
"**/bower_components/**",
|
|
24
|
+
"**/build/**",
|
|
25
|
+
"**/cache/**",
|
|
26
|
+
"**/config/**",
|
|
27
|
+
"**/db/**",
|
|
28
|
+
"**/deps/**",
|
|
29
|
+
"**/dist/**",
|
|
30
|
+
"**/extern/**",
|
|
31
|
+
"**/external/**",
|
|
32
|
+
"**/generated/**",
|
|
33
|
+
"**/Godeps/**",
|
|
34
|
+
"**/gradlew/**",
|
|
35
|
+
"**/mvnw/**",
|
|
36
|
+
"**/node_modules/**",
|
|
37
|
+
"**/protos/**",
|
|
38
|
+
"**/seed/**",
|
|
39
|
+
"**/target/**",
|
|
40
|
+
"**/templates/**",
|
|
41
|
+
"**/testdata/**",
|
|
42
|
+
"**/vendor/**",
|
|
43
|
+
]
|
|
44
|
+
|
|
45
|
+
test_patterns = [
|
|
46
|
+
"**/test/**",
|
|
47
|
+
"**/spec/**",
|
|
48
|
+
"**/*.test.*",
|
|
49
|
+
"**/*.spec.*",
|
|
50
|
+
"**/*_test.*",
|
|
51
|
+
"**/*_spec.*",
|
|
52
|
+
"**/test_*.*",
|
|
53
|
+
"**/spec_*.*",
|
|
54
|
+
]
|
|
55
|
+
|
|
56
|
+
[smells]
|
|
57
|
+
mode = "comment"
|
|
58
|
+
|
|
59
|
+
[[source]]
|
|
60
|
+
name = "default"
|
|
61
|
+
default = true
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
[[plugin]]
|
|
65
|
+
name = "actionlint"
|
|
66
|
+
|
|
67
|
+
[[plugin]]
|
|
68
|
+
name = "osv-scanner"
|
|
69
|
+
|
|
70
|
+
[[plugin]]
|
|
71
|
+
name = "ripgrep"
|
|
72
|
+
mode = "comment"
|
|
73
|
+
|
|
74
|
+
[[plugin]]
|
|
75
|
+
name = "standardrb"
|
|
76
|
+
|
|
77
|
+
[[plugin]]
|
|
78
|
+
name = "trufflehog"
|
|
79
|
+
|
|
80
|
+
[[plugin]]
|
|
81
|
+
name = "zizmor"
|
data/.rspec
ADDED
data/.ruby-version
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
4.0.6
|
data/.standard.yml
ADDED
data/CHANGELOG.md
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
## [0.1.0] - 2026-09-19
|
|
2
|
+
|
|
3
|
+
The first release. Liberty Web is the browser stack for a Liberty application, kept out of Liberty itself so an API never carries sessions it did not ask for.
|
|
4
|
+
|
|
5
|
+
### Added
|
|
6
|
+
|
|
7
|
+
- `Liberty::Web::Config`, a value the application fills in: `session_secrets`, `permitted_hosts`, `secure_cookies`, and optionally `static` and `logger`. Building one never raises; `errors` names what is missing or wrong, one sentence each, and `valid?` says whether there are any.
|
|
8
|
+
- `Liberty::Web.app(config)`, which wraps `Liberty.rack_app` in the browser middleware in the order a browser needs: static files, a request logger, method override, an encrypted cookie session, host authorization, the CSRF token check, the origin check, frame options, and `x-content-type-options: nosniff`. It raises `Liberty::Web::ConfigurationError` for a config with errors, naming every one, and builds nothing.
|
|
9
|
+
- `Liberty::Web::Endpoint`, a `Liberty::Endpoint` with three private helpers: `session`, `csrf_token`, and `location(path)`. Templates are the application's; the gem never sees its renderer.
|
|
10
|
+
- `Liberty::Web::Redirect.to(path, status: 303)`, an endpoint class that only redirects, for an authenticator's `challenge_endpoint_class`. One class per path and status.
|
|
11
|
+
- `Liberty::Web::RequestLogger`, the default logger: INFO on the request's own error stream, so rack-protection's per-request debug output stays out of the log and a spec can read what was warned. An injected logger replaces it.
|
|
12
|
+
- `Liberty::Web::ContentTypeOptions`, the useful half of rack-protection's `XSSHeader`. The obsolete `x-xss-protection` header is not sent.
|
|
13
|
+
- `Liberty::Web::Testing`, required separately, with `lint(app)` for a Rack::Lint-wrapped app and `csrf_token_in(html)` for posting forms back in request specs.
|
|
14
|
+
- `Liberty::Web::Error`, a `Liberty::Error`, and `Liberty::Web::ConfigurationError` beneath it.
|
data/CODE_OF_CONDUCT.md
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
|
2
|
+
|
|
3
|
+
## Our Pledge
|
|
4
|
+
|
|
5
|
+
We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
|
|
6
|
+
|
|
7
|
+
We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
|
|
8
|
+
|
|
9
|
+
## Our Standards
|
|
10
|
+
|
|
11
|
+
Examples of behavior that contributes to a positive environment for our community include:
|
|
12
|
+
|
|
13
|
+
* Demonstrating empathy and kindness toward other people
|
|
14
|
+
* Being respectful of differing opinions, viewpoints, and experiences
|
|
15
|
+
* Giving and gracefully accepting constructive feedback
|
|
16
|
+
* Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
|
|
17
|
+
* Focusing on what is best not just for us as individuals, but for the overall community
|
|
18
|
+
|
|
19
|
+
Examples of unacceptable behavior include:
|
|
20
|
+
|
|
21
|
+
* The use of sexualized language or imagery, and sexual attention or
|
|
22
|
+
advances of any kind
|
|
23
|
+
* Trolling, insulting or derogatory comments, and personal or political attacks
|
|
24
|
+
* Public or private harassment
|
|
25
|
+
* Publishing others' private information, such as a physical or email
|
|
26
|
+
address, without their explicit permission
|
|
27
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
|
28
|
+
professional setting
|
|
29
|
+
|
|
30
|
+
## Enforcement Responsibilities
|
|
31
|
+
|
|
32
|
+
Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
|
|
33
|
+
|
|
34
|
+
Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
|
|
35
|
+
|
|
36
|
+
## Scope
|
|
37
|
+
|
|
38
|
+
This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
|
|
39
|
+
|
|
40
|
+
## Enforcement
|
|
41
|
+
|
|
42
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at alan@ridlehoover.com. All complaints will be reviewed and investigated promptly and fairly.
|
|
43
|
+
|
|
44
|
+
All community leaders are obligated to respect the privacy and security of the reporter of any incident.
|
|
45
|
+
|
|
46
|
+
## Enforcement Guidelines
|
|
47
|
+
|
|
48
|
+
Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
|
|
49
|
+
|
|
50
|
+
### 1. Correction
|
|
51
|
+
|
|
52
|
+
**Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
|
|
53
|
+
|
|
54
|
+
**Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
|
|
55
|
+
|
|
56
|
+
### 2. Warning
|
|
57
|
+
|
|
58
|
+
**Community Impact**: A violation through a single incident or series of actions.
|
|
59
|
+
|
|
60
|
+
**Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
|
|
61
|
+
|
|
62
|
+
### 3. Temporary Ban
|
|
63
|
+
|
|
64
|
+
**Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
|
|
65
|
+
|
|
66
|
+
**Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
|
|
67
|
+
|
|
68
|
+
### 4. Permanent Ban
|
|
69
|
+
|
|
70
|
+
**Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
|
|
71
|
+
|
|
72
|
+
**Consequence**: A permanent ban from any sort of public interaction within the community.
|
|
73
|
+
|
|
74
|
+
## Attribution
|
|
75
|
+
|
|
76
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
|
|
77
|
+
available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
|
|
78
|
+
|
|
79
|
+
Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
|
|
80
|
+
|
|
81
|
+
[homepage]: https://www.contributor-covenant.org
|
|
82
|
+
|
|
83
|
+
For answers to common questions about this code of conduct, see the FAQ at
|
|
84
|
+
https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
PATH
|
|
2
|
+
remote: .
|
|
3
|
+
specs:
|
|
4
|
+
liberty-web (0.1.0)
|
|
5
|
+
liberty (~> 0.5)
|
|
6
|
+
rack (~> 3.1)
|
|
7
|
+
rack-protection (~> 4.2)
|
|
8
|
+
rack-session (~> 2.1)
|
|
9
|
+
|
|
10
|
+
GEM
|
|
11
|
+
remote: https://rubygems.org/
|
|
12
|
+
specs:
|
|
13
|
+
ast (2.4.3)
|
|
14
|
+
base64 (0.3.0)
|
|
15
|
+
diff-lcs (1.6.2)
|
|
16
|
+
flog (4.9.4)
|
|
17
|
+
path_expander (~> 2.0)
|
|
18
|
+
prism (~> 1.7)
|
|
19
|
+
sexp_processor (~> 4.8)
|
|
20
|
+
hansi (0.2.1)
|
|
21
|
+
json (2.21.2)
|
|
22
|
+
language_server-protocol (3.17.0.6)
|
|
23
|
+
liberty (0.5.0)
|
|
24
|
+
json (~> 2.3)
|
|
25
|
+
mustermann (~> 4.0)
|
|
26
|
+
mustermann-contrib (~> 4.0)
|
|
27
|
+
rack (~> 3.1)
|
|
28
|
+
rack-abstract-format (~> 0.9.9)
|
|
29
|
+
rack-accept-media-types (~> 0.9)
|
|
30
|
+
lint_roller (1.1.0)
|
|
31
|
+
logger (1.7.0)
|
|
32
|
+
mustermann (4.0.0)
|
|
33
|
+
mustermann-contrib (4.0.0)
|
|
34
|
+
hansi (~> 0.2.0)
|
|
35
|
+
mustermann (= 4.0.0)
|
|
36
|
+
parallel (2.2.0)
|
|
37
|
+
parser (3.3.12.0)
|
|
38
|
+
ast (~> 2.4.1)
|
|
39
|
+
racc
|
|
40
|
+
path_expander (2.0.1)
|
|
41
|
+
prism (1.9.0)
|
|
42
|
+
racc (1.8.1)
|
|
43
|
+
rack (3.2.7)
|
|
44
|
+
rack-abstract-format (0.9.9)
|
|
45
|
+
rack-accept-media-types (0.9)
|
|
46
|
+
rack-protection (4.2.1)
|
|
47
|
+
base64 (>= 0.1.0)
|
|
48
|
+
logger (>= 1.6.0)
|
|
49
|
+
rack (>= 3.0.0, < 4)
|
|
50
|
+
rack-session (2.1.2)
|
|
51
|
+
base64 (>= 0.1.0)
|
|
52
|
+
rack (>= 3.0.0)
|
|
53
|
+
rack-test (2.2.0)
|
|
54
|
+
rack (>= 1.3)
|
|
55
|
+
rainbow (3.1.1)
|
|
56
|
+
rake (13.4.2)
|
|
57
|
+
regexp_parser (2.12.0)
|
|
58
|
+
rspec (3.13.2)
|
|
59
|
+
rspec-core (~> 3.13.0)
|
|
60
|
+
rspec-expectations (~> 3.13.0)
|
|
61
|
+
rspec-mocks (~> 3.13.0)
|
|
62
|
+
rspec-core (3.13.6)
|
|
63
|
+
rspec-support (~> 3.13.0)
|
|
64
|
+
rspec-expectations (3.13.5)
|
|
65
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
|
66
|
+
rspec-support (~> 3.13.0)
|
|
67
|
+
rspec-mocks (3.13.8)
|
|
68
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
|
69
|
+
rspec-support (~> 3.13.0)
|
|
70
|
+
rspec-support (3.13.7)
|
|
71
|
+
rubocop (1.88.2)
|
|
72
|
+
json (~> 2.3)
|
|
73
|
+
language_server-protocol (~> 3.17.0.2)
|
|
74
|
+
lint_roller (~> 1.1.0)
|
|
75
|
+
parallel (>= 1.10)
|
|
76
|
+
parser (>= 3.3.0.2)
|
|
77
|
+
rainbow (>= 2.2.2, < 4.0)
|
|
78
|
+
regexp_parser (>= 2.9.3, < 3.0)
|
|
79
|
+
rubocop-ast (>= 1.49.0, < 2.0)
|
|
80
|
+
ruby-progressbar (~> 1.7)
|
|
81
|
+
unicode-display_width (>= 2.4.0, < 4.0)
|
|
82
|
+
rubocop-ast (1.50.0)
|
|
83
|
+
parser (>= 3.3.7.2)
|
|
84
|
+
prism (~> 1.7)
|
|
85
|
+
rubocop-performance (1.26.1)
|
|
86
|
+
lint_roller (~> 1.1)
|
|
87
|
+
rubocop (>= 1.75.0, < 2.0)
|
|
88
|
+
rubocop-ast (>= 1.47.1, < 2.0)
|
|
89
|
+
ruby-progressbar (1.13.0)
|
|
90
|
+
sexp_processor (4.17.5)
|
|
91
|
+
simplecov (1.3.0)
|
|
92
|
+
standard (1.56.0)
|
|
93
|
+
language_server-protocol (~> 3.17.0.2)
|
|
94
|
+
lint_roller (~> 1.0)
|
|
95
|
+
rubocop (~> 1.88.0)
|
|
96
|
+
standard-custom (~> 1.0.0)
|
|
97
|
+
standard-performance (~> 1.8)
|
|
98
|
+
standard-custom (1.0.2)
|
|
99
|
+
lint_roller (~> 1.0)
|
|
100
|
+
rubocop (~> 1.50)
|
|
101
|
+
standard-performance (1.9.0)
|
|
102
|
+
lint_roller (~> 1.1)
|
|
103
|
+
rubocop-performance (~> 1.26.0)
|
|
104
|
+
unicode-display_width (3.2.0)
|
|
105
|
+
unicode-emoji (~> 4.1)
|
|
106
|
+
unicode-emoji (4.2.0)
|
|
107
|
+
|
|
108
|
+
PLATFORMS
|
|
109
|
+
arm64-darwin-23
|
|
110
|
+
ruby
|
|
111
|
+
|
|
112
|
+
DEPENDENCIES
|
|
113
|
+
flog
|
|
114
|
+
liberty-web!
|
|
115
|
+
rack-test (~> 2.2)
|
|
116
|
+
rake (~> 13.0)
|
|
117
|
+
rspec (~> 3.13)
|
|
118
|
+
simplecov (~> 1.0)
|
|
119
|
+
standard (~> 1.0)
|
|
120
|
+
|
|
121
|
+
CHECKSUMS
|
|
122
|
+
ast (2.4.3) sha256=954615157c1d6a382bc27d690d973195e79db7f55e9765ac7c481c60bdb4d383
|
|
123
|
+
base64 (0.3.0) sha256=27337aeabad6ffae05c265c450490628ef3ebd4b67be58257393227588f5a97b
|
|
124
|
+
diff-lcs (1.6.2) sha256=9ae0d2cba7d4df3075fe8cd8602a8604993efc0dfa934cff568969efb1909962
|
|
125
|
+
flog (4.9.4) sha256=12cc054fab7a2cbd2a906514397c4d7788954d530564782d6f14939dc2dfbcbb
|
|
126
|
+
hansi (0.2.1) sha256=7e27dfd729f1692a3a7690fc0ef3af9d5e3cfc0285bb135cae7e56180e4805db
|
|
127
|
+
json (2.21.2) sha256=1f1d3b7cf2b3ba1a69beca0bb6db13d5438b80bff3cd54cdaaa620b9b07c1c6a
|
|
128
|
+
language_server-protocol (3.17.0.6) sha256=5ef2c0c138f8267e1bc631d3328347d354f96724b0af22f2c79516120443b7f0
|
|
129
|
+
liberty (0.5.0) sha256=98653b326537b3801b27c239749fe01b1a119191af9b945ff34c4e8c497757c0
|
|
130
|
+
liberty-web (0.1.0)
|
|
131
|
+
lint_roller (1.1.0) sha256=2c0c845b632a7d172cb849cc90c1bce937a28c5c8ccccb50dfd46a485003cc87
|
|
132
|
+
logger (1.7.0) sha256=196edec7cc44b66cfb40f9755ce11b392f21f7967696af15d274dde7edff0203
|
|
133
|
+
mustermann (4.0.0) sha256=91f67411bb208d1d93c41e6128cb3b0f8ddd9ec7c45966f1007e1c43c08040d7
|
|
134
|
+
mustermann-contrib (4.0.0) sha256=62f3f9e15f64b36c512faf4582afd78c1ee8d8d80edafe0f4ddfe8a4c0ef12b2
|
|
135
|
+
parallel (2.2.0) sha256=e1059c5fd7b649558a0aec38a769f06a42942bdb40503d005a59c352fe011cd8
|
|
136
|
+
parser (3.3.12.0) sha256=21a6d7f755d5a24dfbdc6e6b772e4e879a52e7631a88bc5a3a134606052c9828
|
|
137
|
+
path_expander (2.0.1) sha256=2de201164bff4719cc4d0b3767286e9977cc832a59c4d70abab571ec86cb41e4
|
|
138
|
+
prism (1.9.0) sha256=7b530c6a9f92c24300014919c9dcbc055bf4cdf51ec30aed099b06cd6674ef85
|
|
139
|
+
racc (1.8.1) sha256=4a7f6929691dbec8b5209a0b373bc2614882b55fc5d2e447a21aaa691303d62f
|
|
140
|
+
rack (3.2.7) sha256=93e13e1c24f93556671d85d2d79fa228c3485815c50d7e2f265b5330c6528fb7
|
|
141
|
+
rack-abstract-format (0.9.9) sha256=376c0897085e99de8d91742c1bb3a7f69a1d377349e6e0f2a56bd462a6448b78
|
|
142
|
+
rack-accept-media-types (0.9) sha256=2aace41f176858ec711da1b31ff16067859f30551dff22ce2c2a4d38f45ff081
|
|
143
|
+
rack-protection (4.2.1) sha256=cf6e2842df8c55f5e4d1a4be015e603e19e9bc3a7178bae58949ccbb58558bac
|
|
144
|
+
rack-session (2.1.2) sha256=595434f8c0c3473ae7d7ac56ecda6cc6dfd9d37c0b2b5255330aa1576967ffe8
|
|
145
|
+
rack-test (2.2.0) sha256=005a36692c306ac0b4a9350355ee080fd09ddef1148a5f8b2ac636c720f5c463
|
|
146
|
+
rainbow (3.1.1) sha256=039491aa3a89f42efa1d6dec2fc4e62ede96eb6acd95e52f1ad581182b79bc6a
|
|
147
|
+
rake (13.4.2) sha256=cb825b2bd5f1f8e91ca37bddb4b9aaf345551b4731da62949be002fa89283701
|
|
148
|
+
regexp_parser (2.12.0) sha256=35a916a1d63190ab5c9009457136ae5f3c0c7512d60291d0d1378ba18ce08ebb
|
|
149
|
+
rspec (3.13.2) sha256=206284a08ad798e61f86d7ca3e376718d52c0bc944626b2349266f239f820587
|
|
150
|
+
rspec-core (3.13.6) sha256=a8823c6411667b60a8bca135364351dda34cd55e44ff94c4be4633b37d828b2d
|
|
151
|
+
rspec-expectations (3.13.5) sha256=33a4d3a1d95060aea4c94e9f237030a8f9eae5615e9bd85718fe3a09e4b58836
|
|
152
|
+
rspec-mocks (3.13.8) sha256=086ad3d3d17533f4237643de0b5c42f04b66348c28bf6b9c2d3f4a3b01af1d47
|
|
153
|
+
rspec-support (3.13.7) sha256=0640e5570872aafefd79867901deeeeb40b0c9875a36b983d85f54fb7381c47c
|
|
154
|
+
rubocop (1.88.2) sha256=8def251c90cd955feb4daa3edc0ab56893250c4ce90ef81e6c80c03f9a939bbf
|
|
155
|
+
rubocop-ast (1.50.0) sha256=b9ca88300da0803ee222ad20cdb30494c0a784eed06fdc35d254b06d662788db
|
|
156
|
+
rubocop-performance (1.26.1) sha256=cd19b936ff196df85829d264b522fd4f98b6c89ad271fa52744a8c11b8f71834
|
|
157
|
+
ruby-progressbar (1.13.0) sha256=80fc9c47a9b640d6834e0dc7b3c94c9df37f08cb072b7761e4a71e22cff29b33
|
|
158
|
+
sexp_processor (4.17.5) sha256=ae2b48ba98353d5d465ce8759836b7a05f2e12c5879fcd14d7815b026de32f0e
|
|
159
|
+
simplecov (1.3.0) sha256=d9886307863c1ead47657dcbed869bfffd82318808b09014d8f0ff77dcfe7492
|
|
160
|
+
standard (1.56.0) sha256=ae2af4d9669589162ac69ed5ef59dcf9f346d4afc81f7e62b84339310dfcb787
|
|
161
|
+
standard-custom (1.0.2) sha256=424adc84179a074f1a2a309bb9cf7cd6bfdb2b6541f20c6bf9436c0ba22a652b
|
|
162
|
+
standard-performance (1.9.0) sha256=49483d31be448292951d80e5e67cdcb576c2502103c7b40aec6f1b6e9c88e3f2
|
|
163
|
+
unicode-display_width (3.2.0) sha256=0cdd96b5681a5949cdbc2c55e7b420facae74c4aaf9a9815eee1087cb1853c42
|
|
164
|
+
unicode-emoji (4.2.0) sha256=519e69150f75652e40bf736106cfbc8f0f73aa3fb6a65afe62fefa7f80b0f80f
|
|
165
|
+
|
|
166
|
+
BUNDLED WITH
|
|
167
|
+
4.0.16
|
data/LICENSE.txt
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Alan Ridlehoover and Fito von Zastrow
|
|
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,147 @@
|
|
|
1
|
+
<img src="assets/liberty.png"/>
|
|
2
|
+
|
|
3
|
+
# Liberty Web
|
|
4
|
+
|
|
5
|
+
[](https://badge.fury.io/rb/liberty-web)
|
|
6
|
+
[](https://github.com/first-try-software/liberty-web/actions/workflows/main.yml)
|
|
7
|
+
|
|
8
|
+
Liberty Web is the browser stack for a [Liberty](https://github.com/first-try-software/liberty) application: sessions, CSRF, host and origin checks, security headers, a redirect challenge, and a seam for the application's renderer.
|
|
9
|
+
|
|
10
|
+
Liberty itself answers requests and decides who is asking, and it stays free of cookies and templates so an API never carries them. Liberty Web adds what a browser needs, in one explicit call, and no more. It owns the shape of its configuration and the order of its middleware. The application owns every value, its authenticators, its templates, and its data.
|
|
11
|
+
|
|
12
|
+
Two things Liberty Web does not do, on purpose. It has nothing to say about templates: an endpoint calls whatever renderer the application owns, and the gem never sees it. And it reads no environment: the application reads its own settings and fills in a `Config`.
|
|
13
|
+
|
|
14
|
+
## Usage
|
|
15
|
+
|
|
16
|
+
### The stack
|
|
17
|
+
|
|
18
|
+
Build a `Liberty::Web::Config` with the application's values and hand it to `Liberty::Web.app`:
|
|
19
|
+
|
|
20
|
+
```ruby
|
|
21
|
+
# config.ru
|
|
22
|
+
require_relative "app/boot"
|
|
23
|
+
|
|
24
|
+
config = Liberty::Web::Config.new(
|
|
25
|
+
session_secrets: [ENV.fetch("SESSION_SECRET")], # one or more strings of at least 64 bytes; more than one rotates
|
|
26
|
+
permitted_hosts: ENV.fetch("PERMITTED_HOSTS").split(","),
|
|
27
|
+
secure_cookies: ENV["RACK_ENV"] == "production", # true needs the proxy to forward the scheme; see below
|
|
28
|
+
static: "public" # optional: a directory whose top-level entries become urls
|
|
29
|
+
)
|
|
30
|
+
|
|
31
|
+
run Liberty::Web.app(config)
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
Building a `Config` never raises, so a task that serves no pages can hold a half-filled one. `config.errors` names each problem in a sentence, and `Liberty::Web.app` raises `Liberty::Web::ConfigurationError` carrying all of them rather than build a stack that is wrong.
|
|
35
|
+
|
|
36
|
+
The call wraps `Liberty.rack_app` in this middleware, request first:
|
|
37
|
+
|
|
38
|
+
| Middleware | Why it is there |
|
|
39
|
+
|---|---|
|
|
40
|
+
| `Rack::Static` | Files under `static` answer before anything else runs |
|
|
41
|
+
| `Liberty::Web::RequestLogger` | rack-protection logs through `rack.logger`; without one it builds a DEBUG logger per request and chatters. The default is INFO on the request's own error stream; pass `logger:` to use your own |
|
|
42
|
+
| `Rack::MethodOverride` | Forms can only GET and POST; a hidden `_method` field gives a POST another verb. Only a POST is rewritten |
|
|
43
|
+
| `Rack::Session::Cookie` | Encrypted with `secrets:`, never `secret:`, so the legacy HMAC and Marshal path stays off. `SameSite=Lax`, `HttpOnly`, and `Secure` from `secure_cookies` |
|
|
44
|
+
| `Rack::Protection::HostAuthorization` | Before the CSRF check, so a request for the wrong host is told "Host not permitted" rather than a puzzling 403 |
|
|
45
|
+
| `Rack::Protection::AuthenticityToken` | The token from a form field or the `X-CSRF-Token` header; it needs the session above it |
|
|
46
|
+
| `Rack::Protection::HttpOrigin` | An unsafe request's `Origin` must be the application's own |
|
|
47
|
+
| `Rack::Protection::FrameOptions` | `SAMEORIGIN` on HTML |
|
|
48
|
+
| `Liberty::Web::ContentTypeOptions` | `nosniff` on every response. The obsolete `x-xss-protection` header is not sent |
|
|
49
|
+
|
|
50
|
+
To add middleware of your own, wrap the result in your own `Rack::Builder`.
|
|
51
|
+
|
|
52
|
+
### Endpoints
|
|
53
|
+
|
|
54
|
+
Inherit from `Liberty::Web::Endpoint` instead of `Liberty::Endpoint`. It adds three private helpers and nothing else:
|
|
55
|
+
|
|
56
|
+
```ruby
|
|
57
|
+
session # the rack session hash; wrap it in your own class if you want a vocabulary
|
|
58
|
+
csrf_token # the token the CSRF middleware accepts for this session, for a hidden field or a meta tag
|
|
59
|
+
location(path) # {"location" => path}; the status stays your own answer
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
```ruby
|
|
63
|
+
class Journal < Liberty::Web::Endpoint
|
|
64
|
+
responds_to :get, "/", authenticated_by: Authenticators::Session
|
|
65
|
+
|
|
66
|
+
def html
|
|
67
|
+
MyApp.renderer.render("journal", layout: :application, csrf_token: csrf_token, notes: notes)
|
|
68
|
+
end
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
class Logout < Liberty::Web::Endpoint
|
|
72
|
+
responds_to :delete, "/logout", authenticated_by: Authenticators::Session
|
|
73
|
+
|
|
74
|
+
def status
|
|
75
|
+
session.clear
|
|
76
|
+
303
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
def headers = location("/login")
|
|
80
|
+
end
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
The form that posts to a Liberty Web application carries the token in a hidden field, and a page that posts with JavaScript carries it in a `<meta name="csrf-token">` tag and sends it back as `X-CSRF-Token`. Both are the application's markup; the gem only checks them.
|
|
84
|
+
|
|
85
|
+
### Redirects as challenges
|
|
86
|
+
|
|
87
|
+
A Liberty authenticator names the endpoint class that answers when there is no principal. For a browser that is a redirect, and `Redirect.to` builds it:
|
|
88
|
+
|
|
89
|
+
```ruby
|
|
90
|
+
module Authenticators
|
|
91
|
+
class Session < Liberty::Authenticator
|
|
92
|
+
def principal = users.find(request.env["rack.session"][:user_id])
|
|
93
|
+
|
|
94
|
+
def challenge_endpoint_class = Liberty::Web::Redirect.to("/login")
|
|
95
|
+
end
|
|
96
|
+
end
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
`Redirect.to(path, status: 303)` returns one class per path and status, so Liberty sees the same class each time.
|
|
100
|
+
|
|
101
|
+
### Testing
|
|
102
|
+
|
|
103
|
+
`require "liberty/web/testing"` where your specs are set up. It is not loaded by `require "liberty/web"`.
|
|
104
|
+
|
|
105
|
+
```ruby
|
|
106
|
+
APP = Liberty::Web::Testing.lint(Rack::Builder.parse_file("config.ru")) # every request and response checked against the Rack spec
|
|
107
|
+
|
|
108
|
+
token = Liberty::Web::Testing.csrf_token_in(last_response.body) # the hidden field's value, for posting a form back
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
### Behind a proxy
|
|
112
|
+
|
|
113
|
+
With `secure_cookies: true`, the session middleware writes no cookie unless the request arrived over TLS, and the origin check compares against the scheme the request arrived with. Puma usually sits behind a proxy that terminates TLS, so Rack learns the scheme from `X-Forwarded-Proto`, or from a `Forwarded` header with `proto=https`. Caddy, Fly, Heroku, and Render add it for you. Nginx does not:
|
|
114
|
+
|
|
115
|
+
```nginx
|
|
116
|
+
proxy_set_header X-Forwarded-Proto $scheme;
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
Without it, the first symptom is a 403 on every form post.
|
|
120
|
+
|
|
121
|
+
## Installation
|
|
122
|
+
|
|
123
|
+
Install the gem and add to the application's Gemfile by executing:
|
|
124
|
+
|
|
125
|
+
$ bundle add liberty-web
|
|
126
|
+
|
|
127
|
+
If bundler is not being used to manage dependencies, install the gem by executing:
|
|
128
|
+
|
|
129
|
+
$ gem install liberty-web
|
|
130
|
+
|
|
131
|
+
## Development
|
|
132
|
+
|
|
133
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake` to run every gate: the specs, Standard, qlty, and flog. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
|
134
|
+
|
|
135
|
+
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 the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
|
136
|
+
|
|
137
|
+
## Contributing
|
|
138
|
+
|
|
139
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/first-try-software/liberty-web. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/first-try-software/liberty-web/blob/main/CODE_OF_CONDUCT.md).
|
|
140
|
+
|
|
141
|
+
## License
|
|
142
|
+
|
|
143
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
|
144
|
+
|
|
145
|
+
## Code of Conduct
|
|
146
|
+
|
|
147
|
+
Everyone interacting in the Liberty Web project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/first-try-software/liberty-web/blob/main/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "bundler/gem_tasks"
|
|
4
|
+
require "rspec/core/rake_task"
|
|
5
|
+
|
|
6
|
+
RSpec::Core::RakeTask.new(:spec)
|
|
7
|
+
|
|
8
|
+
require "standard/rake"
|
|
9
|
+
|
|
10
|
+
FLOG_LIMIT = 10.0
|
|
11
|
+
|
|
12
|
+
def qlty_bin
|
|
13
|
+
installed = File.expand_path("~/.qlty/bin/qlty")
|
|
14
|
+
ENV.fetch("QLTY_BIN") { File.exist?(installed) ? installed : "qlty" }
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
desc "Run qlty lint checks and report code smells"
|
|
18
|
+
task :qlty do
|
|
19
|
+
sh qlty_bin, "check", "--all", "--no-progress"
|
|
20
|
+
sh qlty_bin, "smells", "--all", "--quiet"
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
desc "Fail when the average flog score per method reaches #{FLOG_LIMIT}"
|
|
24
|
+
task :flog do
|
|
25
|
+
report = `bundle exec flog -m lib`
|
|
26
|
+
puts report
|
|
27
|
+
average = report[/^\s*([\d.]+): flog\/method average/, 1].to_f
|
|
28
|
+
abort("Quality gate failed: average flog #{average} is #{FLOG_LIMIT} or higher") if average >= FLOG_LIMIT
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
task default: %i[spec standard qlty flog]
|
data/assets/liberty.png
ADDED
|
Binary file
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Liberty
|
|
4
|
+
module Web
|
|
5
|
+
# What the stack needs, as a value the application fills in. Building one
|
|
6
|
+
# never raises, so a task that serves no pages can hold a half-filled one;
|
|
7
|
+
# `errors` says what is missing or wrong, and Liberty::Web.app refuses to
|
|
8
|
+
# build while there are any.
|
|
9
|
+
#
|
|
10
|
+
# session_secrets one or more strings of at least MINIMUM_SECRET_BYTES; more than one rotates
|
|
11
|
+
# permitted_hosts the host names the app answers for; never empty, which would mean anyone
|
|
12
|
+
# secure_cookies true when the site is served over TLS. The session cookie is then written
|
|
13
|
+
# only to a request Rack sees as TLS. Behind a proxy that terminates TLS,
|
|
14
|
+
# Rack learns that from X-Forwarded-Proto, so the proxy must send it
|
|
15
|
+
# static a directory whose top-level entries are served as urls, or nil for none
|
|
16
|
+
# logger a ::Logger that every request gets as rack.logger, which is where
|
|
17
|
+
# rack-protection warns; or nil for a Logger per request on that request's
|
|
18
|
+
# own rack.errors stream at INFO, which keeps rack-protection's debug
|
|
19
|
+
# chatter out of the log and lets a spec read the warnings from a StringIO
|
|
20
|
+
class Config < Data.define(:session_secrets, :permitted_hosts, :secure_cookies, :static, :logger)
|
|
21
|
+
MINIMUM_SECRET_BYTES = 64
|
|
22
|
+
|
|
23
|
+
def initialize(session_secrets: [], permitted_hosts: [], secure_cookies: nil, static: nil, logger: nil)
|
|
24
|
+
super(
|
|
25
|
+
session_secrets: Array(session_secrets),
|
|
26
|
+
permitted_hosts: Array(permitted_hosts),
|
|
27
|
+
secure_cookies: secure_cookies,
|
|
28
|
+
static: static,
|
|
29
|
+
logger: logger
|
|
30
|
+
)
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def errors
|
|
34
|
+
[secrets_error, hosts_error, cookies_error, static_error].compact
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def valid?
|
|
38
|
+
errors.empty?
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
private
|
|
42
|
+
|
|
43
|
+
def secrets_error
|
|
44
|
+
return "session_secrets must hold at least one secret" if session_secrets.empty?
|
|
45
|
+
return unless session_secrets.any? { |secret| secret.to_s.bytesize < MINIMUM_SECRET_BYTES }
|
|
46
|
+
|
|
47
|
+
"session_secrets must each be at least #{MINIMUM_SECRET_BYTES} bytes"
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
def hosts_error
|
|
51
|
+
"permitted_hosts must name at least one host" if permitted_hosts.empty?
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
def cookies_error
|
|
55
|
+
"secure_cookies must be true or false" unless [true, false].include?(secure_cookies)
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
def static_error
|
|
59
|
+
"static must be an existing directory" if static && !File.directory?(static)
|
|
60
|
+
end
|
|
61
|
+
end
|
|
62
|
+
end
|
|
63
|
+
end
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Liberty
|
|
4
|
+
module Web
|
|
5
|
+
# Tells the browser never to guess a response's type. This is the useful
|
|
6
|
+
# half of rack-protection's XSSHeader; the other half, x-xss-protection,
|
|
7
|
+
# is obsolete and is not sent.
|
|
8
|
+
class ContentTypeOptions
|
|
9
|
+
attr_reader :app
|
|
10
|
+
|
|
11
|
+
def initialize(app)
|
|
12
|
+
@app = app
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def call(env)
|
|
16
|
+
status, headers, body = app.call(env)
|
|
17
|
+
headers["x-content-type-options"] ||= "nosniff"
|
|
18
|
+
[status, headers, body]
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
end
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "rack/protection"
|
|
4
|
+
|
|
5
|
+
module Liberty
|
|
6
|
+
module Web
|
|
7
|
+
# A Liberty endpoint with what a browser endpoint reaches for. Three
|
|
8
|
+
# private helpers and nothing else: no lifecycle, no response object, and
|
|
9
|
+
# nothing about templates, which are the application's.
|
|
10
|
+
class Endpoint < Liberty::Endpoint
|
|
11
|
+
private
|
|
12
|
+
|
|
13
|
+
# The rack session hash. An application wraps it with its own class when
|
|
14
|
+
# it wants a vocabulary for what the cookie carries.
|
|
15
|
+
def session
|
|
16
|
+
request.env["rack.session"]
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
# The masked token the CSRF middleware accepts for this session, for a
|
|
20
|
+
# hidden field or a meta tag.
|
|
21
|
+
def csrf_token
|
|
22
|
+
Rack::Protection::AuthenticityToken.token(session)
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
# The headers of a redirect. The status stays the endpoint's own answer.
|
|
26
|
+
def location(path)
|
|
27
|
+
{"location" => path}
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
end
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Liberty
|
|
4
|
+
module Web
|
|
5
|
+
# The base for every error Liberty Web raises. A Liberty::Error too, so an
|
|
6
|
+
# application can rescue the framework as a whole.
|
|
7
|
+
Error = Class.new(Liberty::Error)
|
|
8
|
+
|
|
9
|
+
# Raised by Liberty::Web.app for a Config with errors.
|
|
10
|
+
ConfigurationError = Class.new(Error)
|
|
11
|
+
end
|
|
12
|
+
end
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Liberty
|
|
4
|
+
module Web
|
|
5
|
+
# Endpoint classes that only redirect, for an authenticator's challenge:
|
|
6
|
+
#
|
|
7
|
+
# def challenge_endpoint_class = Liberty::Web::Redirect.to("/login")
|
|
8
|
+
#
|
|
9
|
+
# One class per path and status, so Liberty sees the same class each time.
|
|
10
|
+
module Redirect
|
|
11
|
+
class << self
|
|
12
|
+
def to(path, status: 303)
|
|
13
|
+
classes[[path, status]] ||= build(path, status)
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
private
|
|
17
|
+
|
|
18
|
+
def classes
|
|
19
|
+
@classes ||= {}
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def build(path, status)
|
|
23
|
+
Class.new(Endpoint) do
|
|
24
|
+
define_method(:status) { status }
|
|
25
|
+
define_method(:headers) { location(path) }
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
end
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "logger"
|
|
4
|
+
|
|
5
|
+
module Liberty
|
|
6
|
+
module Web
|
|
7
|
+
# Gives every request a logger on rack.logger. rack-protection logs through
|
|
8
|
+
# it, and without one builds a DEBUG logger per request that chatters about
|
|
9
|
+
# every host check. The default is INFO on the request's own error stream,
|
|
10
|
+
# which is what lets a spec hand in a StringIO and read what was logged.
|
|
11
|
+
# An injected logger is handed to every request instead.
|
|
12
|
+
class RequestLogger
|
|
13
|
+
attr_reader :app, :logger
|
|
14
|
+
|
|
15
|
+
def initialize(app, logger = nil)
|
|
16
|
+
@app = app
|
|
17
|
+
@logger = logger
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def call(env)
|
|
21
|
+
env["rack.logger"] = logger || Logger.new(env["rack.errors"], level: Logger::INFO)
|
|
22
|
+
app.call(env)
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
end
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "rack"
|
|
4
|
+
require "rack/builder"
|
|
5
|
+
require "rack/static"
|
|
6
|
+
require "rack/method_override"
|
|
7
|
+
require "rack/session"
|
|
8
|
+
require "rack/protection"
|
|
9
|
+
require_relative "request_logger"
|
|
10
|
+
require_relative "content_type_options"
|
|
11
|
+
|
|
12
|
+
module Liberty
|
|
13
|
+
module Web
|
|
14
|
+
# The middleware around Liberty's application, request first:
|
|
15
|
+
#
|
|
16
|
+
# Rack::Static files answer before anything else runs
|
|
17
|
+
# RequestLogger a quiet logger for rack-protection, or the injected one
|
|
18
|
+
# Rack::MethodOverride forms can only GET and POST; _method gives a POST another verb
|
|
19
|
+
# Rack::Session::Cookie encrypted; `secrets:`, never `secret:`, keeps the legacy HMAC path off
|
|
20
|
+
# HostAuthorization before the CSRF check, so a bad host is told so plainly
|
|
21
|
+
# AuthenticityToken the token from a form field or X-CSRF-Token; needs the session above
|
|
22
|
+
# HttpOrigin an unsafe request's Origin must be the app's own
|
|
23
|
+
# FrameOptions SAMEORIGIN on HTML
|
|
24
|
+
# ContentTypeOptions nosniff on everything; the obsolete x-xss-protection is not sent
|
|
25
|
+
class Stack
|
|
26
|
+
attr_reader :config
|
|
27
|
+
|
|
28
|
+
def initialize(config)
|
|
29
|
+
@config = config
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def to_app
|
|
33
|
+
stack = self
|
|
34
|
+
Rack::Builder.new(Liberty.rack_app) do
|
|
35
|
+
use Rack::Static, urls: stack.static_urls, root: stack.config.static if stack.config.static
|
|
36
|
+
use RequestLogger, stack.config.logger
|
|
37
|
+
use Rack::MethodOverride
|
|
38
|
+
use Rack::Session::Cookie, **stack.session_options
|
|
39
|
+
use Rack::Protection::HostAuthorization, permitted_hosts: stack.config.permitted_hosts
|
|
40
|
+
use Rack::Protection::AuthenticityToken
|
|
41
|
+
use Rack::Protection::HttpOrigin
|
|
42
|
+
use Rack::Protection::FrameOptions
|
|
43
|
+
use ContentTypeOptions
|
|
44
|
+
end.to_app
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
def static_urls
|
|
48
|
+
Dir.children(config.static).sort.map { |name| "/#{name}" }
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
def session_options
|
|
52
|
+
{secrets: config.session_secrets, same_site: :lax, httponly: true, secure: config.secure_cookies}
|
|
53
|
+
end
|
|
54
|
+
end
|
|
55
|
+
end
|
|
56
|
+
end
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "rack/lint"
|
|
4
|
+
|
|
5
|
+
module Liberty
|
|
6
|
+
module Web
|
|
7
|
+
# For an application's own specs. Not loaded by `require "liberty/web"`;
|
|
8
|
+
# require "liberty/web/testing" where the tests are set up.
|
|
9
|
+
module Testing
|
|
10
|
+
TOKEN_FIELD = /name="authenticity_token" value="([^"]+)"/
|
|
11
|
+
|
|
12
|
+
class << self
|
|
13
|
+
# The app with every request and response checked against the Rack spec.
|
|
14
|
+
def lint(app)
|
|
15
|
+
Rack::Lint.new(app)
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
# The value of the hidden field a form carries, for posting it back.
|
|
19
|
+
def csrf_token_in(html)
|
|
20
|
+
html[TOKEN_FIELD, 1]
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
end
|
data/lib/liberty/web.rb
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "liberty"
|
|
4
|
+
require_relative "web/version"
|
|
5
|
+
require_relative "web/errors"
|
|
6
|
+
require_relative "web/config"
|
|
7
|
+
require_relative "web/stack"
|
|
8
|
+
require_relative "web/endpoint"
|
|
9
|
+
require_relative "web/redirect"
|
|
10
|
+
|
|
11
|
+
module Liberty
|
|
12
|
+
# The browser stack for a Liberty application: sessions, CSRF, host and
|
|
13
|
+
# origin checks, security headers, and a redirect challenge. Liberty itself
|
|
14
|
+
# stays free of all of it, so an API never carries cookies it did not ask
|
|
15
|
+
# for. Templates are the application's; the gem has no opinion about them.
|
|
16
|
+
#
|
|
17
|
+
# The application builds a Config with its values and hands it here:
|
|
18
|
+
#
|
|
19
|
+
# run Liberty::Web.app(config)
|
|
20
|
+
module Web
|
|
21
|
+
# Liberty's Rack application wrapped in the browser middleware, in the
|
|
22
|
+
# order a browser needs. Refuses a config with errors, naming them all.
|
|
23
|
+
def self.app(config)
|
|
24
|
+
raise ConfigurationError, "Liberty::Web.app cannot build: #{config.errors.join(". ")}." unless config.valid?
|
|
25
|
+
|
|
26
|
+
Stack.new(config).to_app
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
end
|
data/liberty-web.gemspec
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "lib/liberty/web/version"
|
|
4
|
+
|
|
5
|
+
Gem::Specification.new do |spec|
|
|
6
|
+
spec.name = "liberty-web"
|
|
7
|
+
spec.version = Liberty::Web::VERSION
|
|
8
|
+
spec.authors = ["Alan Ridlehoover", "Fito von Zastrow"]
|
|
9
|
+
spec.email = ["liberty@firsttry.software"]
|
|
10
|
+
|
|
11
|
+
spec.summary = "The browser stack for Liberty applications"
|
|
12
|
+
spec.description = "Sessions, CSRF, host and origin checks, security headers, redirects, and a renderer seam " \
|
|
13
|
+
"for Liberty applications that serve browsers. Liberty itself stays free of them."
|
|
14
|
+
spec.homepage = "https://github.com/first-try-software/liberty-web"
|
|
15
|
+
spec.license = "MIT"
|
|
16
|
+
spec.required_ruby_version = ">= 3.3.0"
|
|
17
|
+
|
|
18
|
+
spec.metadata["homepage_uri"] = spec.homepage
|
|
19
|
+
spec.metadata["source_code_uri"] = spec.homepage
|
|
20
|
+
spec.metadata["changelog_uri"] = "https://github.com/first-try-software/liberty-web/CHANGELOG.md"
|
|
21
|
+
|
|
22
|
+
spec.files = Dir.chdir(__dir__) do
|
|
23
|
+
`git ls-files -z`.split("\x0").reject do |f|
|
|
24
|
+
(f == __FILE__) || f.match(%r{\A(?:(?:bin|test|spec|features)/|\.(?:git|travis|circleci)|appveyor)})
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
spec.bindir = "exe"
|
|
28
|
+
spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
|
|
29
|
+
spec.require_paths = ["lib"]
|
|
30
|
+
|
|
31
|
+
spec.add_dependency "liberty", "~> 0.5"
|
|
32
|
+
spec.add_dependency "rack", "~> 3.1"
|
|
33
|
+
spec.add_dependency "rack-protection", "~> 4.2"
|
|
34
|
+
spec.add_dependency "rack-session", "~> 2.1"
|
|
35
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: liberty-web
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Alan Ridlehoover
|
|
8
|
+
- Fito von Zastrow
|
|
9
|
+
bindir: exe
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
|
12
|
+
dependencies:
|
|
13
|
+
- !ruby/object:Gem::Dependency
|
|
14
|
+
name: liberty
|
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
|
16
|
+
requirements:
|
|
17
|
+
- - "~>"
|
|
18
|
+
- !ruby/object:Gem::Version
|
|
19
|
+
version: '0.5'
|
|
20
|
+
type: :runtime
|
|
21
|
+
prerelease: false
|
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
23
|
+
requirements:
|
|
24
|
+
- - "~>"
|
|
25
|
+
- !ruby/object:Gem::Version
|
|
26
|
+
version: '0.5'
|
|
27
|
+
- !ruby/object:Gem::Dependency
|
|
28
|
+
name: rack
|
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
|
30
|
+
requirements:
|
|
31
|
+
- - "~>"
|
|
32
|
+
- !ruby/object:Gem::Version
|
|
33
|
+
version: '3.1'
|
|
34
|
+
type: :runtime
|
|
35
|
+
prerelease: false
|
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
37
|
+
requirements:
|
|
38
|
+
- - "~>"
|
|
39
|
+
- !ruby/object:Gem::Version
|
|
40
|
+
version: '3.1'
|
|
41
|
+
- !ruby/object:Gem::Dependency
|
|
42
|
+
name: rack-protection
|
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
|
44
|
+
requirements:
|
|
45
|
+
- - "~>"
|
|
46
|
+
- !ruby/object:Gem::Version
|
|
47
|
+
version: '4.2'
|
|
48
|
+
type: :runtime
|
|
49
|
+
prerelease: false
|
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
51
|
+
requirements:
|
|
52
|
+
- - "~>"
|
|
53
|
+
- !ruby/object:Gem::Version
|
|
54
|
+
version: '4.2'
|
|
55
|
+
- !ruby/object:Gem::Dependency
|
|
56
|
+
name: rack-session
|
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
|
58
|
+
requirements:
|
|
59
|
+
- - "~>"
|
|
60
|
+
- !ruby/object:Gem::Version
|
|
61
|
+
version: '2.1'
|
|
62
|
+
type: :runtime
|
|
63
|
+
prerelease: false
|
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
65
|
+
requirements:
|
|
66
|
+
- - "~>"
|
|
67
|
+
- !ruby/object:Gem::Version
|
|
68
|
+
version: '2.1'
|
|
69
|
+
description: Sessions, CSRF, host and origin checks, security headers, redirects,
|
|
70
|
+
and a renderer seam for Liberty applications that serve browsers. Liberty itself
|
|
71
|
+
stays free of them.
|
|
72
|
+
email:
|
|
73
|
+
- liberty@firsttry.software
|
|
74
|
+
executables: []
|
|
75
|
+
extensions: []
|
|
76
|
+
extra_rdoc_files: []
|
|
77
|
+
files:
|
|
78
|
+
- ".qlty/.gitignore"
|
|
79
|
+
- ".qlty/qlty.toml"
|
|
80
|
+
- ".rspec"
|
|
81
|
+
- ".ruby-version"
|
|
82
|
+
- ".standard.yml"
|
|
83
|
+
- CHANGELOG.md
|
|
84
|
+
- CODE_OF_CONDUCT.md
|
|
85
|
+
- Gemfile
|
|
86
|
+
- Gemfile.lock
|
|
87
|
+
- LICENSE.txt
|
|
88
|
+
- README.md
|
|
89
|
+
- Rakefile
|
|
90
|
+
- assets/liberty.png
|
|
91
|
+
- lib/liberty/web.rb
|
|
92
|
+
- lib/liberty/web/config.rb
|
|
93
|
+
- lib/liberty/web/content_type_options.rb
|
|
94
|
+
- lib/liberty/web/endpoint.rb
|
|
95
|
+
- lib/liberty/web/errors.rb
|
|
96
|
+
- lib/liberty/web/redirect.rb
|
|
97
|
+
- lib/liberty/web/request_logger.rb
|
|
98
|
+
- lib/liberty/web/stack.rb
|
|
99
|
+
- lib/liberty/web/testing.rb
|
|
100
|
+
- lib/liberty/web/version.rb
|
|
101
|
+
- liberty-web.gemspec
|
|
102
|
+
homepage: https://github.com/first-try-software/liberty-web
|
|
103
|
+
licenses:
|
|
104
|
+
- MIT
|
|
105
|
+
metadata:
|
|
106
|
+
homepage_uri: https://github.com/first-try-software/liberty-web
|
|
107
|
+
source_code_uri: https://github.com/first-try-software/liberty-web
|
|
108
|
+
changelog_uri: https://github.com/first-try-software/liberty-web/CHANGELOG.md
|
|
109
|
+
rdoc_options: []
|
|
110
|
+
require_paths:
|
|
111
|
+
- lib
|
|
112
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
113
|
+
requirements:
|
|
114
|
+
- - ">="
|
|
115
|
+
- !ruby/object:Gem::Version
|
|
116
|
+
version: 3.3.0
|
|
117
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
118
|
+
requirements:
|
|
119
|
+
- - ">="
|
|
120
|
+
- !ruby/object:Gem::Version
|
|
121
|
+
version: '0'
|
|
122
|
+
requirements: []
|
|
123
|
+
rubygems_version: 4.0.16
|
|
124
|
+
specification_version: 4
|
|
125
|
+
summary: The browser stack for Liberty applications
|
|
126
|
+
test_files: []
|