Skip to content

Commit

Permalink
Minor rename and refactor and TLS preparation
Browse files Browse the repository at this point in the history
  • Loading branch information
richarddavison committed Nov 1, 2023
1 parent ca60d76 commit 6018b53
Show file tree
Hide file tree
Showing 8 changed files with 69 additions and 56 deletions.
2 changes: 1 addition & 1 deletion src/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use tracing::trace;

use crate::{
util::{export_default, ResultExt},
vm::CaughtErrorExtensions,
vm::ErrorExtensions,
};

#[derive(Clone, Debug)]
Expand Down
41 changes: 23 additions & 18 deletions src/fs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,26 +27,11 @@ pub const CONSTANT_R_OK: u32 = 4;
pub const CONSTANT_W_OK: u32 = 2;
pub const CONSTANT_X_OK: u32 = 1;

pub struct FsModule;
pub struct FsPromisesModule;

impl ModuleDef for FsModule {
impl ModuleDef for FsPromisesModule {
fn declare(declare: &mut Declarations) -> Result<()> {
declare.declare("access")?;
declare.declare("open")?;
declare.declare("readFile")?;
declare.declare("writeFile")?;
declare.declare("appendFile")?;
declare.declare("copyFile")?;
declare.declare("rename")?;
declare.declare("readdir")?;
declare.declare("mkdir")?;
declare.declare("mkdtemp")?;
declare.declare("rm")?;
declare.declare("rmdir")?;
declare.declare("stat")?;
declare.declare("constants")?;

declare.declare("default")?;
delarations(declare)?;

Ok(())
}
Expand Down Expand Up @@ -78,3 +63,23 @@ impl ModuleDef for FsModule {
})
}
}

fn delarations(declare: &mut Declarations) -> Result<()> {
declare.declare("access")?;
declare.declare("open")?;
declare.declare("readFile")?;
declare.declare("writeFile")?;
declare.declare("appendFile")?;
declare.declare("copyFile")?;
declare.declare("rename")?;
declare.declare("readdir")?;
declare.declare("mkdir")?;
declare.declare("mkdtemp")?;
declare.declare("rm")?;
declare.declare("rmdir")?;
declare.declare("stat")?;
declare.declare("constants")?;

declare.declare("default")?;
Ok(())
}
25 changes: 3 additions & 22 deletions src/http/fetch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,12 @@ use rquickjs::{
prelude::{Async, Func},
Ctx, Error, Exception, Object, Result, Value,
};
use rustls::{ClientConfig, OwnedTrustAnchor, RootCertStore};
use std::time::Instant;

use webpki::TrustAnchor;
use webpki_roots::TLS_SERVER_ROOTS;
use std::time::Instant;

use crate::{
http::headers::Headers,
net::TLS_CONFIG,
security::{ensure_url_access, HTTP_DENY_LIST},
util::{get_bytes, ObjectExt, ResultExt},
};
Expand Down Expand Up @@ -42,25 +40,8 @@ pub(crate) fn init(ctx: &Ctx<'_>, globals: &Object) -> Result<()> {
));
}

let mut root_certificates = RootCertStore::empty();
let create_owned_trust_anchor = |ta: &TrustAnchor| {
OwnedTrustAnchor::from_subject_spki_name_constraints(
ta.subject,
ta.spki,
ta.name_constraints,
)
};
root_certificates
.add_server_trust_anchors(TLS_SERVER_ROOTS.0.iter().map(create_owned_trust_anchor));

let tls = ClientConfig::builder()
.with_safe_defaults()
//.with_native_roots()
.with_root_certificates(root_certificates)
.with_no_client_auth();

let https = hyper_rustls::HttpsConnectorBuilder::new()
.with_tls_config(tls)
.with_tls_config(TLS_CONFIG.clone())
.https_or_http()
.enable_http1()
.build();
Expand Down
25 changes: 25 additions & 0 deletions src/net/mod.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,35 @@
mod socket;

use once_cell::sync::Lazy;
use rquickjs::{
cstr,
module::{Declarations, Exports, ModuleDef},
Ctx, Result,
};
use rustls::{ClientConfig, OwnedTrustAnchor, RootCertStore};
use webpki::TrustAnchor;
use webpki_roots::TLS_SERVER_ROOTS;

