@fabrika/github-app 0.0.7 → 0.0.8

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 +1 -1
  2. package/src/index.ts +28 -13
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fabrika/github-app",
3
- "version": "0.0.7",
3
+ "version": "0.0.8",
4
4
  "type": "module",
5
5
  "repository": {
6
6
  "type": "git",
package/src/index.ts CHANGED
@@ -201,7 +201,11 @@ export class GitHubAppClient {
201
201
  if (body === NOT_FOUND) throw responseError()
202
202
  const identity = decodeAppIdentity(body)
203
203
  if (identity === null || String(identity.id) !== this.appId) throw responseError()
204
- return identity
204
+ const publicBody = await this.requestJson(`/apps/${identity.slug}`, {}, signal, true, false)
205
+ if (publicBody === NOT_FOUND) return { ...identity, public: false }
206
+ const publicIdentity = decodeAppIdentity(publicBody)
207
+ if (publicIdentity === null || !sameAppIdentity(identity, publicIdentity)) throw responseError()
208
+ return { ...identity, public: true }
205
209
  }
206
210
 
207
211
  /** Read the App webhook transport settings without exposing GitHub's masked secret field. */
@@ -301,20 +305,23 @@ export class GitHubAppClient {
301
305
  init: RequestInit,
302
306
  callerSignal: AbortSignal | undefined,
303
307
  allowNotFound: boolean,
308
+ authenticate = true,
304
309
  ): Promise<unknown | typeof NOT_FOUND> {
305
310
  const cancellation = createCancellation(callerSignal, this.timeoutMs)
306
311
  try {
307
312
  cancellation.throwIfCancelled()
308
- let jwt: string
309
- try {
310
- jwt = await this.signAppJwt()
311
- } catch (error) {
313
+ const headers = new Headers(init.headers)
314
+ if (authenticate) {
315
+ let jwt: string
316
+ try {
317
+ jwt = await this.signAppJwt()
318
+ } catch (error) {
319
+ cancellation.throwIfCancelled()
320
+ throw error
321
+ }
312
322
  cancellation.throwIfCancelled()
313
- throw error
323
+ headers.set('authorization', `Bearer ${jwt}`)
314
324
  }
315
- cancellation.throwIfCancelled()
316
- const headers = new Headers(init.headers)
317
- headers.set('authorization', `Bearer ${jwt}`)
318
325
  headers.set('accept', GITHUB_ACCEPT)
319
326
  headers.set('x-github-api-version', GITHUB_API_VERSION)
320
327
  headers.set('user-agent', GITHUB_USER_AGENT)
@@ -743,11 +750,10 @@ function validateWebhookSecret(value: string): void {
743
750
  if (value.length === 0 || value.length > 4096 || hasAsciiControl(value)) throw configurationError()
744
751
  }
745
752
 
746
- function decodeAppIdentity(value: unknown): GitHubAppIdentity | null {
753
+ function decodeAppIdentity(value: unknown): Omit<GitHubAppIdentity, 'public'> | null {
747
754
  const id = objectField(value, 'id')
748
755
  const slug = objectField(value, 'slug')
749
756
  const htmlUrl = objectField(value, 'html_url')
750
- const publicApp = objectField(value, 'public')
751
757
  const permissions = objectField(value, 'permissions')
752
758
  const contents = objectField(permissions, 'contents')
753
759
  const events = objectField(value, 'events')
@@ -757,7 +763,7 @@ function decodeAppIdentity(value: unknown): GitHubAppIdentity | null {
757
763
  if (
758
764
  typeof id !== 'number' || !isPositiveSafeInteger(id) || typeof slug !== 'string' || !APP_SLUG_PATTERN.test(slug)
759
765
  || typeof htmlUrl !== 'string' || typeof login !== 'string' || login.length === 0 || login.length > 100
760
- || !REPOSITORY_COMPONENT_PATTERN.test(login) || (type !== 'Organization' && type !== 'User') || typeof publicApp !== 'boolean'
766
+ || !REPOSITORY_COMPONENT_PATTERN.test(login) || (type !== 'Organization' && type !== 'User')
761
767
  || contents !== 'read' || !hasExactAppPermissions(permissions) || !isStringArray(events) || !events.includes('push')
762
768
  ) {
763
769
  return null
@@ -775,7 +781,16 @@ function decodeAppIdentity(value: unknown): GitHubAppIdentity | null {
775
781
  ) {
776
782
  return null
777
783
  }
778
- return { id, slug, htmlUrl, public: publicApp, permissions: { contents }, events, owner: { login, type } }
784
+ return { id, slug, htmlUrl, permissions: { contents }, events, owner: { login, type } }
785
+ }
786
+
787
+ function sameAppIdentity(
788
+ left: Omit<GitHubAppIdentity, 'public'>,
789
+ right: Omit<GitHubAppIdentity, 'public'>,
790
+ ): boolean {
791
+ return left.id === right.id && left.slug === right.slug && left.htmlUrl === right.htmlUrl
792
+ && left.owner.login === right.owner.login && left.owner.type === right.owner.type
793
+ && left.events.length === right.events.length && left.events.every((event, index) => event === right.events[index])
779
794
  }
780
795
 
781
796
  function decodeWebhookConfig(value: unknown): GitHubAppWebhookConfig | null {