Skip to content

Commit

Permalink
refactor(backend): refactor bound check and remove hardcoded macro
Browse files Browse the repository at this point in the history
  • Loading branch information
Eason0729 committed Jul 10, 2024
1 parent 1d24940 commit 48083db
Show file tree
Hide file tree
Showing 40 changed files with 2,021 additions and 245 deletions.
1,832 changes: 1,749 additions & 83 deletions Cargo.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[workspace]
resolver = "2"
members = [
# "frontend",
"frontend",
"backend",
"judger",
# "testsuit",
Expand Down
2 changes: 1 addition & 1 deletion backend/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ features = [
"judger",
"transport",
"serde",
"extra_trait",
"extra_trait"
]

[dependencies.postcard]
Expand Down
13 changes: 8 additions & 5 deletions backend/src/init/config.rs → backend/src/config.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
use std::{path::PathBuf, str::FromStr};

use super::Error;
use crate::server;
use crate::server::InitError;
use ip_network::IpNetwork;
use serde::{Deserialize, Serialize};
use tokio::{fs, io::AsyncReadExt};

pub use crate::server::PACKAGE_NAME;

lazy_static::lazy_static! {
pub static ref CONFIG_PATH: PathBuf=PathBuf::from_str(
&std::env::var("CONFIG_PATH").unwrap_or("config.toml".to_string()))
Expand Down Expand Up @@ -130,7 +133,7 @@ impl Default for Imgur {
}
}

pub async fn init() -> super::Result<GlobalConfig> {
pub async fn init() -> server::Result<GlobalConfig> {
let config_path = CONFIG_PATH.as_path();
if fs::metadata(config_path).await.is_ok() {
let mut buf = Vec::new();
Expand All @@ -140,10 +143,10 @@ pub async fn init() -> super::Result<GlobalConfig> {
config
.read_to_end(&mut buf)
.await
.map_err(Error::ConfigRead)?;
.map_err(InitError::ConfigRead)?;
let config =
std::str::from_utf8(&buf).expect("Config file may container non-utf8 character");
let config: GlobalConfig = toml::from_str(config).map_err(Error::ConfigParse)?;
let config: GlobalConfig = toml::from_str(config).map_err(InitError::ConfigParse)?;
Ok(config)
} else {
println!(
Expand All @@ -155,7 +158,7 @@ pub async fn init() -> super::Result<GlobalConfig> {
let config_txt = toml::to_string(&config).unwrap();
fs::write(config_path, config_txt)
.await
.map_err(Error::ConfigWrite)?;
.map_err(InitError::ConfigWrite)?;

println!(
"Config generated, please edit {:?} before restart",
Expand Down
2 changes: 1 addition & 1 deletion backend/src/controller/crypto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use rand::{rngs::OsRng, Rng};
use serde::{de::DeserializeOwned, Serialize};
use tracing::Span;

use crate::init::config::GlobalConfig;
use crate::config::GlobalConfig;
use base64::{engine::general_purpose::URL_SAFE_NO_PAD, Engine};
use blake2::{Blake2b512, Digest};

Expand Down
2 changes: 1 addition & 1 deletion backend/src/controller/imgur.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::collections::HashMap;
use reqwest::{multipart, Client};
use tracing::instrument;

use crate::{init::config, report_internal};
use crate::{config, report_internal};

#[derive(Debug, thiserror::Error)]
pub enum Error {
Expand Down
2 changes: 1 addition & 1 deletion backend/src/controller/judger/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ use std::{
use tokio_stream::StreamExt;

use crate::{
init::{config, logger::PACKAGE_NAME},
report_internal, TonicStream,
{config, config::PACKAGE_NAME},
};
use grpc::backend::StateCode as BackendCode;
use opentelemetry::{global, metrics::ObservableGauge};
Expand Down
2 changes: 1 addition & 1 deletion backend/src/controller/judger/route/direct.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use super::{ConnectionDetail, Error, Routable, RouteStatus};
use crate::init::config::Judger;
use crate::config::Judger;
use tonic::transport::Uri;

/// Upstream source for static(only emit once)
Expand Down
2 changes: 1 addition & 1 deletion backend/src/controller/judger/route/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use tonic::{service::Interceptor, *};
use tracing::{debug_span, instrument, span, Instrument, Level, Span};
use uuid::Uuid;

use crate::init::config::{self, Judger as JudgerConfig};
use crate::config::{self, Judger as JudgerConfig};
use grpc::judger::{judger_client::*, *};

// TODO: add tracing
Expand Down
2 changes: 1 addition & 1 deletion backend/src/controller/judger/route/swarm.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::{collections::HashSet, net::IpAddr, time::Duration};

use super::{ConnectionDetail, Error};
use crate::init::config;
use crate::config;
use hickory_resolver::TokioAsyncResolver;

use super::{Routable, RouteStatus};
Expand Down
2 changes: 1 addition & 1 deletion backend/src/controller/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use opentelemetry::{
};
use opentelemetry_sdk::metrics::MeterProvider as SdkMeterProvider;

use crate::init::logger::PACKAGE_NAME;
use crate::config::PACKAGE_NAME;

macro_rules! impl_metrics {
($n:expr) => {
Expand Down
12 changes: 5 additions & 7 deletions backend/src/endpoint/announcement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,9 @@ impl Announcement for ArcServer {
.parse_request_n(req, NonZeroU32!(5))
.in_current_span()
.await?;
let (user_id, perm) = auth.ok_or_default()?;
let (user_id, perm) = auth.auth_or_guest()?;

check_length!(SHORT_ART_SIZE, req.info, title);
check_length!(LONG_ART_SIZE, req.info, content);
req.check_with_error()?;

let uuid = Uuid::parse_str(&req.request_id).map_err(Error::InvaildUUID)?;
if let Some(x) = self.dup.check::<Id>(user_id, uuid) {
Expand Down Expand Up @@ -157,10 +156,9 @@ impl Announcement for ArcServer {
.parse_request_n(req, NonZeroU32!(5))
.in_current_span()
.await?;
let (user_id, _perm) = auth.ok_or_default()?;
let (user_id, _perm) = auth.auth_or_guest()?;

check_exist_length!(SHORT_ART_SIZE, req.info, title);
check_exist_length!(LONG_ART_SIZE, req.info, content);
req.check_with_error()?;

let uuid = Uuid::parse_str(&req.request_id).map_err(Error::InvaildUUID)?;
if let Some(x) = self.dup.check::<()>(user_id, uuid) {
Expand Down Expand Up @@ -219,7 +217,7 @@ impl Announcement for ArcServer {
.parse_request_n(req, NonZeroU32!(5))
.in_current_span()
.await?;
let (user_id, perm) = auth.ok_or_default()?;
let (user_id, perm) = auth.auth_or_guest()?;

if !perm.super_user() {
return Err(Error::RequirePermission(RoleLv::Super).into());
Expand Down
4 changes: 2 additions & 2 deletions backend/src/endpoint/chat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ impl Chat for ArcServer {
.parse_request_n(req, NonZeroU32!(5))
.in_current_span()
.await?;
let (user_id, _) = auth.ok_or_default()?;
let (user_id, _) = auth.auth_or_guest()?;

check_length!(LONG_ART_SIZE, req, message);
req.check_with_error()?;

let uuid = Uuid::parse_str(&req.request_id).map_err(Error::InvaildUUID)?;
if let Some(x) = self.dup.check::<Id>(user_id, uuid) {
Expand Down
13 changes: 5 additions & 8 deletions backend/src/endpoint/contest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,9 @@ impl Contest for ArcServer {
.parse_request_n(req, NonZeroU32!(5))
.in_current_span()
.await?;
let (user_id, perm) = auth.ok_or_default()?;
let (user_id, perm) = auth.auth_or_guest()?;

check_length!(SHORT_ART_SIZE, req.info, title, tags);
check_length!(LONG_ART_SIZE, req.info, content);
req.check_with_error()?;

let uuid = Uuid::parse_str(&req.request_id).map_err(Error::InvaildUUID)?;
if let Some(x) = self.dup.check::<Id>(user_id, uuid) {
Expand Down Expand Up @@ -129,10 +128,9 @@ impl Contest for ArcServer {
.parse_request_n(req, NonZeroU32!(5))
.in_current_span()
.await?;
let (user_id, perm) = auth.ok_or_default()?;
let (user_id, perm) = auth.auth_or_guest()?;

check_exist_length!(SHORT_ART_SIZE, req.info, title, tags);
check_exist_length!(LONG_ART_SIZE, req.info, content);
req.check_with_error()?;

let uuid = Uuid::parse_str(&req.request_id).map_err(Error::InvaildUUID)?;
if let Some(x) = self.dup.check::<()>(user_id, uuid) {
Expand All @@ -149,7 +147,6 @@ impl Contest for ArcServer {
.map_err(Into::<Error>::into)?
.ok_or(Error::NotInDB)?;

check_exist_length!(SHORT_ART_SIZE, req.info, password);
if let Some(src) = req.info.password {
if let Some(tar) = model.password.as_ref() {
if perm.root() || self.crypto.hash_eq(&src, tar) {
Expand Down Expand Up @@ -212,7 +209,7 @@ impl Contest for ArcServer {
.parse_request_n(req, NonZeroU32!(5))
.in_current_span()
.await?;
let (user_id, perm) = auth.ok_or_default()?;
let (user_id, perm) = auth.auth_or_guest()?;

let model = Entity::read_filter(Entity::find_by_id(req.id), &auth)?
.one(self.db.deref())
Expand Down
12 changes: 5 additions & 7 deletions backend/src/endpoint/education.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,9 @@ impl Education for ArcServer {
.parse_request_n(req, NonZeroU32!(5))
.in_current_span()
.await?;
let (user_id, perm) = auth.ok_or_default()?;
let (user_id, perm) = auth.auth_or_guest()?;

check_length!(SHORT_ART_SIZE, req.info, title);
check_length!(LONG_ART_SIZE, req.info, content);
req.check_with_error()?;

let uuid = Uuid::parse_str(&req.request_id).map_err(Error::InvaildUUID)?;
if let Some(x) = self.dup.check::<Id>(user_id, uuid) {
Expand Down Expand Up @@ -79,10 +78,9 @@ impl Education for ArcServer {
.parse_request_n(req, NonZeroU32!(5))
.in_current_span()
.await?;
let (user_id, _perm) = auth.ok_or_default()?;
let (user_id, _perm) = auth.auth_or_guest()?;

check_exist_length!(SHORT_ART_SIZE, req.info, title);
check_exist_length!(LONG_ART_SIZE, req.info, content);
req.check_with_error()?;

let uuid = Uuid::parse_str(&req.request_id).map_err(Error::InvaildUUID)?;
if let Some(x) = self.dup.check::<()>(user_id, uuid) {
Expand Down Expand Up @@ -142,7 +140,7 @@ impl Education for ArcServer {
.parse_request_n(req, NonZeroU32!(5))
.in_current_span()
.await?;
let (user_id, perm) = auth.ok_or_default()?;
let (user_id, perm) = auth.auth_or_guest()?;

let (problem, model) = tokio::try_join!(
problem::Entity::read_by_id(req.problem_id, &auth)?
Expand Down
2 changes: 1 addition & 1 deletion backend/src/endpoint/imgur.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ impl Image for ArcServer {
req: Request<UploadRequest>,
) -> Result<Response<UploadResponse>, Status> {
let (auth, req) = self.parse_request_n(req, crate::NonZeroU32!(5)).await?;
let (user_id, _) = auth.ok_or_default()?;
let (user_id, _) = auth.auth_or_guest()?;

let uuid = Uuid::parse_str(&req.request_id).map_err(Error::InvaildUUID)?;
if let Some(x) = self.dup.check::<UploadResponse>(user_id, uuid) {
Expand Down
9 changes: 3 additions & 6 deletions backend/src/endpoint/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,6 @@ mod token;
mod user;

mod tools {
/// longest allowed size for short string
pub const SHORT_ART_SIZE: usize = 128;
/// longest allowed size for long string
pub const LONG_ART_SIZE: usize = 262144;
pub use crate::NonZeroU32;
pub use grpc::backend::Id;
pub use sea_orm::*;
Expand All @@ -34,12 +30,13 @@ mod tools {
};
pub use crate::util::{
auth::RoleLv,
bound::BoundCheck,
error::{atomic_fail, Error},
time::*,
};
pub use crate::{
check_exist_length, check_length, fill_active_model, fill_exist_active_model,
parse_pager_param, server::ArcServer, TonicStream,
fill_active_model, fill_exist_active_model, parse_pager_param, server::ArcServer,
TonicStream,
};
}

Expand Down
18 changes: 8 additions & 10 deletions backend/src/endpoint/problem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,17 +71,16 @@ impl Problem for ArcServer {
.parse_request_n(req, NonZeroU32!(5))
.in_current_span()
.await?;
let (user_id, perm) = auth.ok_or_default()?;
let (user_id, perm) = auth.auth_or_guest()?;

check_length!(SHORT_ART_SIZE, req.info, title, tags);
check_length!(LONG_ART_SIZE, req.info, content);
req.check_with_error()?;

let uuid = Uuid::parse_str(&req.request_id).map_err(Error::InvaildUUID)?;
if let Some(x) = self.dup.check::<Id>(user_id, uuid) {
return Ok(Response::new(x));
};

if !(perm.super_user()) {
if !perm.super_user() {
return Err(Error::RequirePermission(RoleLv::Super).into());
}

Expand Down Expand Up @@ -116,10 +115,9 @@ impl Problem for ArcServer {
.parse_request_n(req, NonZeroU32!(5))
.in_current_span()
.await?;
let (user_id, _perm) = auth.ok_or_default()?;
let (user_id, _perm) = auth.auth_or_guest()?;

check_exist_length!(SHORT_ART_SIZE, req.info, title, tags);
check_exist_length!(LONG_ART_SIZE, req.info, content);
req.check_with_error()?;

let uuid = Uuid::parse_str(&req.request_id).map_err(Error::InvaildUUID)?;
if let Some(x) = self.dup.check::<()>(user_id, uuid) {
Expand Down Expand Up @@ -179,7 +177,7 @@ impl Problem for ArcServer {
.parse_request_n(req, NonZeroU32!(5))
.in_current_span()
.await?;
let (user_id, perm) = auth.ok_or_default()?;
let (user_id, perm) = auth.auth_or_guest()?;

if !perm.admin() {
return Err(Error::RequirePermission(RoleLv::Root).into());
Expand Down Expand Up @@ -255,7 +253,7 @@ impl Problem for ArcServer {
.parse_request_n(req, NonZeroU32!(5))
.in_current_span()
.await?;
let (_, perm) = auth.ok_or_default()?;
let (_, perm) = auth.auth_or_guest()?;

tracing::debug!(id = req.id);

Expand Down Expand Up @@ -290,7 +288,7 @@ impl Problem for ArcServer {
.parse_request_n(req, NonZeroU32!(5))
.in_current_span()
.await?;
let (_, perm) = auth.ok_or_default()?;
let (_, perm) = auth.auth_or_guest()?;

tracing::debug!(id = req.id);

Expand Down
8 changes: 3 additions & 5 deletions backend/src/endpoint/submit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,9 @@ impl Submit for ArcServer {
#[instrument(skip_all, level = "debug")]
async fn create(&self, req: Request<CreateSubmitRequest>) -> Result<Response<Id>, Status> {
let (auth, req) = self.parse_request_n(req, crate::NonZeroU32!(15)).await?;
let (user_id, _) = auth.ok_or_default()?;
let (user_id, _) = auth.auth_or_guest()?;

if req.code.len() > SUBMIT_CODE_LEN {
return Err(Error::BufferTooLarge("info.code").into());
}
req.check_with_error()?;

let lang = Uuid::parse_str(req.lang.as_str()).map_err(Into::<Error>::into)?;

Expand Down Expand Up @@ -182,7 +180,7 @@ impl Submit for ArcServer {
.parse_request_n(req, NonZeroU32!(5))
.in_current_span()
.await?;
let (user_id, perm) = auth.ok_or_default()?;
let (user_id, perm) = auth.auth_or_guest()?;

let submit_id = req.submit_id;

Expand Down
Loading

0 comments on commit 48083db

Please sign in to comment.