pub static TLS_CONFIG: Lazy<ClientConfig> = Lazy::new(|| {
let mut root_certificates = RootCertStore::empty();
let create_owned_trust_anchor = |ta: &TrustAnchor| {
OwnedTrustAnchor::from_subject_spki_name_constraints(
ta.subject,
ta.spki,
ta.name_constraints,
)
};
root_certificates
.add_server_trust_anchors(TLS_SERVER_ROOTS.0.iter().map(create_owned_trust_anchor));

let tls: ClientConfig = ClientConfig::builder()
.with_safe_defaults()
//.with_native_roots()
.with_root_certificates(root_certificates)
.with_no_client_auth();

tls
});

pub struct NetModule;

Expand Down
2 changes: 1 addition & 1 deletion src/net/socket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ impl<'js> Socket<'js> {
if let Some(path) = path.clone() {
ensure_net_access(&ctx, &path)?;
}
if let Some(port) = port.clone() {
if let Some(port) = port {
let hostname = format!("{}:{}", host, port);
ensure_net_access(&ctx, &hostname)?;
addr = Some(hostname);
Expand Down
2 changes: 1 addition & 1 deletion src/security.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ pub fn ensure_url_access(ctx: &Ctx<'_>, uri: &Uri) -> Result<()> {
Ok(())
}

fn url_restricted_error<'js>(ctx: &Ctx<'js>, message: &str, uri: &Uri) -> Error {
fn url_restricted_error(ctx: &Ctx<'_>, message: &str, uri: &Uri) -> Error {
let uri_host = uri.host().unwrap_or_default();
let uri_port = uri
.port_u16()
Expand Down
14 changes: 5 additions & 9 deletions src/stream/writable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::sync::{Arc, RwLock};
use rquickjs::{
class::{Trace, Tracer},
prelude::{Func, Opt, This},
CatchResultExt, Class, Ctx, Error, Exception, Function, Result, Value,
Class, Ctx, Error, Exception, Function, Result, Value,
};

use tokio::{
Expand All @@ -19,7 +19,7 @@ use crate::{
events::{EmitError, Emitter, EventEmitter, EventList},
stream::set_destroyed_and_error,
util::{get_bytes, ResultExt},
vm::{CaughtErrorExtensions, CtxExtension},
vm::{CtxExtension, ErrorExtensions},
};

use super::SteamEvents;
Expand Down Expand Up @@ -201,13 +201,9 @@ where
.is_err()
{
if let Some(cb) = callback {
let err = Err::<(), _>(Exception::throw_message(
&ctx,
"This socket has been ended by the other party",
))
.catch(&ctx)
.unwrap_err()
.into_value(&ctx)?;
let err =
Exception::throw_message(&ctx, "This socket has been ended by the other party")
.into_value(&ctx)?;

cb.call((err,))?;
}
Expand Down
14 changes: 10 additions & 4 deletions src/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ use crate::{
crypto::CryptoModule,
encoding::HexModule,
events::EventsModule,
fs::FsModule,
fs::FsPromisesModule,
module::ModuleModule,
net::NetModule,
os::OsModule,
Expand Down Expand Up @@ -80,7 +80,7 @@ create_modules!(
"crypto" => CryptoModule,
"uuid" => UuidModule,
"hex" => HexModule,
"fs/promises" => FsModule,
"fs/promises" => FsPromisesModule,
"os" => OsModule,
"timers" => TimersModule,
"events" => EventsModule,
Expand Down Expand Up @@ -588,11 +588,17 @@ fn set_import_meta(module: &Module<'_>, filepath: &str) -> Result<()> {
Ok(())
}

pub trait CaughtErrorExtensions<'js> {
pub trait ErrorExtensions<'js> {
fn into_value(self, ctx: &Ctx<'js>) -> Result<Value<'js>>;
}

impl<'js> CaughtErrorExtensions<'js> for CaughtError<'js> {
impl<'js> ErrorExtensions<'js> for Error {
fn into_value(self, ctx: &Ctx<'js>) -> Result<Value<'js>> {
Err::<(), _>(self).catch(ctx).unwrap_err().into_value(ctx)
}
}

impl<'js> ErrorExtensions<'js> for CaughtError<'js> {
fn into_value(self, ctx: &Ctx<'js>) -> Result<Value<'js>> {
Ok(match self {
CaughtError::Error(err) => {
Expand Down

0 comments on commit 6018b53

Please sign in to comment.