Skip to content

Commit

Permalink
[patch] fix: risk of getting fluid requests
Browse files Browse the repository at this point in the history
  • Loading branch information
rnmeow committed Nov 2, 2024
1 parent 9d95318 commit 6799cf7
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 2 deletions.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@
"wrangler": "^3.81.0"
},
"dependencies": {
"@hono-rate-limiter/cloudflare": "^0.2.1",
"hono": "4.6.5",
"hono-rate-limiter": "^0.4.0",
"nanoid": "^5.0.7"
}
}
26 changes: 26 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 18 additions & 2 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,34 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

import { Hono } from 'hono'
import { Hono, type Context, type Next } from 'hono'
import { RegExpRouter } from 'hono/router/reg-exp-router'
import { HTTPException } from 'hono/http-exception'

import { rateLimiter } from 'hono-rate-limiter'
import {
DurableObjectStore,
DurableObjectRateLimiter,
} from '@hono-rate-limiter/cloudflare'

import { handlers as rootHandlers } from '@/routes/root'
import { handlers as redirectHandlers } from '@/routes/[slug]'
import { handlers as shortenHandlers } from '@/routes/api/shorten'
import { handlers as revokeHandlers } from '@/routes/api/revoke'

import { genHttpException } from '@/errors/http_error'

const app = new Hono({ router: new RegExpRouter() })
const app = new Hono<{
Bindings: { CACHE: DurableObjectNamespace<DurableObjectRateLimiter> }
}>({ router: new RegExpRouter() }).use((ctxt: Context, next: Next) =>
rateLimiter({
windowMs: 10 * 60 * 1000, // 10 mins
limit: 10,
standardHeaders: 'draft-6',
keyGenerator: (_ctxt) => crypto.randomUUID(),
store: new DurableObjectStore({ namespace: ctxt.env.CACHE }),
})(ctxt, next),
)

app
.get('/', ...rootHandlers)
Expand Down
4 changes: 4 additions & 0 deletions src/routes/api/shorten.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { createFactory } from 'hono/factory'
import { logger } from 'hono/logger'

import { nanoid } from 'nanoid/non-secure'

import { baseUrl, randSlugSize } from '@/conf'
Expand Down Expand Up @@ -75,6 +76,9 @@ export const handlers = factory.createHandlers(logger(), async (ctxt) => {
)
}

// TEMP SOLUTION SINCE TOKEN ISN'T DESIGNED
ctxt.header('Referrer-Policy', 'strict-origin-when-cross-origin')

return ctxt.json<
JsonResp & {
shortenedUrl: string
Expand Down
8 changes: 8 additions & 0 deletions wrangler.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,11 @@ assets = { directory = "./www/", binding = "ASSETS" }
binding = "DB"
database_name = "url-shortner-db"
database_id = "83e2a789-0639-4e3b-b2e3-545deb75a421"

[[durable_objects.bindings]]
name = "CACHE"
class_name = "DurableObjectRateLimiter"

[[migrations]]
tag = "v0"
new_classes = ["DurableObjectRateLimiter"]

0 comments on commit 6799cf7

Please sign in to comment.