Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: ban cjs and cts file extensions #818

Merged
merged 14 commits into from
Jan 15, 2025
44 changes: 43 additions & 1 deletion api/src/analysis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ async fn analyze_package_inner(
.analyzer
.get_parsed_source(module.specifier())
{
check_for_banned_extensions(&parsed_source)?;
mbhrznr marked this conversation as resolved.
Show resolved Hide resolved
check_for_banned_syntax(&parsed_source)?;
check_for_banned_triple_slash_directives(&parsed_source)?;
}
Expand Down Expand Up @@ -802,6 +803,19 @@ fn collect_dependencies(
Ok(dependencies)
}

fn check_for_banned_extensions(
parsed_source: &ParsedSource,
) -> Result<(), PublishError> {
match parsed_source.media_type() {
deno_ast::MediaType::Cjs | deno_ast::MediaType::Cts => {
return Err(PublishError::BannedCommonJsExtension {
specifier: parsed_source.specifier().to_string(),
});
}
_ => Ok(()),
}
}

fn check_for_banned_syntax(
parsed_source: &ParsedSource,
) -> Result<(), PublishError> {
Expand Down Expand Up @@ -957,8 +971,15 @@ fn check_for_banned_triple_slash_directives(
#[cfg(test)]
mod tests {
fn parse(source: &str) -> deno_ast::ParsedSource {
let specifier = deno_ast::ModuleSpecifier::parse("file:///mod.ts").unwrap();
let media_type = deno_ast::MediaType::TypeScript;
parse_with_media_type(source, media_type)
}

fn parse_with_media_type(
source: &str,
media_type: deno_ast::MediaType,
) -> deno_ast::ParsedSource {
let specifier = deno_ast::ModuleSpecifier::parse("file:///mod.ts").unwrap();
deno_ast::parse_module(deno_ast::ParseParams {
specifier,
text: source.into(),
Expand All @@ -970,6 +991,27 @@ mod tests {
.unwrap()
}

#[test]
fn banned_extensions() {
let x =
parse_with_media_type("let x = 1;", deno_ast::MediaType::TypeScript);
assert!(super::check_for_banned_extensions(&x).is_ok());

let x = parse_with_media_type("let x = 1;", deno_ast::MediaType::Cjs);
let err = super::check_for_banned_extensions(&x).unwrap_err();
assert!(
matches!(err, super::PublishError::BannedCommonJsExtension { .. }),
"{err:?}",
);

let x = parse_with_media_type("let x = 1;", deno_ast::MediaType::Cts);
let err = super::check_for_banned_extensions(&x).unwrap_err();
assert!(
matches!(err, super::PublishError::BannedCommonJsExtension { .. }),
"{err:?}",
);
}

#[test]
fn banned_triple_slash_directives() {
let x = parse("let x = 1;");
Expand Down
8 changes: 7 additions & 1 deletion api/src/gcp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -573,12 +573,18 @@ impl FakeGcsTester {

assert!(self.proc.is_none());

#[cfg(target_os = "macos")]
#[cfg(all(target_os = "macos", target_arch = "aarch64"))]
let p = concat!(
env!("CARGO_MANIFEST_DIR"),
"/../tools/bin/darwin-arm64/fake-gcs-server"
);

#[cfg(all(target_os = "macos", target_arch = "x86_64"))]
mbhrznr marked this conversation as resolved.
Show resolved Hide resolved
let p = concat!(
env!("CARGO_MANIFEST_DIR"),
"/../tools/bin/darwin-amd64/fake-gcs-server"
);

#[cfg(target_os = "linux")]
let p = concat!(
env!("CARGO_MANIFEST_DIR"),
Expand Down
10 changes: 10 additions & 0 deletions api/src/publish.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1308,6 +1308,16 @@ pub mod tests {
assert_eq!(task.status, PublishingTaskStatus::Success, "{task:#?}");
}

#[tokio::test]
async fn cjs_import() {
let t = TestSetup::new().await;
let bytes = create_mock_tarball("cjs_import");
let task = process_tarball_setup(&t, bytes).await;
assert_eq!(task.status, PublishingTaskStatus::Failure, "{task:#?}");
let error = task.error.unwrap();
assert_eq!(error.code, "bannedCommonJsExtension");
}

#[tokio::test]
async fn npm_tarball() {
let t = TestSetup::new().await;
Expand Down
12 changes: 12 additions & 0 deletions api/src/tarball.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,12 @@ pub async fn process_tarball(
});
}

if path.ends_with(".cjs") | path.ends_with(".cts") {
return Err(PublishError::BannedCommonJsExtension {
specifier: path.to_string(),
});
}
mbhrznr marked this conversation as resolved.
Show resolved Hide resolved

let size = header.size().map_err(PublishError::UntarError)?;
if size > max_file_size {
return Err(PublishError::FileTooLarge {
Expand Down Expand Up @@ -517,6 +523,9 @@ pub enum PublishError {
column: usize,
},

#[error("CommonJS is not allowed {specifier}")]
BannedCommonJsExtension { specifier: String },
mbhrznr marked this conversation as resolved.
Show resolved Hide resolved

#[error("triple slash directives that modify globals (for example, '/// <reference no-default-lib=\"true\" />' or '/// <reference lib=\"dom\" />') are not allowed. Instead instruct the user of your package to specify these directives. {specifier}:{line}:{column}")]
BannedTripleSlashDirectives {
specifier: String,
Expand Down Expand Up @@ -636,6 +645,9 @@ impl PublishError {
Some("globalTypeAugmentation")
}
PublishError::CommonJs { .. } => Some("commonJs"),
PublishError::BannedCommonJsExtension { .. } => {
Some("bannedCommonJsExtension")
}
PublishError::BannedTripleSlashDirectives { .. } => {
Some("bannedTripleSlashDirectives")
}
Expand Down
5 changes: 5 additions & 0 deletions api/testdata/tarballs/cjs_import/jsr.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"name": "@scope/foo",
"version": "1.2.3",
"exports": "./mod.ts"
}
3 changes: 3 additions & 0 deletions api/testdata/tarballs/cjs_import/mod.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { test } from "./other.cjs"; // bad

export const hello = `Hello, ${test}!`;
1 change: 1 addition & 0 deletions api/testdata/tarballs/cjs_import/other.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
exports.test = "test";
Loading