-
Notifications
You must be signed in to change notification settings - Fork 602
/
github_secret_scanning.rs
209 lines (180 loc) · 8.49 KB
/
github_secret_scanning.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
use crate::util::MockRequestExt;
use crate::{RequestHelper, TestApp};
use crates_io::util::token::HashedToken;
use crates_io::{models::ApiToken, schema::api_tokens};
use diesel::prelude::*;
use googletest::prelude::*;
use http::StatusCode;
use insta::assert_json_snapshot;
static URL: &str = "/api/github/secret-scanning/verify";
// Test request and signature from https://docs.github.com/en/developers/overview/secret-scanning-partner-program#create-a-secret-alert-service
static GITHUB_ALERT: &[u8] =
br#"[{"token":"some_token","type":"some_type","url":"some_url","source":"some_source"}]"#;
static GITHUB_PUBLIC_KEY_IDENTIFIER: &str =
"f9525bf080f75b3506ca1ead061add62b8633a346606dc5fe544e29231c6ee0d";
static GITHUB_PUBLIC_KEY_SIGNATURE: &str = "MEUCIFLZzeK++IhS+y276SRk2Pe5LfDrfvTXu6iwKKcFGCrvAiEAhHN2kDOhy2I6eGkOFmxNkOJ+L2y8oQ9A2T9GGJo6WJY=";
#[tokio::test(flavor = "multi_thread")]
async fn github_secret_alert_revokes_token() {
let (app, anon, user, token) = TestApp::init().with_token();
// Ensure no emails were sent up to this point
assert_eq!(app.as_inner().emails.mails_in_memory().unwrap().len(), 0);
// Ensure that the token currently exists in the database
app.db(|conn| {
let tokens: Vec<ApiToken> = assert_ok!(ApiToken::belonging_to(user.as_model())
.select(ApiToken::as_select())
.filter(api_tokens::revoked.eq(false))
.load(conn));
assert_that!(tokens, len(eq(1)));
assert_eq!(tokens[0].name, token.as_model().name);
});
// Set token to expected value in signed request
app.db(|conn| {
let hashed_token = HashedToken::hash("some_token");
diesel::update(api_tokens::table)
.set(api_tokens::token.eq(hashed_token))
.execute(conn)
.unwrap();
});
let mut request = anon.post_request(URL);
*request.body_mut() = GITHUB_ALERT.into();
request.header("GITHUB-PUBLIC-KEY-IDENTIFIER", GITHUB_PUBLIC_KEY_IDENTIFIER);
request.header("GITHUB-PUBLIC-KEY-SIGNATURE", GITHUB_PUBLIC_KEY_SIGNATURE);
let response = anon.run::<()>(request).await;
assert_eq!(response.status(), StatusCode::OK);
assert_json_snapshot!(response.json());
// Ensure that the token was revoked
app.db(|conn| {
let tokens: Vec<ApiToken> = assert_ok!(ApiToken::belonging_to(user.as_model())
.select(ApiToken::as_select())
.filter(api_tokens::revoked.eq(false))
.load(conn));
assert_that!(tokens, empty());
let tokens: Vec<ApiToken> = assert_ok!(ApiToken::belonging_to(user.as_model())
.select(ApiToken::as_select())
.filter(api_tokens::revoked.eq(true))
.load(conn));
assert_that!(tokens, len(eq(1)));
});
// Ensure exactly one email was sent
assert_eq!(app.as_inner().emails.mails_in_memory().unwrap().len(), 1);
}
#[tokio::test(flavor = "multi_thread")]
async fn github_secret_alert_for_revoked_token() {
let (app, anon, user, token) = TestApp::init().with_token();
// Ensure no emails were sent up to this point
assert_eq!(app.as_inner().emails.mails_in_memory().unwrap().len(), 0);
// Ensure that the token currently exists in the database
app.db(|conn| {
let tokens: Vec<ApiToken> = assert_ok!(ApiToken::belonging_to(user.as_model())
.select(ApiToken::as_select())
.filter(api_tokens::revoked.eq(false))
.load(conn));
assert_that!(tokens, len(eq(1)));
assert_eq!(tokens[0].name, token.as_model().name);
});
// Set token to expected value in signed request and revoke it
app.db(|conn| {
let hashed_token = HashedToken::hash("some_token");
diesel::update(api_tokens::table)
.set((
api_tokens::token.eq(hashed_token),
api_tokens::revoked.eq(true),
))
.execute(conn)
.unwrap();
});
let mut request = anon.post_request(URL);
*request.body_mut() = GITHUB_ALERT.into();
request.header("GITHUB-PUBLIC-KEY-IDENTIFIER", GITHUB_PUBLIC_KEY_IDENTIFIER);
request.header("GITHUB-PUBLIC-KEY-SIGNATURE", GITHUB_PUBLIC_KEY_SIGNATURE);
let response = anon.run::<()>(request).await;
assert_eq!(response.status(), StatusCode::OK);
assert_json_snapshot!(response.json());
// Ensure that the token is still revoked
app.db(|conn| {
let tokens: Vec<ApiToken> = assert_ok!(ApiToken::belonging_to(user.as_model())
.select(ApiToken::as_select())
.filter(api_tokens::revoked.eq(false))
.load(conn));
assert_that!(tokens, empty());
let tokens: Vec<ApiToken> = assert_ok!(ApiToken::belonging_to(user.as_model())
.select(ApiToken::as_select())
.filter(api_tokens::revoked.eq(true))
.load(conn));
assert_that!(tokens, len(eq(1)));
});
// Ensure still no emails were sent
assert_eq!(app.as_inner().emails.mails_in_memory().unwrap().len(), 0);
}
#[tokio::test(flavor = "multi_thread")]
async fn github_secret_alert_for_unknown_token() {
let (app, anon, user, token) = TestApp::init().with_token();
// Ensure no emails were sent up to this point
assert_eq!(app.as_inner().emails.mails_in_memory().unwrap().len(), 0);
// Ensure that the token currently exists in the database
app.db(|conn| {
let tokens: Vec<ApiToken> = assert_ok!(ApiToken::belonging_to(user.as_model())
.select(ApiToken::as_select())
.filter(api_tokens::revoked.eq(false))
.load(conn));
assert_that!(tokens, len(eq(1)));
assert_eq!(tokens[0].name, token.as_model().name);
});
let mut request = anon.post_request(URL);
*request.body_mut() = GITHUB_ALERT.into();
request.header("GITHUB-PUBLIC-KEY-IDENTIFIER", GITHUB_PUBLIC_KEY_IDENTIFIER);
request.header("GITHUB-PUBLIC-KEY-SIGNATURE", GITHUB_PUBLIC_KEY_SIGNATURE);
let response = anon.run::<()>(request).await;
assert_eq!(response.status(), StatusCode::OK);
assert_json_snapshot!(response.json());
// Ensure that the token was not revoked
app.db(|conn| {
let tokens: Vec<ApiToken> = assert_ok!(ApiToken::belonging_to(user.as_model())
.select(ApiToken::as_select())
.filter(api_tokens::revoked.eq(false))
.load(conn));
assert_that!(tokens, len(eq(1)));
assert_eq!(tokens[0].name, token.as_model().name);
});
// Ensure still no emails were sent
assert_eq!(app.as_inner().emails.mails_in_memory().unwrap().len(), 0);
}
#[tokio::test(flavor = "multi_thread")]
async fn github_secret_alert_invalid_signature_fails() {
let (_, anon) = TestApp::init().empty();
// No headers or request body
let request = anon.post_request(URL);
let response = anon.run::<()>(request).await;
assert_eq!(response.status(), StatusCode::BAD_REQUEST);
// Request body but no headers
let mut request = anon.post_request(URL);
*request.body_mut() = GITHUB_ALERT.into();
let response = anon.run::<()>(request).await;
assert_eq!(response.status(), StatusCode::BAD_REQUEST);
// Headers but no request body
let mut request = anon.post_request(URL);
request.header("GITHUB-PUBLIC-KEY-IDENTIFIER", GITHUB_PUBLIC_KEY_IDENTIFIER);
request.header("GITHUB-PUBLIC-KEY-SIGNATURE", GITHUB_PUBLIC_KEY_SIGNATURE);
let response = anon.run::<()>(request).await;
assert_eq!(response.status(), StatusCode::BAD_REQUEST);
// Request body but only key identifier header
let mut request = anon.post_request(URL);
*request.body_mut() = GITHUB_ALERT.into();
request.header("GITHUB-PUBLIC-KEY-IDENTIFIER", GITHUB_PUBLIC_KEY_IDENTIFIER);
let response = anon.run::<()>(request).await;
assert_eq!(response.status(), StatusCode::BAD_REQUEST);
// Invalid signature
let mut request = anon.post_request(URL);
*request.body_mut() = GITHUB_ALERT.into();
request.header("GITHUB-PUBLIC-KEY-IDENTIFIER", GITHUB_PUBLIC_KEY_IDENTIFIER);
request.header("GITHUB-PUBLIC-KEY-SIGNATURE", "bad signature");
let response = anon.run::<()>(request).await;
assert_eq!(response.status(), StatusCode::BAD_REQUEST);
// Invalid signature that is valid base64
let mut request = anon.post_request(URL);
*request.body_mut() = GITHUB_ALERT.into();
request.header("GITHUB-PUBLIC-KEY-IDENTIFIER", GITHUB_PUBLIC_KEY_IDENTIFIER);
request.header("GITHUB-PUBLIC-KEY-SIGNATURE", "YmFkIHNpZ25hdHVyZQ==");
let response = anon.run::<()>(request).await;
assert_eq!(response.status(), StatusCode::BAD_REQUEST);
}