Skip to content
This repository was archived by the owner on May 22, 2024. It is now read-only.

feat: disallow unique entry file name #1328

Merged
merged 6 commits into from
Jan 31, 2023
Merged

feat: disallow unique entry file name #1328

merged 6 commits into from
Jan 31, 2023

Conversation

danez
Copy link
Contributor

@danez danez commented Jan 30, 2023

Summary

Ref https://github.com/netlify/pod-compute/issues/349

This disallows functions to be named ___netlify-lambda-entry and also disallows any additional files/directories in the bundled function to contain ___netlify-lambda-entry.

This is behind a FF. I also thought about silently ignoring function files named like this, but I think that would be more surprising.

The release plan is to roll this feature out and monitor how often this error happens and if it happens, disable FF and investigate and maybe pick another name. But I suspect it not being used at all. The expected case is that no error is thrown at all when activating the FF.


For us to review and ship your PR efficiently, please perform the following steps:

  • Open a bug/issue before writing your code 🧑‍💻.
    This ensures we can discuss the changes and get feedback from everyone that should be involved. If you`re fixing
    a typo or something that`s on fire 🔥 (e.g. incident related), you can skip this step.
  • Read the contribution guidelines 📖. This ensures your code follows our style guide and
    passes our tests.
  • Update or add tests (if any source code was changed or added) 🧪
  • Update or add documentation (if features were changed or added) 📝
  • Make sure the status checks below are successful ✅

A picture of a cute animal (not mandatory, but encouraged)

Sorry, something went wrong.

Verified

This commit was signed with the committer’s verified signature.
fanatid Kirill Fomichev
@danez danez requested a review from a team January 30, 2023 12:38
@danez danez self-assigned this Jan 30, 2023
@github-actions
Copy link
Contributor

github-actions bot commented Jan 30, 2023

⏱ Benchmark results

Comparing with 84189b1

largeDepsEsbuild: 2.1s

⬇️ 31.44% decrease vs. 84189b1

^                                            3s                                           
│                                           ┌──┐                            2.7s          
│                   2.7s    2.6s            |  |                            ┌──┐          
│           2.5s    ┌──┐    ┌──┐            |  |                            |  |          
│           ┌──┐    |  |    |  |            |  |                            |  |          
│ ──────────┼──┼────┼──┼────┼──┼────────────┼──┼────2.2s────────────────────┼──┼──────────
│   2.1s    |  |    |  |    |  |     2s     |  |    ┌──┐    2.1s    2.1s    |  |    2.1s  
│   ┌──┐    |  |    |  |    |  |    ┌──┐    |  |    |  |    ┌──┐    ┌──┐    |  |    ┌──┐  
│   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
└───┴──┴────┴──┴────┴──┴────┴──┴────┴──┴────┴──┴────┴──┴────┴──┴────┴──┴────┴──┴────┴──┴──>
    T-10    T-9     T-8     T-7     T-6     T-5     T-4     T-3     T-2     T-1      T    
Legend

largeDepsNft: 7.4s

⬇️ 51.51% decrease vs. 84189b1

^                                          11.7s                                          
│                                           ┌──┐                           11.3s          
│                                           |  |                            ┌──┐          
│                   9.9s    10s             |  |                            |  |          
│           9.6s    ┌──┐    ┌──┐            |  |                            |  |          
│           ┌──┐    |  |    |  |            |  |    8.8s                    |  |          
│ ───8s─────┼──┼────┼──┼────┼──┼────────────┼──┼────┌──┐────────────────────┼──┼──────────
│   ┌──┐    |  |    |  |    |  |    7.6s    |  |    |  |    7.7s    7.5s    |  |    7.4s  
│   |  |    |  |    |  |    |  |    ┌──┐    |  |    |  |    ┌──┐    ┌──┐    |  |    ┌──┐  
│   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
└───┴──┴────┴──┴────┴──┴────┴──┴────┴──┴────┴──┴────┴──┴────┴──┴────┴──┴────┴──┴────┴──┴──>
    T-10    T-9     T-8     T-7     T-6     T-5     T-4     T-3     T-2     T-1      T    
Legend

largeDepsZisi: 14.8s

⬇️ 39.87% decrease vs. 84189b1

^                                          21.5s                                          
│                                           ┌──┐                           20.8s          
│                          19.3s            |  |                            ┌──┐          
│          18.6s    18s     ┌──┐            |  |                            |  |          
│           ┌──┐    ┌──┐    |  |            |  |                            |  |          
│ ──────────┼──┼────┼──┼────┼──┼────────────┼──┼───16.2s────────────────────┼──┼──────────
│  15.6s    |  |    |  |    |  |   14.8s    |  |    ┌──┐   15.1s    15s     |  |   14.8s  
│   ┌──┐    |  |    |  |    |  |    ┌──┐    |  |    |  |    ┌──┐    ┌──┐    |  |    ┌──┐  
│   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
└───┴──┴────┴──┴────┴──┴────┴──┴────┴──┴────┴──┴────┴──┴────┴──┴────┴──┴────┴──┴────┴──┴──>
    T-10    T-9     T-8     T-7     T-6     T-5     T-4     T-3     T-2     T-1      T    
