dherault / dherault/serverless-offline
`sls-offline-authorizer-override` header and `AUTHORIZER` env var do not populate `event.requestContext.authorizer` when using `request`-type custom authorizer
- Dominant language
- JavaScript
- Stars
- 5.3k
- Forks
- 811
- Avg merge
- 2d 4h
- Merged PRs (30d)
- 3
Description
## Bug Report
**Current Behavior**
The `sls-offline-authorizer-override` custom header and the `AUTHORIZER` environment variable both fail to populate `event.requestContext.authorizer` regardless of the `noAuth` setting in `serverless.yml`.
- With `noAuth: true`: the header `sls-offline-authorizer-override` is received but ends up in `event.headers` instead of `event.requestContext.authorizer`. The authorizer context is filled with `offlineContext_*` defaults, ignoring the override entirely.
- With `noAuth: false`: the server returns `401` because the real authorizer is invoked, making local development without valid credentials impossible.
- The `AUTHORIZER` env var (documented as a way to mock remote authorizers) also has no effect, as reported in #1826.
This makes it impossible to test functions locally that depend on `event.requestContext.authorizer` values without resorting to workarounds.
**Sample Code**
- file: `serverless.yml`
```yaml
service: my-service
plugins:
- serverless-offline
provider:
name: aws
runtime: nodejs20.x
stage: dev
functions:
myAuthorizer:
handler: functions/authorizer.main
timeout: 200
hello:
handler: functions/hello.main
events:
- http:
path: /hello/{id}
method: get
authorizer:
name: myAuthorizer
identitySource: method.request.header.Authorization
resultTtlInSeconds: 0
type: request
custom:
serverless-offline:
noAuth: true
noTimeout: true
```
- file: `functions/hello.js`
```js
exports.main = async (event) => {
const allowCpf = event.requestContext.authorizer.allowCpf
if (!allowCpf) return { statusCode: 400, body: JSON.stringify({ error: 'allowCpf missing' }) }
return {
statusCode: 200,
body: JSON.stringify({ allowCpf }),
}
}
```
Request made with:
```bash
curl http://localhost:3000/dev/hello/123 \
--header 'sls-offline-authorizer-override: {"allowCpf": "12345678900"}'
```
**Expected behavior**
`event.requestContext.authorizer` should be `{ allowCpf: "12345678900" }` as set via the `sls-offline-authorizer-override` header.
**Actual behavior**
`event.requestContext.authorizer` contains only `offlineContext_*` defaults. The override header is present in `event.headers` but never applied to the authorizer context.
**Environment**
- `serverless` version: 3.40.0
- `serverless-offline` version: 7.2.3
- `node.js` version: 24.14.1
- `OS`: Windows 11
**Additional context**
Related to #1826, which reports the same broken behavior for the `AUTHORIZER` env var since v13.x. Both mocking mechanisms (`sls-offline-authorizer-override` header and `AUTHORIZER` env var) appear to be broken when a `request`-type custom authorizer is defined, regardless of the `noAuth` configuration.
Contributor guide
Assessment
This issue has not been assessed yet.