Skip to content

Commit a1dfed9

Browse files
committed
handle trailing padding for extlib's Base64.decode_string
1 parent cd3b4b9 commit a1dfed9

File tree

1 file changed

+24
-9
lines changed

1 file changed

+24
-9
lines changed

lib/github.ml

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -74,15 +74,30 @@ let get_remote_config_json_url filename ?token req =
7474
end
7575

7676
let config_of_content_api_response response =
77-
match response.encoding with
78-
| "base64" ->
79-
Lwt.return
80-
@@ Notabot_j.config_of_string
81-
@@ Base64.decode_string
82-
@@ String.concat
83-
@@ String.split ~on:'\n'
84-
@@ response.content
85-
| e -> remote_config_error "unknown encoding format '%s'" e
77+
let decode_string_pad s =
78+
let rec strip_padding i =
79+
if i < 0 then ""
80+
else (
81+
match s.[i] with
82+
| '=' | '\n' | '\r' | '\t' | ' ' -> strip_padding (i - 1)
83+
| _ -> String.sub s ~pos:0 ~len:(i + 1)
84+
)
85+
in
86+
Base64.decode_string @@ strip_padding (String.length s - 1)
87+
in
88+
try%lwt
89+
match response.encoding with
90+
| "base64" ->
91+
Lwt.return
92+
@@ Notabot_j.config_of_string
93+
@@ decode_string_pad
94+
@@ String.concat
95+
@@ String.split_lines
96+
@@ response.content
97+
| e -> remote_config_error "unknown encoding format '%s'" e
98+
with
99+
| Base64.Invalid_char -> remote_config_error "unable to decode configuration file from base64"
100+
| Yojson.Json_error msg -> remote_config_error "unable to parse configuration file as valid JSON (%s)" msg
86101

87102
let load_config_json url =
88103
let headers = [ "Accept: application/vnd.github.v3+json" ] in

0 commit comments

Comments
 (0)