matrix-org / matrix-appservice-discord

A bridge between Matrix and Discord.
Apache License 2.0
791 stars 148 forks source link

Bridge crashing with M_FORBIDDEN #831

Closed TheNamelessWonderer closed 1 year ago

TheNamelessWonderer commented 1 year ago

Describe the bug The bridge is crashing with M_FORBIDDEN error. I do not use t2bot.io so I don't know why this error is appearing.

To Reproduce Steps to reproduce the behavior:

  1. Start bot
  2. Invite to room or try to send message
  3. Open Logs
  4. See error

Expected behavior The bot should not crash

Logs

Aug-12 18:08:06.817 [bot-sdkMatrixHttpClient] error: [
  '(REQ-2)',
  {
    errcode: 'M_FORBIDDEN',
    error: 'Application service has not registered this user (@_discord_472521620452278292:t2bot.io)'
  }
]

/node_modules/matrix-bot-sdk/src/http.ts:110
        throw new MatrixError(errBody, response.statusCode);
              ^
Error: M_FORBIDDEN: Application service has not registered this user (@_discord_472521620452278292:t2bot.io)
    at doHttpRequest (/node_modules/matrix-bot-sdk/src/http.ts:110:15)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at MatrixClient.descriptor.value (/node_modules/matrix-bot-sdk/src/metrics/decorators.ts:28:32)
    at MatrixClient.descriptor.value (/node_modules/matrix-bot-sdk/src/metrics/decorators.ts:28:32)
    at Intent.refreshJoinedRooms (/node_modules/matrix-bot-sdk/src/appservice/Intent.ts:296:33)
    at Intent.descriptor.value (/node_modules/matrix-bot-sdk/src/metrics/decorators.ts:83:32)
    at Appservice.processMembershipEvent (/node_modules/matrix-bot-sdk/src/appservice/Appservice.ts:616:9)
    at /node_modules/matrix-bot-sdk/src/appservice/Appservice.ts:841:21

Additional context The bridge has been working fine for months before this error. Now it won't join rooms or sent from matrix to discord. Discord to matrix works fine though.

Half-Shot commented 1 year ago

Out of interest, what does your registration file namespace section look like?

TheNamelessWonderer commented 1 year ago

It is the default:

namespaces:
  aliases:
    - exclusive: true
      regex: '#_discord_.*'
  rooms: []
  users:
    - exclusive: true
      regex: '@_discord_.*'
TheNamelessWonderer commented 1 year ago

I regenerated the discord-registration.yaml file and changed the regex to @_discord_.*:domain.org. This seems to have fixed it! Not sure if this should be added to the readme or not. I'll close it for now and will reopen it if it happens again. Thank you!

Akselmo commented 1 year ago

Thanks, this seemed to fix the issue on my end too. If your discord database has old t2bot.io profiles, the bridge tries to use those as well even you dont own them.

TheNamelessWonderer commented 1 year ago

Reopening this as it is happening to other people. I think either the readme needs to be updated or the program that generates the file needs to be changed.

jlu5 commented 1 year ago

Just ran into this as well.. How are t2bot.io profiles getting into my DB in the first place?

edit: sqlite3 db/discord.db .dump | grep -i t2bot returns no matches, so maybe there's some incorrect user lookup or fallback being used?

jlu5 commented 1 year ago

For completeness, here are the errors I see in debug.log:

Aug-15 00:14:06.920 [bot-sdkAppservice] ESC[32minfoESC[39m: [ 'Processing transaction 47324' ]
Aug-15 00:14:06.923 [bot-sdkAppservice] ESC[32minfoESC[39m: [ 'Processing event of type m.room.member' ]
Aug-15 00:14:06.946 [bot-sdkMatrixHttpClient] ESC[31merrorESC[39m: [
  '(REQ-1004)',
  {
    errcode: 'M_FORBIDDEN',
    error: 'Application service has not registered this user (@_discord_795964964799119391:t2bot.io)'
  }
]
Aug-15 23:24:10.298 [bot-sdkAppservice] ESC[32minfoESC[39m: [ 'Processing transaction 47324' ]
Aug-15 23:24:10.299 [bot-sdkAppservice] ESC[32minfoESC[39m: [ 'Processing event of type m.room.member' ]
Aug-15 23:24:10.310 [bot-sdkMatrixHttpClient] ESC[31merrorESC[39m: [
  '(REQ-1006)',
  {
    errcode: 'M_FORBIDDEN',
    error: 'Application service cannot masquerade as this user (@_discord_795964964799119391:t2bot.io).'
  }
]

Neither 795964964799119391 nor t2bot appear in my DB, and it's not clear to me where the UID is coming from.

turt2live commented 1 year ago

It'll be from another room on your server - the hint is "Processing event of type m.room.member". Essentially, because of the wide namespace in the registration, the bridge is being sent events related to any discord bridge and the bridge appears to have forgotten it's not supposed to do anything with ones not from its server.

jlu5 commented 1 year ago

ahh, indeed, updating the registration file as mentioned in https://github.com/matrix-org/matrix-appservice-discord/issues/831#issuecomment-1213460270 fixes the issue. (I forgot that the setup guide tells to copy that file into the homeserver config file, and I forgot to change both copies)

Resetting / clearing the DB does not seem to be necessary.