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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
//! Slack source: fetches messages from Slack channels using the Web API.
//!
//! This allows KeyHog to identify secrets leaked in chat history.
//! Requires a Slack API token (Bot or User) with `channels:history` and `groups:history` scopes.
use keyhog_core::{Chunk, ChunkMetadata, Source, SourceError};
use reqwest::blocking::Client;
use serde::Deserialize;
/// Scan Slack messages via the `conversations.history` API.
pub struct SlackSource {
token: String,
lookback_messages: usize,
/// Shared HTTP policy (proxy, insecure_tls, ua_suffix, timeout). Defaults
/// to `HttpClientConfig::default()` (env-var fallbacks honored). Set via
/// `with_http_config` so the CLI's `--proxy` / `--insecure` reach this
/// source. Without this every Slack API call would silently bypass the
/// configured corporate proxy and the operator'"'"'s Burp interception.
http: crate::http::HttpClientConfig,
}
impl SlackSource {
/// Create a new Slack source.
pub fn new(token: impl Into<String>) -> Self {
Self {
token: token.into(),
lookback_messages: 1000,
http: crate::http::HttpClientConfig {
ua_suffix: Some("slack".into()),
..Default::default()
},
}
}
/// Set how many messages to fetch per channel.
pub fn with_lookback(mut self, n: usize) -> Self {
self.lookback_messages = n;
self
}
/// Override the shared HTTP policy. Threads CLI `--proxy` / `--insecure`
/// into the Slack API client.
pub fn with_http_config(mut self, http: crate::http::HttpClientConfig) -> Self {
self.http = http;
self
}
}
impl Source for SlackSource {
fn name(&self) -> &str {
"slack"
}
fn chunks(&self) -> Box<dyn Iterator<Item = Result<Chunk, SourceError>> + '_> {
match self.collect_chunks() {
Ok(chunks) => Box::new(chunks.into_iter().map(Ok)),
Err(e) => Box::new(std::iter::once(Err(e))),
}
}
fn as_any(&self) -> &dyn std::any::Any {
self
}
}
#[derive(Deserialize)]
struct SlackResponse<T> {
ok: bool,
error: Option<String>,
#[serde(flatten)]
data: T,
}
#[derive(Deserialize)]
struct ConversationsList {
channels: Vec<Channel>,
}
#[derive(Deserialize)]
struct Channel {
id: String,
name: String,
}
#[derive(Deserialize)]
struct History {
messages: Vec<Message>,
}
#[derive(Deserialize)]
struct Message {
user: Option<String>,
text: String,
ts: String,
}
impl SlackSource {
fn collect_chunks(&self) -> Result<Vec<Chunk>, SourceError> {
let http = if self.http.timeout.is_none() {
let mut h = self.http.clone();
h.timeout = Some(crate::timeouts::HTTP_REQUEST);
h
} else {
self.http.clone()
};
let client = crate::http::blocking_client_builder(&http)
.map_err(SourceError::Other)?
.build()
.map_err(|e| SourceError::Other(format!("failed to build Slack client: {e}")))?;
let channels = self.list_channels(&client)?;
// Concurrent per-channel history fetch. Slack's tier-2 rate limit is
// 20+ requests/minute; cap parallelism at 8 to leave headroom for the
// burst budget. Was sequential - see audits/legendary-2026-04-26.
use rayon::prelude::*;
let pool = rayon::ThreadPoolBuilder::new()
.num_threads(8)
.build()
.map_err(|e| SourceError::Other(format!("rayon pool build: {e}")))?;
let per_channel: Vec<Result<Vec<Chunk>, SourceError>> = pool.install(|| {
channels
.par_iter()
.map(|channel| -> Result<Vec<Chunk>, SourceError> {
let messages = self.fetch_history(&client, &channel.id)?;
let mut channel_chunks = Vec::new();
let mut channel_buffer = String::new();
for msg in messages {
if let Some(user) = &msg.user {
channel_buffer
.push_str(&format!("\n[USER: {} TS: {}]\n", user, msg.ts));
}
channel_buffer.push_str(&msg.text);
channel_buffer.push('\n');
if channel_buffer.len() > 64 * 1024 {
channel_chunks.push(Chunk {
data: std::mem::take(&mut channel_buffer).into(),
metadata: ChunkMetadata {
base_offset: 0,
base_line: 0,
source_type: "slack".into(),
path: Some(format!("slack://#{}", channel.name)),
..Default::default()
},
});
}
}
if !channel_buffer.is_empty() {
channel_chunks.push(Chunk {
data: channel_buffer.into(),
metadata: ChunkMetadata {
base_offset: 0,
base_line: 0,
source_type: "slack".into(),
path: Some(format!("slack://#{}", channel.name)),
..Default::default()
},
});
}
Ok(channel_chunks)
})
.collect()
});
let mut chunks = Vec::new();
for result in per_channel {
chunks.extend(result?);
}
Ok(chunks)
}
fn list_channels(&self, client: &Client) -> Result<Vec<Channel>, SourceError> {
let resp: SlackResponse<ConversationsList> = client
.get("https://slack.com/api/conversations.list")
.bearer_auth(&self.token)
.query(&[("types", "public_channel,private_channel")])
.send()
.map_err(|e| SourceError::Other(e.to_string()))?
.json()
.map_err(|e| SourceError::Other(e.to_string()))?;
if !resp.ok {
// `resp.error` is omitted by some Slack API responses (rate
// limits return only HTTP status + headers; non-OK 200s with
// {"ok": false} sometimes lack the field). Map None to a
// descriptive marker rather than "" so the operator sees the
// shape of the failure ("invalid_auth" / "missing field" /
// "channel_not_found" are common values; "<no error field>"
// distinguishes a malformed response from one with an actual
// error code).
let error_code = resp.error.as_deref().unwrap_or("<no error field>");
return Err(SourceError::Other(format!("Slack API error: {error_code}")));
}
Ok(resp.data.channels)
}
fn fetch_history(
&self,
client: &Client,
channel_id: &str,
) -> Result<Vec<Message>, SourceError> {
let resp: SlackResponse<History> = client
.get("https://slack.com/api/conversations.history")
.bearer_auth(&self.token)
.query(&[
("channel", channel_id),
("limit", &self.lookback_messages.to_string()),
])
.send()
.map_err(|e| SourceError::Other(e.to_string()))?
.json()
.map_err(|e| SourceError::Other(e.to_string()))?;
if !resp.ok {
// `resp.error` is omitted by some Slack API responses (rate
// limits return only HTTP status + headers; non-OK 200s with
// {"ok": false} sometimes lack the field). Map None to a
// descriptive marker rather than "" so the operator sees the
// shape of the failure ("invalid_auth" / "missing field" /
// "channel_not_found" are common values; "<no error field>"
// distinguishes a malformed response from one with an actual
// error code).
let error_code = resp.error.as_deref().unwrap_or("<no error field>");
return Err(SourceError::Other(format!("Slack API error: {error_code}")));
}
Ok(resp.data.messages)
}
}