Skip to content

Commit

Permalink
Implement encryption key into turbopack as hash salt (#72933)
Browse files Browse the repository at this point in the history
I checked that the encryption key is now being used as the hash_salt.
I'm not sure about how to add a test for this behavior so let me know
about any suggestions there. (I tried looking for tests that would use
the functions up the call tree without any luck)
  • Loading branch information
lfades authored Jan 9, 2025
1 parent d80bd57 commit 6b7241a
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 11 deletions.
7 changes: 7 additions & 0 deletions crates/next-api/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ impl AppProject {
Value::new(self.client_ty().await?.clone_value()),
self.project().next_mode(),
self.project().next_config(),
self.project().encryption_key(),
))
}

Expand Down Expand Up @@ -213,6 +214,7 @@ impl AppProject {
self.project().next_mode(),
self.project().next_config(),
NextRuntime::NodeJs,
self.project().encryption_key(),
))
}

Expand All @@ -225,6 +227,7 @@ impl AppProject {
self.project().next_mode(),
self.project().next_config(),
NextRuntime::Edge,
self.project().encryption_key(),
))
}

Expand All @@ -237,6 +240,7 @@ impl AppProject {
self.project().next_mode(),
self.project().next_config(),
NextRuntime::NodeJs,
self.project().encryption_key(),
))
}

Expand All @@ -249,6 +253,7 @@ impl AppProject {
self.project().next_mode(),
self.project().next_config(),
NextRuntime::Edge,
self.project().encryption_key(),
))
}

Expand Down Expand Up @@ -551,6 +556,7 @@ impl AppProject {
self.project().next_mode(),
self.project().next_config(),
NextRuntime::NodeJs,
self.project().encryption_key(),
))
}

Expand All @@ -563,6 +569,7 @@ impl AppProject {
self.project().next_mode(),
self.project().next_config(),
NextRuntime::Edge,
self.project().encryption_key(),
))
}

Expand Down
7 changes: 7 additions & 0 deletions crates/next-api/src/pages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,7 @@ impl PagesProject {
}),
self.project().next_mode(),
self.project().next_config(),
self.project().encryption_key(),
))
}

Expand Down Expand Up @@ -436,6 +437,7 @@ impl PagesProject {
self.project().next_mode(),
self.project().next_config(),
NextRuntime::NodeJs,
self.project().encryption_key(),
))
}

Expand All @@ -450,6 +452,7 @@ impl PagesProject {
self.project().next_mode(),
self.project().next_config(),
NextRuntime::Edge,
self.project().encryption_key(),
))
}

Expand All @@ -464,6 +467,7 @@ impl PagesProject {
self.project().next_mode(),
self.project().next_config(),
NextRuntime::NodeJs,
self.project().encryption_key(),
))
}

Expand All @@ -478,6 +482,7 @@ impl PagesProject {
self.project().next_mode(),
self.project().next_config(),
NextRuntime::Edge,
self.project().encryption_key(),
))
}

Expand All @@ -492,6 +497,7 @@ impl PagesProject {
self.project().next_mode(),
self.project().next_config(),
NextRuntime::NodeJs,
self.project().encryption_key(),
))
}

Expand All @@ -508,6 +514,7 @@ impl PagesProject {
self.project().next_mode(),
self.project().next_config(),
NextRuntime::Edge,
self.project().encryption_key(),
))
}

Expand Down
8 changes: 8 additions & 0 deletions crates/next-api/src/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -691,6 +691,11 @@ impl Project {
*self.js_config
}

#[turbo_tasks::function]
pub(super) fn encryption_key(&self) -> Vc<RcStr> {
Vc::cell(self.encryption_key.clone())
}

