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

Use randomly generated delimiter for multiline env variables #26

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ version = "0.1.4"
[deps]
JSON = "682c06a0-de6a-54ab-a142-c8b1cf79cde6"
Logging = "56ddb016-857b-54e1-b83d-db4d58db5568"
UUIDs = "cf7118a7-6976-5b1a-9a39-7adc72f591a4"

[compat]
JSON = "0.21"
Expand Down
18 changes: 18 additions & 0 deletions THIRD_PARTY_NOTICE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Third Party Notices and Information

Parts of this software have been derived from other open source software.
Find their full licence information below.

- [`@actions/core`](https://github.com/actions/toolkit/tree/main/packages/core)

## [`@actions/core`](https://github.com/actions/toolkit/tree/main/packages/core)

> The MIT License (MIT)
>
>Copyright 2019 GitHub
>
> Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
>
> The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
>
> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
14 changes: 10 additions & 4 deletions src/GitHubActions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ using Logging: Logging, AbstractLogger, Debug, Info, Warn, Error

using JSON: json

using UUIDs: uuid4

const CMD_MARKER = "::"

"""
Expand Down Expand Up @@ -187,11 +189,15 @@ Set environment variable `k` to value `v`.
"""
function set_env(k, v)
val = cmd_value(v)

delimiter = "ghadelimiter_$(uuid4())"

# Safety precaution in case UUID generation is exploitable
!occursin(string(delimiter), val) || (set_failed("value of environment variable must not contain the delimiter $delimiter"); exit())
!occursin(string(delimiter), k) || (set_failed("name of environment variable must not contain the delimiter $delimiter"); exit())

ENV[k] = val
delimiter = "EOF"
while occursin(delimiter, val)
delimiter *= "EOF"
end

add_to_file("GITHUB_ENV", join(["$k<<$delimiter", val, delimiter], "\n"))
end

Expand Down
46 changes: 32 additions & 14 deletions test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
using Logging: with_logger
using UUIDs: UUID, uuid4
using Test: @test, @testset, @test_throws

using GitHubActions
using SimpleMock: called_once_with, mock
using SimpleMock: called_once_with, mock, Mock
using Suppressor: @capture_out

const GHA = GitHubActions
Expand Down Expand Up @@ -61,19 +62,36 @@ const GHA = GitHubActions
@test (@capture_out group(() -> println("!"), "a")) == "::group::a\n!\n::endgroup::\n"

mktemp() do file, io
withenv("GITHUB_ENV" => file, map(c -> string(c) => nothing, 'a':'z')...) do
set_env("a", "b")
@test ENV["a"] == "b"
@test read(file, String) == "a<<EOF\nb\nEOF\n"
set_env("b", "fooEOFbar")
@test read(file, String) == "a<<EOF\nb\nEOF\nb<<EOFEOF\nfooEOFbar\nEOFEOF\n"
rm(file)
set_env("c", [])
@test ENV["c"] == "[]"
@test read(file, String) == "c<<EOF\n[]\nEOF\n"
set_env("d", nothing)
@test ENV["d"] == ""
@test read(file, String) == "c<<EOF\n[]\nEOF\nd<<EOF\n\nEOF\n"
uuid = UUID("9b1deb4d-3b7d-4bad-9bdd-2b0d7b3dcb6d")
delimiter = "ghadelimiter_$uuid"
mock((uuid4) => Mock(uuid)) do _
withenv("GITHUB_ENV" => file, map(c -> string(c) => nothing, 'a':'z')...) do
set_env("a", "b")
@test ENV["a"] == "b"
@test read(file, String) == "a<<$delimiter\nb\n$delimiter\n"

mock(exit) do _
mock(set_failed) do sf
set_env("b", "foo$(delimiter)bar")
@test called_once_with(sf, "value of environment variable must not contain the delimiter $delimiter")
end
end

mock(exit) do _
mock(set_failed) do sf
set_env("b$(delimiter)", "c")
@test called_once_with(sf, "name of environment variable must not contain the delimiter $delimiter")
end
end

rm(file)
set_env("c", [])
@test ENV["c"] == "[]"
@test read(file, String) == "c<<$delimiter\n[]\n$delimiter\n"
set_env("d", nothing)
@test ENV["d"] == ""
@test read(file, String) == "c<<$delimiter\n[]\n$delimiter\nd<<$delimiter\n\n$delimiter\n"
end
end
end

Expand Down