Skip to content

Commit b979c71

Browse files
tpjonasroussel
andauthored
fix: Require header to be a Map when decoding JWT (#69)
Even though the header's contents are not checked. Co-authored-by: Jonas Roussel <[email protected]>
1 parent 874eb13 commit b979c71

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

lib/src/jwt.dart

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,8 @@ class JWT {
210210
static JWT decode(String token) {
211211
try {
212212
final parts = token.split('.');
213-
final header = jsonBase64.decode(base64Padded(parts[0]));
213+
var header =
214+
jsonBase64.decode(base64Padded(parts[0])) as Map<String, dynamic>;
214215

215216
final payload =
216217
(jsonBase64.decode(base64Padded(parts[1])) as Map<String, dynamic>);
@@ -222,8 +223,8 @@ class JWT {
222223

223224
return JWT(
224225
payload,
225-
header: header is! Map<String, dynamic> ? null : header,
226-
audience: audience,
226+
header: header,
227+
audience: audiance,
227228
issuer: issuer,
228229
subject: subject,
229230
jwtId: jwtId,

test/header_test.dart

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,5 +147,18 @@ void main() {
147147
});
148148
});
149149
});
150+
151+
group('invalid header', () {
152+
test('invalid (non map) header should fail to decode', () {
153+
final token =
154+
'W10' + // base64 for `[]`, which can JSON decode but is not valid
155+
'.eyJmb28iOiJiYXIifQ' +
156+
'.'; // signature is not checked here
157+
158+
final jwt = JWT.tryDecode(token);
159+
160+
expect(jwt, isNull);
161+
});
162+
});
150163
});
151164
}

0 commit comments

Comments
 (0)