From d24bd6798609ae356db308d65577e99aad0cf432 Mon Sep 17 00:00:00 2001 From: Keno Fischer Date: Fri, 24 Nov 2023 08:25:55 -0500 Subject: [PATCH] Give a better error message if token contains newlines (#221) I constructed a token as `OAuth2(read("token", String))`, but my token file had a trailing newline. GitHub stops parsing headers after an empty line, so depending on where the header ended up in the list of headers, this was causing all sorts of strange errors. Detect this situation and throw an intelligent error in GitHub.jl instead. --- src/utils/auth.jl | 14 ++++++++++++++ test/auth_tests.jl | 3 +++ 2 files changed, 17 insertions(+) diff --git a/src/utils/auth.jl b/src/utils/auth.jl index 15e3c92..3fa6744 100644 --- a/src/utils/auth.jl +++ b/src/utils/auth.jl @@ -7,6 +7,13 @@ abstract type Authorization end # TODO: SecureString on 0.7 struct OAuth2 <: Authorization token::String + function OAuth2(token) + token = convert(String, token) + if !all(c->isascii(c) && !isspace(c), token) + throw(ArgumentError("token `$token` has invalid whitespace or non-ascii character.")) + end + new(token) + end end struct UsernamePassAuth <: Authorization @@ -18,6 +25,13 @@ struct AnonymousAuth <: Authorization end struct JWTAuth <: Authorization JWT::String + function JWTAuth(token) + token = convert(String, token) + if !all(c->isascii(c) && !isspace(c), token) + throw(ArgumentError("ArgumentError token `$token` has invalid whitespace or non-ascii character.")) + end + new(token) + end end #################### diff --git a/test/auth_tests.jl b/test/auth_tests.jl index 27876d0..e6d07cc 100644 --- a/test/auth_tests.jl +++ b/test/auth_tests.jl @@ -21,3 +21,6 @@ auth2 = GitHub.JWTAuth(1234, key; iat = DateTime("2016-9-15T14:00")) # test to make sure things don't break. @test auth.JWT == correct_jwt @test auth2.JWT == correct_jwt + +@test_throws ArgumentError GitHub.OAuth2("ghp_\n") +@test_throws ArgumentError GitHub.JWTAuth("ghp_\n")