Skip to content

Commit

Permalink
Move static to public
Browse files Browse the repository at this point in the history
Because QR codes generate in the folder, it is not static. The name
"public" is a rails convention for this folder that makes sense to me.
  • Loading branch information
DanGould committed Nov 11, 2022
1 parent 301183f commit 9aa1361
Show file tree
Hide file tree
Showing 22 changed files with 9 additions and 9 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/target
tests/compose/nginx/ssl/localhost-key.pem
tests/compose/nginx/ssl/localhost.pem
static/qr_codes/*.png
public/qr_codes/*.png
Binary file added public/.DS_Store
Binary file not shown.
Binary file added public/favicon.ico
Binary file not shown.
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes.
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes
File renamed without changes
File renamed without changes.
16 changes: 8 additions & 8 deletions src/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ use qrcode_generator::QrCodeEcc;
use crate::scheduler::{self, ScheduledPayJoin, Scheduler, SchedulerError};

#[cfg(not(feature = "test_paths"))]
const STATIC_DIR: &str = "/usr/share/nolooking/static";
const PUBLIC_DIR: &str = "/usr/share/nolooking/public";

#[cfg(feature = "test_paths")]
const STATIC_DIR: &str = "static";
const PUBLIC_DIR: &str = "public";

/// Create QR code and save to `STATIC_DIR/qr_codes/<name>.png`
/// Create QR code and save to `PUBLIC_DIR/qr_codes/<name>.png`
fn create_qr_code(qr_string: &str, name: &str) {
let filename = format!("{}/qr_codes/{}.png", STATIC_DIR, name);
let filename = format!("{}/qr_codes/{}.png", PUBLIC_DIR, name);
qrcode_generator::to_png_to_file(qr_string, QrCodeEcc::Low, 512, filename.clone())
.expect(&format!("Saved QR code: {}", filename));
}
Expand Down Expand Up @@ -50,7 +50,7 @@ async fn handle_web_req(
(&Method::GET, "/") => handle_index().await,
(&Method::POST, "/pj") => handle_pj(scheduler, req).await,
(&Method::POST, "/schedule") => handle_schedule(scheduler, endpoint, req).await,
(&Method::GET, path) => handle_static(path).await,
(&Method::GET, path) => serve_public_file(path).await,
_ => handle_404().await,
};

Expand All @@ -67,15 +67,15 @@ async fn handle_404() -> Result<Response<Body>, HttpError> {
}

async fn handle_index() -> Result<Response<Body>, HttpError> {
let index = std::fs::read(Path::new(STATIC_DIR).join("index.html")).expect("can't open index");
let index = std::fs::read(Path::new(PUBLIC_DIR).join("index.html")).expect("can't open index");
Ok(Response::new(Body::from(index)))
}

async fn handle_static(path: &str) -> Result<Response<Body>, HttpError> {
async fn serve_public_file(path: &str) -> Result<Response<Body>, HttpError> {
// A path argument to PathBuf::join(&self, path) with a leading slash
// is treated as an absolute path, so we strip it in preparation.
let directory_traversal_vulnerable_path = &path[("/".len())..];
match std::fs::read(Path::new(STATIC_DIR).join(directory_traversal_vulnerable_path)) {
match std::fs::read(Path::new(PUBLIC_DIR).join(directory_traversal_vulnerable_path)) {
Ok(file) => Response::builder()
.status(200)
.header("Cache-Control", "max-age=604800")
Expand Down

0 comments on commit 9aa1361

Please sign in to comment.