panva / paseto

PASETO (Platform-Agnostic SEcurity TOkens) for Node.js with no dependencies
MIT License
428 stars 26 forks source link

bug: Library breaks on lambda #4

Closed spacemandev-git closed 4 years ago

spacemandev-git commented 4 years ago

Describe the bug

I tried the same code locally as I did on aws lambda, and the lambda container could not execute. I am using a custom runtime (node v12.13) from here (https://github.com/lambci/node-custom-lambda) as the only official lambda runtimes are v8 and v10 but I don't think that's the issue.

To Reproduce Here's a sample lambda function to test the library in that I'm using

export const test: APIGatewayProxyHandler = async (_event, _context) => {
  let response = {statusCode:200, headers: {}, body: "{}"}
  //console.log(_event)
  let k2 = await V2.generateKey('public')
  let token = await V2.sign({'test': '12'}, k2)
  console.log("new token: ", token)
  let pub = crypto.createPublicKey(k2)
  let req = await V2.verify(token, pub)
  console.log("req: ", req);

  return response;
}

Expected behavior When used locally this code creates and verifies a token

Environment:

Bug:

events.js:187
throw er; // Unhandled 'error' event
^
Error: Cannot find module '/index.js'
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:794:15)
at Function.Module._load (internal/modules/cjs/loader.js:687:27)
at Function.Module.runMain (internal/modules/cjs/loader.js:1025:10)
at MessagePort.<anonymous> (internal/main/worker_thread.js:139:25)
at MessagePort.emit (events.js:210:5)
at MessagePort.onmessage (internal/worker/io.js:70:8)
Emitted 'error' event on Worker instance at:
at Worker.[kOnErrorMessage] (internal/worker.js:176:10)
at Worker.[kOnMessage] (internal/worker.js:186:37)
at MessagePort.<anonymous> (internal/worker.js:118:57)
at MessagePort.emit (events.js:210:5)
at MessagePort.EventEmitter.emit (domain.js:476:20)
at MessagePort.onmessage (internal/worker/io.js:70:8) {
code: 'MODULE_NOT_FOUND',
requireStack: []
}
panva commented 4 years ago

Doesn't seem like the error is coming from paseto, but rather, node. Maybe the worker_threads module is not available in that build?

spacemandev-git commented 4 years ago

Going to retry with an official v12 runtime when that becomes available on lambda

panva commented 4 years ago

Works fine in the official node12.x runtime.

START RequestId: d8726aa1-4c84-44f5-895c-090498e0b76d Version: $LATEST
2019-11-22T13:29:41.688Z    d8726aa1-4c84-44f5-895c-090498e0b76d    INFO    issued token: v2.public.eyJ0ZXN0IjoxMiwiaWF0IjoiMjAxOS0xMS0yMlQxMzoyOTo0MS42ODVaIn20kNcGmftZH2XPSek6tGmzi_Xo1W9eLV1RnMflIDJAGCqsFCw0ewH7Xo3ZqV8G_a5nxndRgG_rmRzkQUPKp0AI 
2019-11-22T13:29:41.688Z    d8726aa1-4c84-44f5-895c-090498e0b76d    INFO    verified: {
  payload: { test: 12, iat: '2019-11-22T13:29:41.685Z' },
  footer: undefined,
  version: 'v2',
  purpose: 'public'
} 
END RequestId: d8726aa1-4c84-44f5-895c-090498e0b76d
REPORT RequestId: d8726aa1-4c84-44f5-895c-090498e0b76d  Duration: 4.98 ms   Billed Duration: 100 ms Memory Size: 1024 MB    Max Memory Used: 93 MB  
const { V2 } = require('paseto')

module.exports.handler = async (event, context) => {
  const key = await V2.generateKey('public')
  const token = await V2.sign({ test: 12 }, key)
  console.log('issued token:', token, '\n')
  const verified = await V2.verify(token, key, { complete: true })
  console.log('verified:', verified, '\n')
}