Skip to content

Commit

Permalink
fix: prevent imports from folder with same name
Browse files Browse the repository at this point in the history
  • Loading branch information
coderbyheart committed Apr 8, 2024
1 parent 19ee0fa commit 3aa049b
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 1 deletion.
30 changes: 30 additions & 0 deletions src/module-folder-named-like-handler-bug.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import assert from 'node:assert/strict'
import { it, describe } from 'node:test'
import { ImportFromFolderNameError, packLambda } from './packLambda.js'
import path, { dirname } from 'node:path'
import { fileURLToPath } from 'node:url'
import fs from 'node:fs/promises'
import os from 'node:os'

const tmpDir = os.tmpdir()

void describe('packLambda()', () => {
// See https://github.com/aws/aws-lambda-nodejs-runtime-interface-client/issues/93#issuecomment-2042201321
void it('should fail if it imports from a folder that has the same name as the handler module', async () =>
assert.rejects(
async () =>
packLambda({
sourceFile: path.join(
dirname(fileURLToPath(import.meta.url)),
'test-data',
'module-folder-named-like-handler-bug',
'acme.ts',
),
zipFile: path.join(
await fs.mkdtemp(`${tmpDir}${path.sep}`),
'acme.zip',
),
}),
ImportFromFolderNameError,
))
})
26 changes: 25 additions & 1 deletion src/packLambda.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { checkSumOfFiles } from './checksumOfFiles.js'
import { commonParent } from './commonParent.js'
import { findDependencies } from './findDependencies.js'
import { fileURLToPath } from 'node:url'
import path from 'node:path'

export type PackedLambda = {
id: string
Expand Down Expand Up @@ -43,12 +44,21 @@ export const packLambda = async ({
debug?: (label: string, info: string) => void
progress?: (label: string, info: string) => void
}): Promise<{ handler: string; hash: string }> => {
const lambdaFiles = [sourceFile, ...findDependencies(sourceFile)]
const deps = findDependencies(sourceFile)
const lambdaFiles = [sourceFile, ...deps]

const zipfile = new yazl.ZipFile()

const stripCommon = removeCommonAncestor(commonParent(lambdaFiles))

const handler = stripCommon(sourceFile)

const folderNames = new Set(deps.map(stripCommon).map((s) => s.split('/')[0]))
const handlerName = path.parse(handler).name
if (folderNames.has(handlerName)) {
throw new ImportFromFolderNameError(handlerName)
}

for (const file of lambdaFiles) {
const compiled = (
await swc.transformFile(file, {
Expand Down Expand Up @@ -91,3 +101,17 @@ export const packLambda = async ({

return { handler: stripCommon(sourceFile), hash }
}

/**
* @see https://github.com/aws/aws-lambda-nodejs-runtime-interface-client/issues/93#issuecomment-2042201321
*/
export class ImportFromFolderNameError extends Error {
public readonly folderName: string
constructor(folderName: string) {
super(
`Import from folder with same name as handler ("${folderName}") not allowed!`,
)
this.name = 'ImportFromFolderNameError'
this.folderName = folderName
}
}
7 changes: 7 additions & 0 deletions src/test-data/module-folder-named-like-handler-bug/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Lambda's will fail if they import from a folder that has the same name as the
handler.

`packLambda` should fail in this case.

See
https://github.com/aws/aws-lambda-nodejs-runtime-interface-client/issues/93#issuecomment-2042201321
3 changes: 3 additions & 0 deletions src/test-data/module-folder-named-like-handler-bug/acme.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { hello } from './acme/lib.js'

export const handler = (): string => hello()
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const hello = (): string => 'Hello World!'

0 comments on commit 3aa049b

Please sign in to comment.