@meith/notifications 0.29.0 → 0.30.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/package.json +4 -4
  2. package/src/push.ts +22 -1
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@meith/notifications",
3
- "version": "0.29.0",
3
+ "version": "0.30.0",
4
4
  "license": "MIT",
5
5
  "repository": {
6
6
  "type": "git",
@@ -19,8 +19,8 @@
19
19
  "access": "public"
20
20
  },
21
21
  "dependencies": {
22
- "@meith/core": "0.29.0",
23
- "@meith/i18n": "0.29.0",
24
- "@meith/mail": "0.29.0"
22
+ "@meith/core": "0.30.0",
23
+ "@meith/i18n": "0.30.0",
24
+ "@meith/mail": "0.30.0"
25
25
  }
26
26
  }
package/src/push.ts CHANGED
@@ -1,5 +1,8 @@
1
1
  import { createCipheriv, hkdfSync, randomBytes } from 'node:crypto'
2
2
 
3
+ import { env, isProduction } from '@meith/core'
4
+ import { assertAllowedUrl, guardedRequest } from '@meith/core/outbound'
5
+
3
6
  export const PUSH_PAYLOAD_LIMIT = 3000
4
7
 
5
8
  export const PUSH_RECORD_SIZE = 4096
@@ -182,6 +185,24 @@ export function pushOutcomeFor(status: number): PushSendOutcome {
182
185
  return 'failed'
183
186
  }
184
187
 
188
+ export function pushAllowsPrivateHosts(): boolean {
189
+ return env.PUSH_ALLOW_PRIVATE_HOSTS || !isProduction()
190
+ }
191
+
192
+ const guardedPushFetch: typeof fetch = (async (url: string, init: RequestInit) => {
193
+ const allowPrivateHosts = pushAllowsPrivateHosts()
194
+ const target = assertAllowedUrl(url, { allowPrivateHosts })
195
+ const { status } = await guardedRequest({
196
+ url: target,
197
+ method: 'POST',
198
+ headers: (init.headers ?? {}) as Record<string, string>,
199
+ body: init.body as Uint8Array,
200
+ timeoutMs: PUSH_REQUEST_TIMEOUT_MS,
201
+ allowPrivateHosts,
202
+ })
203
+ return { status } as Response
204
+ }) as unknown as typeof fetch
205
+
185
206
  export async function sendWebPush(input: {
186
207
  readonly subscription: PushSubscriptionKeys
187
208
  readonly payload: string
@@ -190,7 +211,7 @@ export async function sendWebPush(input: {
190
211
  readonly now?: Date
191
212
  readonly fetchImpl?: typeof fetch
192
213
  }): Promise<PushSendResult> {
193
- const doFetch = input.fetchImpl ?? fetch
214
+ const doFetch = input.fetchImpl ?? guardedPushFetch
194
215
 
195
216
  let encrypted: EncryptedPushPayload
196
217
  let authorization: string