Legend

danez and others added 3 commits January 30, 2023 13:51
Co-authored-by: Eduardo Bouças <mail@eduardoboucas.com>
@danez danez requested a review from eduardoboucas January 30, 2023 13:15
let hasConflict = false

srcFiles.forEach((srcFile) => {
if (featureFlags.zisi_disallow_new_entry_name && srcFile.includes(ENTRY_FILE_NAME)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are you using includes here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To catch dirs as well as files that contain the name. It would also catch names that only include ___netlify-lambda-entry as part of it, but it is simpler, the alternative would be:

basename(srcFile, extname(srcFile)) === ENTRY_FILE_NAME || srcFile.includes(`/${ENTRY_FILE_NAME}/`)

Copy link
Member

@eduardoboucas eduardoboucas Jan 30, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see. I find it a bit disconcerting that we're checking so loosely for that string anywhere in the file or directory names, even if that's a very unique string.

But more generally, I wonder if we need to do this type of check at all. We're the ones adding the entry file, so we know exactly what is its name and extension. Can we check, at the point of inserting it, whether there's already a file in the zip with that name? At that point, it becomes a simple equality check, which should also be more performant?

Copy link
Contributor Author

@danez danez Jan 31, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The problem is that it is not just one file, but multiple that we are forbidding. When AWS searches for the entrypoint this is what it looks for (tested just now):

  1. /___netlify-lambda-entry.js
  2. /___netlify-lambda-entry/index.js
  3. /___netlify-lambda-entry.mjs
  4. /___netlify-lambda-entry.cjs

So any user file named like this should not be allowed. Checking when adding the entry file is a little too late for now, because this PR is meant to check if ___netlify-lambda-entry is already used. And as we currently do not always generate an entry file, we would not always check for these files.

Alternatively we could have a Set with all those relative paths and check if the user files is included?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I see!

Alternatively we could have a Set with all those relative paths and check if the user files is included?

That makes sense to me. If you're okay with that, I would suggest this route.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Worse, some combinations make Lambda crash I just noticed:
Crashes:
Screenshot 2023-01-31 at 15 27 08

Rename the folder:
Screenshot 2023-01-31 at 15 27 28

I will look into the Set.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Disallowing /___netlify-lambda-entry/ and ___netlify-lambda-entry.(c|m)?js would look like this:
I am not sure I like this, and also does a lot more per iteration.

@@ -1,4 +1,4 @@
-import { basename, extname, resolve } from 'path'
+import { basename, extname, relative, resolve } from 'path'
 
 import type { FeatureFlags } from '../../../feature_flags.js'
 import { FunctionBundlingUserError } from '../../../utils/error.js'
@@ -24,6 +24,12 @@
   return `export { handler } from '${importPath}'`
 }
 
+const LAMBDA_ENTRY_FILES = new Set([
+  ENTRY_FILE_NAME + ModuleFileExtension.JS,
+  ENTRY_FILE_NAME + ModuleFileExtension.MJS,
+  ENTRY_FILE_NAME + ModuleFileExtension.CJS,
+])
+
 // They are in the order that AWS Lambda will try to find the entry point
 const POSSIBLE_LAMBDA_ENTRY_EXTENSIONS = [ModuleFileExtension.JS, ModuleFileExtension.MJS, ModuleFileExtension.CJS]
 
@@ -65,7 +71,12 @@
   let hasConflict = false
 
   srcFiles.forEach((srcFile) => {
-    if (featureFlags.zisi_disallow_new_entry_name && srcFile.includes(ENTRY_FILE_NAME)) {
+    const relativeSrcFile = relative(basePath, srcFile)
+
+    if (
+      featureFlags.zisi_disallow_new_entry_name &&
+      (relativeSrcFile.startsWith(`${ENTRY_FILE_NAME}/`) || LAMBDA_ENTRY_FILES.has(relativeSrcFile))
+    ) {
       throw new FunctionBundlingUserError(
         `'${ENTRY_FILE_NAME}' is a reserved word and cannot be used as a file or directory name.`,
         {

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed.

@danez danez requested a review from eduardoboucas January 30, 2023 14:43
Copy link
Member

@eduardoboucas eduardoboucas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚀

let hasConflict = false

srcFiles.forEach((srcFile) => {
if (featureFlags.zisi_disallow_new_entry_name && srcFile.includes(ENTRY_FILE_NAME)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed.

@kodiakhq kodiakhq bot merged commit 17231da into main Jan 31, 2023
@kodiakhq kodiakhq bot deleted the entry branch January 31, 2023 17:12
Skn0tt pushed a commit to netlify/build that referenced this pull request May 21, 2024
* feat: disallow unique entry file name

* Apply suggestions from code review

Co-authored-by: Eduardo Bouças <mail@eduardoboucas.com>

* chore: review fixes

* chore: fixes

* chore: fix name

---------

Co-authored-by: Eduardo Bouças <mail@eduardoboucas.com>
Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants