Skip to content

Commit

Permalink
worker: improve path validation
Browse files Browse the repository at this point in the history
  • Loading branch information
ije committed Jul 21, 2024
1 parent 7c442bd commit 5f0fdfc
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions worker/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -487,7 +487,7 @@ function withESMWorker(middleware?: Middleware, cache: Cache = (caches as any).d
if (packageName === "") {
return err("Invalid path", ctx.corsHeaders(), 400);
}
if (!regexpNpmNaming.test(packageName)) {
if (!regexpNpmNaming.test(packageName) || packageVersion.endsWith(".") || packageVersion.endsWith("-")) {
return err(`Invalid package name '${packageName}'`, ctx.corsHeaders(), 400);
}

Expand All @@ -511,6 +511,9 @@ function withESMWorker(middleware?: Middleware, cache: Cache = (caches as any).d
}
}
}
if (packageVersion && packageVersion.endsWith(".")) {
return err(`Invalid package version '${packageVersion}'`, ctx.corsHeaders(), 400);
}

// redirect to commit-ish version for GitHub packages
if (
Expand Down Expand Up @@ -592,10 +595,10 @@ function withESMWorker(middleware?: Middleware, cache: Cache = (caches as any).d
}
}
} catch (_) {
// error of `satisfies` function
return err(`Invalid package version '${packageVersion}'`, ctx.corsHeaders());
// not a semver version
return err(`Invalid package version '${packageVersion}'`, ctx.corsHeaders(), 400);
}
return err("Could not get the package version", ctx.corsHeaders());
return err("Could not get the package version", ctx.corsHeaders(), 404);
});
}

Expand Down

0 comments on commit 5f0fdfc

Please sign in to comment.