Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Crystal 1.0 support #1445

Merged
merged 2 commits into from
Mar 31, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .crystal-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.36.1
1.0.0
28 changes: 22 additions & 6 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,17 @@ on:

jobs:
check_format:
strategy:
fail-fast: false
matrix:
crystal_version:
- 0.36.1
- 1.0.0
experimental:
- false
runs-on: ubuntu-latest
container:
image: crystallang/crystal:0.36.1
continue-on-error: ${{ matrix.experimental }}
container: crystallang/crystal:${{ matrix.crystal_version }}-alpine
steps:
- uses: actions/checkout@v1
- name: Install shards
Expand All @@ -20,17 +28,25 @@ jobs:
- name: Lint
run: ./bin/ameba
specs:
strategy:
fail-fast: false
matrix:
crystal_version:
- 0.36.1
- 1.0.0
experimental:
- false
runs-on: ubuntu-latest
container:
image: crystallang/crystal:0.36.1
continue-on-error: ${{ matrix.experimental }}
container: crystallang/crystal:${{ matrix.crystal_version }}-alpine
steps:
- uses: actions/checkout@v2
- name: Install shards
run: shards install
- name: Cache Crystal
uses: actions/cache@v1
with:
path: ~/.cache/crystal
key: ${{ runner.os }}-crystal
- name: Install shards
run: shards install
- name: Run tests
run: crystal spec
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM crystallang/crystal:0.36.1
FROM crystallang/crystal:1.0.0
WORKDIR /data

RUN apt-get update && \
Expand Down
24 changes: 12 additions & 12 deletions shard.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: lucky
version: 0.26.0

crystal: 0.36.1
crystal: ">=0.36.1, < 2.0.0"

authors:
- Paul Smith <[email protected]>
Expand Down Expand Up @@ -33,34 +33,34 @@ targets:
dependencies:
lucky_cli:
github: luckyframework/lucky_cli
version: ~> 0.26.0
branch: master
habitat:
github: luckyframework/habitat
version: ~> 0.4.4
branch: master
wordsmith:
github: luckyframework/wordsmith
version: ~> 0.2.0
branch: master
avram:
github: luckyframework/avram
version: ~> 0.19.0
branch: master
lucky_router:
github: luckyframework/lucky_router
version: ~> 0.4.0
branch: master
shell-table:
github: luckyframework/shell-table.cr
branch: refactor/setter
branch: master
cry:
github: luckyframework/cry
version: ~> 0.4.0
branch: master
exception_page:
github: crystal-loot/exception_page
version: ~> 0.1.0
github: Sija/exception_page
branch: crystal-1.0
dexter:
github: luckyframework/dexter
version: ~> 0.3.2
branch: master
pulsar:
github: luckyframework/pulsar
version: ~> 0.2.0
branch: master

development_dependencies:
ameba:
Expand Down
21 changes: 15 additions & 6 deletions spec/lucky/cookies/cookie_jar_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ describe Lucky::CookieJar do
end

it "sets and gets raw HTTP::Cookie object with indifferent access" do
value = "Nestle Tollhouse"
value = "Nestle_Tollhouse"
jar = Lucky::CookieJar.empty_jar

jar.set_raw("cookie", value)
jar.set_raw(:symbol, "symbol value")
jar.set_raw(:symbol, "symbol_value")

jar.get_raw(:cookie).should be_a(HTTP::Cookie)
jar.get_raw("symbol").value.should eq("symbol value")
jar.get_raw("symbol").value.should eq("symbol_value")
jar.get_raw(:cookie).value.should eq(value)
jar.get_raw("cookie").value.should eq(value)
jar.get_raw?(:cookie).not_nil!.value.should eq(value)
Expand All @@ -30,6 +30,15 @@ describe Lucky::CookieJar do
jar.get_raw?("missing").should be_nil
end

it "raises a nicer error for invalid cookie values" do
value = "Double Chocolate"
jar = Lucky::CookieJar.empty_jar

expect_raises(Lucky::InvalidCookieValueError, "Cookie value for 'cookie' is invalid") do
jar.set_raw("cookie", value)
end
end

it "raises CookieNotFoundError when getting a raw cookie that doesn't exist" do
jar = Lucky::CookieJar.empty_jar

Expand Down Expand Up @@ -80,7 +89,7 @@ describe Lucky::CookieJar do
message = jar.get_raw(:message)
message.http_only.should be_true
message.expires.should be_nil
message.path.should eq "/"
message.path.should be_nil
message.domain.should be_nil
message.secure.should be_false
end
Expand Down Expand Up @@ -117,7 +126,7 @@ describe Lucky::CookieJar do
it "raises an error if the cookie is > 4096 bytes" do
expect_raises(Lucky::CookieOverflowError) do
jar = Lucky::CookieJar.empty_jar
jar.set_raw(:overflow, "x" * (4097 - 27)) # "overflow=x...x; path=/; HttpOnly",
jar.set_raw(:overflow, "x" * 4097) # "overflow=x...x; HttpOnly",
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure why there was maths here, but it wasn't throwing an exception, so I just made it the raw number because "x" * 4097 is 4097 bytes

end
end
end
Expand Down Expand Up @@ -196,7 +205,7 @@ describe Lucky::CookieJar do
headers["Cookie"] = "name=Rick%20James"

jar = Lucky::CookieJar.from_request_cookies(
HTTP::Cookies.from_headers(headers))
HTTP::Cookies.from_client_headers(headers))

jar.clear do |cookie|
cookie.path("/")
Expand Down
4 changes: 3 additions & 1 deletion src/lucky/cookies/cookie_jar.cr
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
class Lucky::CookieJar
MAX_COOKIE_SIZE = 4096
LUCKY_ENCRYPTION_PREFIX = Base64.encode("lucky") + "--"
LUCKY_ENCRYPTION_PREFIX = Base64.strict_encode("lucky") + "--"
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Base64.encode adds a newline at the end, but newline characters are invalid cookie values.

alias Key = String | Symbol
private property cookies
private property set_cookies
Expand Down Expand Up @@ -129,6 +129,8 @@ class Lucky::CookieJar
raise Lucky::CookieOverflowError.new("size of '#{key}' cookie is too big")
end
cookies[key.to_s] = set_cookies[key.to_s] = raw_cookie
rescue IO::Error
raise InvalidCookieValueError.new(key)
end

private def encrypt(raw_value : String) : String
Expand Down
21 changes: 21 additions & 0 deletions src/lucky/errors.cr
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,27 @@ module Lucky
end
end

# Crystal raises `Invalid cookie value (IO::Error)` by default.
# This provides a nicer error
class InvalidCookieValueError < Error
getter :key

def initialize(@key : String | Symbol)
end

def message : String
<<-ERROR
Cookie value for '#{key}' is invalid.

Be sure the value does not contain any blank characters,
comma, double quote, semicolon, or double backslash.

See https://tools.ietf.org/html/rfc6265#section-4.1.1 for valid
characters
ERROR
end
end

class InvalidSignatureError < Error
end

Expand Down