#[turbo_tasks::function]
pub(super) async fn should_create_webpack_stats(&self) -> Result<Vc<bool>> {
Ok(Vc::cell(
Expand Down Expand Up @@ -1122,6 +1127,7 @@ impl Project {
self.next_mode(),
self.next_config(),
NextRuntime::Edge,
self.encryption_key(),
),
get_edge_resolve_options_context(
self.project_path(),
Expand Down Expand Up @@ -1207,6 +1213,7 @@ impl Project {
self.next_mode(),
self.next_config(),
NextRuntime::NodeJs,
self.encryption_key(),
),
get_server_resolve_options_context(
self.project_path(),
Expand Down Expand Up @@ -1261,6 +1268,7 @@ impl Project {
self.next_mode(),
self.next_config(),
NextRuntime::Edge,
self.encryption_key(),
),
get_edge_resolve_options_context(
self.project_path(),
Expand Down
7 changes: 5 additions & 2 deletions crates/next-core/src/next_client/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@ pub async fn get_client_module_options_context(
ty: Value<ClientContextType>,
mode: Vc<NextMode>,
next_config: Vc<NextConfig>,
encryption_key: ResolvedVc<RcStr>,
) -> Result<Vc<ModuleOptionsContext>> {
let next_mode = mode.await?;
let resolve_options_context = get_client_resolve_options_context(
Expand Down Expand Up @@ -283,9 +284,11 @@ pub async fn get_client_module_options_context(
let target_browsers = env.runtime_versions();

let mut next_client_rules =
get_next_client_transforms_rules(next_config, ty.into_value(), mode, false).await?;
get_next_client_transforms_rules(next_config, ty.into_value(), mode, false, encryption_key)
.await?;
let foreign_next_client_rules =
get_next_client_transforms_rules(next_config, ty.into_value(), mode, true).await?;
get_next_client_transforms_rules(next_config, ty.into_value(), mode, true, encryption_key)
.await?;
let additional_rules: Vec<ModuleRule> = vec![
get_swc_ecma_transform_plugin_rule(next_config, project_path).await?,
get_relay_transform_rule(next_config, project_path).await?,
Expand Down
5 changes: 4 additions & 1 deletion crates/next-core/src/next_client/transforms.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use anyhow::Result;
use next_custom_transforms::transforms::strip_page_exports::ExportFilter;
use turbo_tasks::Vc;
use turbo_rcstr::RcStr;
use turbo_tasks::{ResolvedVc, Vc};
use turbopack::module_options::ModuleRule;

use crate::{
Expand All @@ -27,6 +28,7 @@ pub async fn get_next_client_transforms_rules(
context_ty: ClientContextType,
mode: Vc<NextMode>,
foreign_code: bool,
encryption_key: ResolvedVc<RcStr>,
) -> Result<Vec<ModuleRule>> {
let mut rules = vec![];

Expand Down Expand Up @@ -73,6 +75,7 @@ pub async fn get_next_client_transforms_rules(
is_app_dir = true;
rules.push(get_server_actions_transform_rule(
ActionsTransform::Client,
encryption_key,
enable_mdx_rs,
dynamic_io_enabled,
cache_kinds,
Expand Down
25 changes: 19 additions & 6 deletions crates/next-core/src/next_server/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -419,14 +419,27 @@ pub async fn get_server_module_options_context(
mode: Vc<NextMode>,
next_config: Vc<NextConfig>,
next_runtime: NextRuntime,
encryption_key: ResolvedVc<RcStr>,
) -> Result<Vc<ModuleOptionsContext>> {
let next_mode = mode.await?;
let mut next_server_rules =
get_next_server_transforms_rules(next_config, ty.into_value(), mode, false, next_runtime)
.await?;
let mut foreign_next_server_rules =
get_next_server_transforms_rules(next_config, ty.into_value(), mode, true, next_runtime)
.await?;
let mut next_server_rules = get_next_server_transforms_rules(
next_config,
ty.into_value(),
mode,
false,
next_runtime,
encryption_key,
)
.await?;
let mut foreign_next_server_rules = get_next_server_transforms_rules(
next_config,
ty.into_value(),
mode,
true,
next_runtime,
encryption_key,
)
.await?;
let mut internal_custom_rules = get_next_server_internal_transforms_rules(
ty.into_value(),
next_config.mdx_rs().await?.is_some(),
Expand Down
7 changes: 6 additions & 1 deletion crates/next-core/src/next_server/transforms.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use anyhow::Result;
use next_custom_transforms::transforms::strip_page_exports::ExportFilter;
use turbo_tasks::Vc;
use turbo_rcstr::RcStr;
use turbo_tasks::{ResolvedVc, Vc};
use turbopack::module_options::ModuleRule;

use crate::{
Expand Down Expand Up @@ -30,6 +31,7 @@ pub async fn get_next_server_transforms_rules(
mode: Vc<NextMode>,
foreign_code: bool,
next_runtime: NextRuntime,
encryption_key: ResolvedVc<RcStr>,
) -> Result<Vec<ModuleRule>> {
let mut rules = vec![];

Expand Down Expand Up @@ -90,6 +92,7 @@ pub async fn get_next_server_transforms_rules(
// need to apply to foreign code too
rules.push(get_server_actions_transform_rule(
ActionsTransform::Client,
encryption_key,
mdx_rs,
dynamic_io_enabled,
cache_kinds,
Expand All @@ -102,6 +105,7 @@ pub async fn get_next_server_transforms_rules(
ServerContextType::AppRSC { .. } => {
rules.push(get_server_actions_transform_rule(
ActionsTransform::Server,
encryption_key,
mdx_rs,
dynamic_io_enabled,
cache_kinds,
Expand All @@ -114,6 +118,7 @@ pub async fn get_next_server_transforms_rules(
ServerContextType::AppRoute { .. } => {
rules.push(get_server_actions_transform_rule(
ActionsTransform::Server,
encryption_key,
mdx_rs,
dynamic_io_enabled,
cache_kinds,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use anyhow::Result;
use async_trait::async_trait;
use next_custom_transforms::transforms::server_actions::{server_actions, Config};
use swc_core::{common::FileName, ecma::ast::Program};
use turbo_rcstr::RcStr;
use turbo_tasks::ResolvedVc;
use turbopack::module_options::{ModuleRule, ModuleRuleEffect};
use turbopack_ecmascript::{CustomTransformer, EcmascriptInputTransform, TransformContext};
Expand All @@ -18,13 +19,15 @@ pub enum ActionsTransform {
/// Returns a rule which applies the Next.js Server Actions transform.
pub fn get_server_actions_transform_rule(
transform: ActionsTransform,
encryption_key: ResolvedVc<RcStr>,
enable_mdx_rs: bool,
dynamic_io_enabled: bool,
cache_kinds: ResolvedVc<CacheKinds>,
) -> ModuleRule {
let transformer =
EcmascriptInputTransform::Plugin(ResolvedVc::cell(Box::new(NextServerActions {
transform,
encryption_key,
dynamic_io_enabled,
cache_kinds,
}) as _));
Expand All @@ -40,6 +43,7 @@ pub fn get_server_actions_transform_rule(
#[derive(Debug)]
struct NextServerActions {
transform: ActionsTransform,
encryption_key: ResolvedVc<RcStr>,
dynamic_io_enabled: bool,
cache_kinds: ResolvedVc<CacheKinds>,
}
Expand All @@ -53,7 +57,7 @@ impl CustomTransformer for NextServerActions {
Config {
is_react_server_layer: matches!(self.transform, ActionsTransform::Server),
dynamic_io_enabled: self.dynamic_io_enabled,
hash_salt: "".into(),
hash_salt: self.encryption_key.await?.to_string(),
cache_kinds: self.cache_kinds.await?.clone_value(),
},
ctx.comments.clone(),
Expand Down

0 comments on commit 6b7241a

Please sign in to comment.