Skip to content

Commit 164f532

Browse files
committed
expose CSP check in API, ensure it gets updated on full cron
1 parent 0d2a8ba commit 164f532

File tree

4 files changed

+18
-6
lines changed

4 files changed

+18
-6
lines changed

README.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ The image supports the use of the following environment variables:
1616

1717
- `CRON`: (Optional) If set when running the app a cron cycle is performed.
1818
Set it to `CRON=FULL` to run a full cron (once a day).
19-
use this app to hammer the listed instances. Any string works, for example
20-
one generated using `openssl rand -hex 32`
2119
- `GEOIP_MMDB`: path to the GeoIP database, in MaxMind format
2220
- `ROCKET_DATABASES`: [database dict](https://api.rocket.rs/v0.4/rocket_contrib/databases/index.html#environment-variables)
2321
for Diesel SQLite library integration into Rocket

src/main.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,13 +269,14 @@ async fn report(
269269
}
270270

271271
#[get(
272-
"/api?<top>&<attachments>&<country>&<https>&<https_redirect>&<version>&<min_uptime>&<min_rating>",
272+
"/api?<top>&<attachments>&<country>&<csp_header>&<https>&<https_redirect>&<version>&<min_uptime>&<min_rating>",
273273
format = "json"
274274
)]
275275
async fn api(
276276
top: Option<NonZeroU8>,
277277
attachments: Option<bool>,
278278
country: Option<String>,
279+
csp_header: Option<bool>,
279280
https: Option<bool>,
280281
https_redirect: Option<bool>,
281282
version: Option<String>,
@@ -300,6 +301,9 @@ async fn api(
300301
let is_country_set = country.is_some();
301302
let country = country.unwrap_or_default();
302303

304+
let is_csp_header_set = csp_header.is_some();
305+
let csp_header = csp_header.unwrap_or(false);
306+
303307
let is_https_set = https.is_some();
304308
let https = https.unwrap_or(false);
305309

@@ -321,6 +325,7 @@ async fn api(
321325
for instance in &*cache.instances.read().unwrap() {
322326
if (is_attachments_set && instance.attachments != attachments)
323327
|| (is_country_set && instance.country_id != country)
328+
|| (is_csp_header_set && instance.csp_header != csp_header)
324329
|| (is_https_set && instance.https != https)
325330
|| (is_https_redirect_set && instance.https_redirect != https_redirect)
326331
|| (is_version_set && !instance.version.starts_with(&version))

src/tasks.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ pub async fn check_full(rocket: Rocket<Build>) {
5151
{
5252
match diesel::delete(instances.filter(id.eq(result.instance.id))).execute(&conn)
5353
{
54-
Ok(_) => println!(" removed the instance, due to: {message}"),
54+
Ok(_) => print!(" removed the instance, due to: {message}"),
5555
Err(e) => {
5656
println!(" error removing the instance: {e:?}");
5757
}
@@ -80,6 +80,7 @@ pub async fn check_full(rocket: Rocket<Build>) {
8080
version.eq(updated_instance.version),
8181
https.eq(updated_instance.https),
8282
https_redirect.eq(updated_instance.https_redirect),
83+
csp_header.eq(updated_instance.csp_header),
8384
attachments.eq(updated_instance.attachments),
8485
country_id.eq(updated_instance.country_id),
8586
)),
@@ -162,6 +163,11 @@ async fn check_instance(instance: Instance) -> InstanceCheckResult {
162163
format!("{:?}", instance.https_redirect.clone()),
163164
String::new(),
164165
),
166+
(
167+
"csp_header",
168+
format!("{:?}", instance.csp_header.clone()),
169+
String::new(),
170+
),
165171
(
166172
"attachments",
167173
format!("{:?}", instance.attachments.clone()),
@@ -180,8 +186,9 @@ async fn check_instance(instance: Instance) -> InstanceCheckResult {
180186
instance_options[0].2 = privatebin.instance.version.clone();
181187
instance_options[1].2 = format!("{:?}", privatebin.instance.https.clone());
182188
instance_options[2].2 = format!("{:?}", privatebin.instance.https_redirect.clone());
183-
instance_options[3].2 = format!("{:?}", privatebin.instance.attachments.clone());
184-
instance_options[4].2 = privatebin.instance.country_id.clone();
189+
instance_options[3].2 = format!("{:?}", privatebin.instance.csp_header.clone());
190+
instance_options[4].2 = format!("{:?}", privatebin.instance.attachments.clone());
191+
instance_options[5].2 = privatebin.instance.country_id.clone();
185192
let elapsed = timer.elapsed();
186193
let timer = Instant::now();
187194
if instance_options.iter().any(|x| x.1 != x.2) {

templates/about.html.tera

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,8 @@ $ curl --header "Accept: application/json" https://privatebin.info/directory/api
8484
<dd>Boolean (true or false), unset by default. Only return instances that offer attachment upload in their web UI - third party clients can always upload attachments.</dd>
8585
<dt>country</dt>
8686
<dd>ISO 3166-1 alpha-2 country code, unset by default. Only return instances of that country. Note the limitations of this type of lookup, as explained above.</dd>
87+
<dt>csp_header</dt>
88+
<dd>Boolean (true or false), unset by default. Only return instances that set the currently recommend HTTP <code>Content-Security-Policy</code> (<a href="https://content-security-policy.com/">CSP</a>) header (see above).</dd>
8789
<dt>https</dt>
8890
<dd>Boolean (true or false), unset by default. Only return instances that offer HTTPS.</dd>
8991
<dt>https_redirect</dt>

0 commit comments

Comments
 (0)