Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

urlEncodePathParser #438

Merged
merged 6 commits into from
Dec 9, 2019
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -586,6 +586,7 @@ Currently available middlewares:
- [`http-partial-response`](/packages/http-partial-response): Filter response objects attributes based on query string parameters.
- [`http-security-headers`](/packages/http-security-headers): Applies best practice security headers to responses. It's a simplified port of HelmetJS.
- [`http-urlencode-body-parser`](/packages/http-urlencode-body-parser): Automatically parses HTTP requests with URL encoded body (typically the result of a form submit).
- [`http-urlencode-path-parser`](/packages/http-urlencode-path-parser): Automatically parses HTTP requests with URL encoded path.
- [`s3-key-normalizer`](/packages/s3-key-normalizer): Normalizes key names in s3 events.
- [`secrets-manager`](/packages/secrets-manager): Fetches parameters from [AWS Secrets Manager](https://docs.aws.amazon.com/secretsmanager/latest/userguide/intro.html).
- [`ssm`](/packages/ssm): Fetches parameters from [AWS Systems Manager Parameter Store](https://docs.aws.amazon.com/systems-manager/latest/userguide/systems-manager-paramstore.html).
Expand Down
92 changes: 92 additions & 0 deletions packages/http-urlencode-path-parser/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
# Middy http-urlencode-body-parser middleware

<div align="center">
<img alt="Middy logo" src="https://github.com/middyjs/middy/master/img/middy-logo.png"/>
</div>

<div align="center">
<p><strong>HTTP URLencode body parser middleware for the middy framework, the stylish Node.js middleware engine for AWS Lambda</strong></p>
</div>

<div align="center">
<p>
<a href="http://badge.fury.io/js/%40middy%2Fhttp-urlencode-path-parser">
<img src="https://badge.fury.io/js/%40middy%2Fhttp-urlencode-path-parser.svg" alt="npm version" style="max-width:100%;">
</a>
<a href="https://snyk.io/test/github/middyjs/middy">
<img src="https://snyk.io/test/github/middyjs/middy/badge.svg" alt="Known Vulnerabilities" data-canonical-src="https://snyk.io/test/github/middyjs/middy" style="max-width:100%;">
</a>
<a href="https://standardjs.com/">
<img src="https://img.shields.io/badge/code_style-standard-brightgreen.svg" alt="Standard Code Style" style="max-width:100%;">
</a>
<a href="https://greenkeeper.io/">
<img src="https://badges.greenkeeper.io/middyjs/middy.svg" alt="Greenkeeper badge" style="max-width:100%;">
</a>
<a href="https://gitter.im/middyjs/Lobby">
<img src="https://badges.gitter.im/gitterHQ/gitter.svg" alt="Chat on Gitter" style="max-width:100%;">
</a>
</p>
</div>

This middleware automatically parses HTTP requests with URL-encoded paths. This can happen when using path variables (ie `/{name}/`) for an endpoint and the UI `encodeURIComponent` the values before making the request.


## Install

To install this middleware you can use NPM:

```bash
npm install --save @middy/http-urlencode-path-parser
```


## Options

None


## Sample usage

```javascript
const middy = require('@middy/core')
const httpUrlEncodePathParser = require('@middy/http-urlencode-path-parser')

const handler = middy((event, context, cb) => {
cb(null, event.body) // propagates the body as response
})

handler.use(httpUrlEncodePathParser({extended: false}))
Copy link
Contributor

Choose a reason for hiding this comment

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

You don't utilize extended field anywhere

Copy link
Member Author

Choose a reason for hiding this comment

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

it was copied from body parser, removed.


// When Lambda runs the handler with a sample event...
const event = {

pathParameters: {
name: encodeURIComponent('Mîddy')
}
}

handler(event, {}, (_, body) => {
expect(body).toEqual({
name: 'Mîddy'
})
})
```


## Middy documentation and examples

For more documentation and examples, refers to the main [Middy monorepo on GitHub](https://github.com/middyjs/middy) or [Middy official website](https://middy.js.org).


## Contributing

Everyone is very welcome to contribute to this repository. Feel free to [raise issues](https://github.com/middyjs/middy/issues) or to [submit Pull Requests](https://github.com/middyjs/middy/pulls).


## License

Licensed under [MIT License](LICENSE). Copyright (c) 2017-2018 Luciano Mammino and the [Middy team](https://github.com/middyjs/middy/graphs/contributors).

<a href="https://app.fossa.io/projects/git%2Bgithub.51.al%2Fmiddyjs%2Fmiddy?ref=badge_large">
<img src="https://app.fossa.io/api/projects/git%2Bgithub.51.al%2Fmiddyjs%2Fmiddy.svg?type=large" alt="FOSSA Status" style="max-width:100%;">
</a>
25 changes: 25 additions & 0 deletions packages/http-urlencode-path-parser/__tests__/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
const middy = require('../../core')
const urlEncodePathParser = require('../')

describe('📦 Middleware URL Encoded Path Parser', () => {
test('It should decode simple url encoded requests', () => {
Copy link
Contributor

Choose a reason for hiding this comment

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

I would add test for bad case as well here

Copy link
Member Author

Choose a reason for hiding this comment

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

Added in the example from #429

const handler = middy((event, context, cb) => {
cb(null, event.pathParameters) // propagates the body as response
})

handler.use(urlEncodePathParser())

// invokes the handler
const event = {
pathParameters: {
char: encodeURIComponent('Mîddy')
}
}

handler(event, {}, (_, body) => {
expect(body).toEqual({
char: 'Mîddy'
})
})
})
})
5 changes: 5 additions & 0 deletions packages/http-urlencode-path-parser/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import middy from '../core'

declare function urlEncodePathParser(): middy.IMiddyMiddlewareObject;

export default urlEncodePathParser
15 changes: 15 additions & 0 deletions packages/http-urlencode-path-parser/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module.exports = () => ({
before: (handler, next) => {
if (handler.event.pathParameters) {
for (let key in handler.event.pathParameters) {
try {
handler.event.pathParameters[key] = decodeURIComponent(handler.event.pathParameters[key])
} catch (e) {
console.error(e)
Copy link
Contributor

Choose a reason for hiding this comment

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

How about you introduce flag here, for example shouldThrow and check it here, or better yet, maybe errorHandler, like:

handler.use(urlEncodePathParser({
    onError: (event, context) => {
         // do something here
    }
}))

Just a suggestion, this is something that I would find useful if I were to use this plugin - otherwise you won't know if this function worked or not

Copy link
Member Author

Choose a reason for hiding this comment

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

I opted to a throw an error, so that the error handler middleware can take care of it.

}
}
}

next()
}
})
Loading