Commit Graph

129 Commits

Author SHA1 Message Date
gnuxie e17ebe1ff0 Fix abuseReportTest (it works!). 2024-04-06 20:03:36 +01:00
gnuxie d718967a7c Fix hello test (it works).
Ok so this is pretty shit, i hate the integration test suite now.

The reason why we return the test functions with `as any` in the
hello test is because we had to remove `Record<string, any>` from
mocha's test context interface, otherwise the interface would
have been completely useless. Maybe there is a ts setting though
to not infer any from `this` at all? and just ignore those properties.

The tsconfig.json situation is a bit weird, i don't understand why
it's in this situation. However, it seems like we can try to
https://github.com/jaredpalmer/tsdx/issues/84#issuecomment-489690504
use this workaround so that ts language features work in the test
directory.

I think we should focus on doing as little effort as possible getting
these tests into working condition. If something is too complicated,
it will need removing. If we need to make additional tests,
this entire integration tests directory should be moved
to a legacy-integration directory and we can start afresh.

We should also ideally not integration tests as much as possible
and try to reuse the unit helpers from MPS.
This is even going to be critical later on.
2024-04-06 20:03:36 +01:00
gnuxie cc97c8550e Naively fix appservice integration tests. 2024-04-06 20:03:36 +01:00
gnuxie 3eb3bae085 Instantiate web apis in an appropriate place. 2024-04-06 20:03:36 +01:00
gnuxie 0c2d391ff4 Fix inverted boolean logic in CommandReader.
We couldn't read room references.
2024-04-06 20:03:35 +01:00
gnuxie ac4c447637 Start client in manual launch script.
Not doing this would just cause draupnir to exit.
2024-04-06 20:03:35 +01:00
gnuxie c6f198303d Start fixes to dev environment 2024-04-06 20:03:35 +01:00
gnuxie 6d28ac81b0 Remove the RuleServer.
It was never used and we don't test it.
We can't really support it.
2024-04-06 20:03:32 +01:00
Gnuxie bef21850e8 Use --forbid-only flag in integration test suite. (#97)
Yes this has happened again, at least we know how to fix it now.
2023-09-07 14:13:26 +01:00
gnuxie 73601687a8 Add UnbanPropagation BanPropagationProtection.
https://github.com/Gnuxie/Draupnir/pull/93/
2023-09-05 19:53:02 +01:00
Gnuxie 0a7305b8fa A simple script for testing room membership query. (#81)
https://github.com/Gnuxie/Draupnir/issues/80
2023-08-29 13:56:33 +01:00
Aminda Suomalainen e4c02b96cd Add pre-commit configuration (#34)
* add .pre-commit-config.yaml

Signed-off-by: Aminda Suomalainen <suomalainen+git@mikaela.info>

* .editorconfig: decrease indent size for text

* .pre-commit-config.yaml: remove prettier

Signed-off-by: Aminda Suomalainen <suomalainen+git@mikaela.info>

* .editorconfig consistency.

* .pre-commit-config.yaml: restore sample hooks

* .editorconfig: disable indent_size for LICENSE & NOTICE

* pre-commit run --all-files

* tsconfig.json: tabs to spaces

* .pre-commit-config.yaml: update editorconfig-checker to 2.7.2

* .editorconfig: disable indent_size for markdown

* mjolnir-entrypoint.sh: retab

* .editorconfig: also exclude json from indent checking

* test/nginx.conf: retab

* test/integration/commands/redactCommandTest.ts: remove leading space

* retab or remove leading whitespaces for the rest of the files

* src/appservice/datastore.ts remove newlines

* test/integration/commands/roomTest.ts: remove leading space.

---------

Signed-off-by: Aminda Suomalainen <suomalainen+git@mikaela.info>
Co-authored-by: gnuxie <Gnuxie@protonmail.com>
2023-08-29 13:38:00 +01:00
Gnuxie 56ddbdf96c Failure to ban a single member should not stop other bans being applied. (#79)
Fixes https://github.com/Gnuxie/Draupnir/issues/74.
2023-08-28 16:38:24 +01:00
Gnuxie c11fb6ef06 Fix an issue where protected rooms could not be removed. (#65)
This was introduced in https://github.com/Gnuxie/Draupnir/pull/54/ (and therefore 1.83.0). Essentially we forgot to remove the room from the protected rooms set, when the remove command was used.

Ontop of this something to note is that during testing it is clear that the protected rooms set is loaded when configuring mjolnir, not when starting it. This is problematic as it means setup code in `fixtures.ts` does not actually wipe the protected rooms set.
2023-07-04 17:19:03 +01:00
Gnuxie c16c5d29e1 Integration tests convenience nginx should listen to ipv6 too. 2023-05-13 15:21:21 +01:00
Gnuxie 99e6f168ca Appservice convenience (#47)
Canonicalise the existence of the "admin room" for managing the appservice and Draupnir instances

* Add utilities for managing users in the admin room

* Merge the appservice admin room and access control list.

The majority of admins will need to use the draupnir admin commands
to manage the list.

* Utility methods for creating generic rules in PolicyLists.

* Commands for managing appservice users.
2023-05-03 16:31:28 +01:00
Gnuxie 924bed5813 PolicyListManager watch/unwatch should use MatrixRoomReferences. (#42) 2023-03-28 19:55:34 +01:00
gnuxie 492230d61d Update references to a now non-existant IProtection in tests. 2023-03-24 18:57:25 +00:00
gnuxie 81b0c9a20d Yeah i don't have to spoons for this test matey.
If you're reading this commit and this is the hill you want to die on
for judging Gnuxie then lol go die on it.
2023-03-24 18:57:25 +00:00
gnuxie 1a73492178 Update protectionSettingsTest for Protection changes.
Worried about when IProtection existed and when it was removed??
Idk but this wasn't going great to begin with.
2023-03-24 18:57:25 +00:00
gnuxie 598f5483d9 type guards for updated Permalink helper 2023-03-09 15:20:52 +00:00
Gnuxie 5414c46c36 Ban Propagation protection (that is enabled by default) (#36)
* Experimental Protection to propagate room level bans to policies.

- Needs an automated option
- I really want this to be enabled by default
- It needs to be easily configurable and very visible because it's a really useful feature.
- Need to check that they are not already banned on a policy list.
- Allow possibility to rely last message like a report behind spoiler text.

* Use MatrixDataManager for enabled protections.

This will allow us to create "enabled by default" protections
via a schema migration.

* Enable BanPropagationProtection by default

* BanPropagation: only prompt when user is not already banned.

* Test for BanPropagationProtection.

* clearTimeout for prompt reactions if we got a reaction.

* Allow renderMatrixAndSend to not need a reply.

* document getFirstEventMatching
2023-02-22 16:43:24 +00:00
gnuxie 8157bae07a Migrate status command to use interface-manager. 2023-02-11 14:20:46 +00:00
gnuxie 1257b26306 Rename Parmater Parameter
Yeah don't ask.
2023-02-08 13:10:49 +00:00
Gnuxie c936332442 Ban/Unban rework + Prompts for missing arguments (#12)
* basic ban conversion, but i have better ideas

* Still very WIP on CLIM prompt-for-accept semantics.

* Introduce promotable streams.

This allows parameters to specify details to prompt for missing
arguments
and allow for interactive commands.

* Changes that were made before PolicyListManager that no longer make sense

We don't want the default list anymore since we're just going to prompt
with the lists that they can choose from.

* Fix semantics of TagDynamicEnvironment.

Bind and write were wrong and bind was binding to the node name
instead of the variable name.

* The JSX factory can render presentation types to DocumentNodes, unsure if this is the right
move yet but it works

* Attributes for anchor nodes now render properly

* Ban command prompts are working!!!!

* Stub AppserviceBotEmitter.

There isn't much we can do right now until there is time to work on
https://github.com/Gnuxie/Draupnir/issues/13.

* Combine ban/unban syntax.

* Remove old UnbanBanCommands.

WARNING: There is a major difference in that the ban command no longer supports
globs, I don't think?

* Activate new unban command.

* The presentation type boolean will have to be just a string for now.

I don't think it makes sense to read them into actual booleans.

* configurable defaults for ban reason.
2023-02-08 12:50:23 +00:00
David Teller 9e9b48700d This should hopefully fix some startup woes (#462)
Splitting PolicyListManager from Mjolnir, making it more resilient to startup errors
2023-01-31 20:12:04 +00:00
gnuxie 5164e48149 Rename MakeRoomAdminCommand to HijackRoomCommand 2023-01-31 20:00:14 +00:00
gnuxie 277140240a Catch exceptions from commands better by giving them a uuid.
They can then be used as a reference to look for in the logs.
The MakeAdminCommand has been refactored as an example.
2023-01-31 19:57:56 +00:00
gnuxie 7a076033a3 Protect Draupnir from matrix-bot-sdk 2023-01-31 18:25:42 +00:00
gnuxie b3f9fbe0af Remove stale test
Yes a test should probably be written for this, even if just as a debug
utility. This test just wasn't testing much regardless and
time needs to be put aside to do it properly.

The feature is still under development though.
2023-01-14 19:05:51 +00:00
gnuxie ccc17c4750 Add Utility for testing appservice commands in integration tests. 2023-01-14 19:00:33 +00:00
gnuxie beb9152b10 New command system working in appservice
This commit is NOT contributed under the Apache-2.0 License.
Copyright (C) 2022 Gnuxie <Gnuxie@protonmail.com>
All rights reserved.
2023-01-14 18:33:06 +00:00
gnuxie d7adaef0bf It's time for mjolnir-shell, a presentation style interface.
While this isn't a true presentation style interface,
the idea is that there is an argument stream to commands
that we pattern match commands against, and there are
different mediums that the commands can be invoked from.

There are translators between presentation types
and also between commands and mediums to do things like
render the result of the command for Matrix etc.

This is all inspired by the Common Lisp Interface Manager (CLIM).
But there are significant differences since, hello, this is
essentially being made for Matrix bots and appservices.

This commit is NOT contributed under the Apache-2.0 License.
Copyright (C) 2022 Gnuxie <Gnuxie@protonmail.com>
All rights reserved.
2023-01-14 18:33:06 +00:00
gnuxie 9e1cf659d2 Introduce the interface manager as a sub library.
This will want splitting out for other matrix bots to use eventually.
MatrixInterfaceCommand needs decoupling from Mjolnir and then it will
be added too.
2023-01-14 18:26:12 +00:00
gnuxie 98ea5a9586 Create a reader to assist parsing commands.
The idea is that the reader allows us to parse commands into
concrete types that will help us destructure/pattern match
against them later on.

This commit is NOT contributed under the Apache-2.0 License.
Copyright (C) 2022 Gnuxie <Gnuxie@protonmail.com>
All rights reserved.
2023-01-14 18:26:12 +00:00
Gnuxie 433ff7eadd A look at PolicyList.update (#454)
This started out as just a way to find out why mjolnir was syncing with lists several times for each update to a policy list.

The main changes are

- Verbosity was irrelevant to the sync command but for some reason was an option.
  Unfortunately all this did was suppress whether to tell you when it had finished, meaning it wouldn't
  when verbose logging was disabled. Historically this was probably a parameter that got passed through
  to applyServerAcl/applyUserBans, which can be horribly verbose, but they access the config directly.

- Stop emitting `'PolicyList.update'` when there are no changes.
- Include a revision ID for the `'PolicyList.update'`method and event.
- Use the revision ID in the `ProtectedRoomsSet` so that we don't unnecessarily resynchronize all rooms when the `'PolicyList.update'` event is received. Though not when the `sync` command is used. Since this is supposed to `sync` in the case when there is a state reset or otherwise or the user has changed some room settings.
- insert an await lock around the `PolicyList.update` method to avoid a race condition where a call can be started and finished within the extent of an existing call (via another task, this can happen if the server is slow with handling one request). `PolicyList.udpate` now has a helper that is synchronous to be called directly after requesting the room state. The reason for this is to enforce that no one `await`s while updating the policy list's cache of rules. Which is important because it is one of the biggest methods that I tolerate and visually checking for `await` is impossible.
- The revision ID uses a ULID, but this is unnecessary and could have just been a "dumb counter".

closes https://github.com/matrix-org/mjolnir/issues/447
2022-12-08 16:09:55 +00:00
Gnuxie 704bb660c2 Refactor how we listen for matrix events. (#446)
* Refactor Matrix event listener in Mjolnir and ManagedMjolnir.

closes https://github.com/matrix-org/mjolnir/issues/411.

Issue #411 says that we have to be careful about room.join,
but this was before we figured how to make matrix-appservice-bridge
echo events sent by its own intents.

* Remove MatrixClientListener since it isn't actually needed.

* Protect which config values can be used for ManagedMjolnirs.

* Introduce MatrixSendClient

so listeners aren't accidentally added to a MatrixClient instead
of MatrixEmitter.

* doc

* Move provisioned mjolnir config to src/config.

This just aids maintance so whenever someone goes to change the config
of the bot they will see this and update it.

* doc for matrix intent listener.
2022-12-06 17:17:40 +00:00
David Teller 2915757b7d Very basic support for Sentry. (#398)
The Sentry package is very useful for monitoring runtime errors. With this PR,
we simply add the necessary mechanism to:

- log to sentry any uncaught error that reaches the toplevel, including startup errors.
2022-11-30 16:06:02 +01:00
Gnuxie 38b18cda4f Appservice tests weren't added to tsconfig.json properly. (#440)
Some minor fixes now that they have been.
2022-11-28 12:55:43 +00:00
Jess Porter 50f80f2392 manymjolnir appservice (#364)
Mjolnir can now be run as an application service,
meaning it will host multiple independent mjolnirs that can be requested by users.
If the user is on the same homeserver as the appservice is deployed on,
then they can provision a mjolnir via a widget https://github.com/matrix-org/mjolnir-widget.
Otherwise they can invite the appservice bot to a room they want to protect.
This will create them a mjolnir, a management room and a policy list.

The appservice shares the same docker image as the bot,
but is started slightly differently by specifying "appservice"
as the first argument to docker run (this s managed by `mjolnir-entrypoint.sh`. 
We could have used another Dockerfile for the appservice,
extending the existing one but we decided not to because there
would have been lots of fiddling around the entrypoint
and logistics involved around adding a tag for it via github actions.
Not to mention that this would be duplicating the image
just to run it with a different binary.

A list of followup issues can be found here https://github.com/issues?q=is%3Aopen+is%3Aissue+author%3AGnuxie+archived%3Afalse+label%3AA-Appservice.

Somewhat relevant and squashed commit messages(regrettably squashing because frankly these won't make sense in isolation): 

* draft widget backend

* add `managementRoomId` to `provisionNewMjolnir`

* remove ratelimits from appservice mjolnirs

* add /join endpoint to api backend


* tighter guard around room type in PolicyList

matrix-bot-sdk imporved the types for this

* enable esModuleInterop

* launch and use postgres in a container whilst using mx-tester


* limited access control

policy list used for access control

* Redesign initialization API of many mjolnir.

It's much harder to forget to initialize the components now that you have to in order to construct them in the first place.


* Ammend config not to clash with existing CI

this means that the appsrvice bot is now called 'mjolnir-bot' by default
which was easier than going through old code base and renaming


* Change entrypoint in Dockerfile so that we can start the appservice.

We could have used another Dockerfile for the appservice,
extending the exising one but we decided not to because there
would have been lots of fiddling around the entrypoint
and logistics involved around adding a tag for it via github actions.
Not to mention that this would be duplicating the image
just to run it with a different binary.

This solution is much simpler, backwards compatible, and conscious about the future.


Co-authored-by: gnuxie <gnuxie@element.io>
2022-11-15 18:03:06 +00:00
David Teller 81cd91c250 Unbitrotting ruleserver tests (#418) 2022-11-15 13:06:41 +01:00
gnuxie a970733d0b Update roomMemberTest for removal of mjolnir.protectedRooms object.
https://github.com/matrix-org/mjolnir/issues/370.
2022-10-19 15:21:51 +01:00
gnuxie c537f2067e protected rooms config test
https://github.com/matrix-org/mjolnir/issues/370
2022-10-19 15:21:51 +01:00
Gnuxie da084328a9 Refactor list creation with MSC3784 support. (#386)
https://github.com/matrix-org/matrix-spec-proposals/pull/3784

This was extracted from the appservice mjolnir work to reduce review burden.
2022-10-19 10:33:12 +01:00
Gnuxie 5bd23ced9b Access Control Unit (#378)
The ACL unit allows you to combine an policy lists and conveniently test users and servers against them.
The main motivation for this work is provide access control on who can provision and continue to use mjolnir instances in the appservice component.
We include a new recommendation type org.matrix.mjolnir.allow which can be used with user and server entity types to create allow lists.
We have also replaced the destructing of policy lists in applyServerACL and applyMemberBans (in ProtectedRooms.ts) with calls to the AccessControlUnit.
Adding commands to add/remove allowed entities is not something i want to do at the moment.
2022-10-18 16:28:11 +01:00
David Teller 7b0edadd17 Update tests and instructions to intercept reports also with v3 endpoint (#388)
In both our instructions and our tests, we use the r0 endpoint to intercept abuse reports. This endpoint is deprecated and not implemented by all clients. This PR updates the instructions and tests to the new endpoint.
2022-10-18 15:48:39 +02:00
Gnuxie 77ad40e27a Refactor protected rooms. (#371)
* Attempt to factor out protected rooms from Mjolnir.

This is useful to the appservice because it means we don't
have to wrap a Mjolnir that is designed to sync.

It's also useful if we later on want to have specific
settings per space.

It's also just a nice seperation between Mjolnir's needs while
syncing via client-server and the behaviour of syncing policy rooms.

 ### Things that have changed

- `ErrorCache` no longer a static class (phew), gets used by `ProtectedRooms`.
- `ManagementRoomOutput` class gets created to handle logging back to the management room.
- Responsibilities for syncing member bans and server ACL are handled by `ProtectedRooms`.
- Responsibilities for watched lists should be moved to `ProtectedRooms` if they haven't been.
- `EventRedactionQueue` is moved to `ProtectedRooms` since this needs to happen after
  member bans.
- ApplyServerAcls moved to `ProtectedRooms`
- ApplyMemberBans move to `ProtectedRooms`
- `logMessage` and `replaceRoomIdsWithPills` moved to `ManagementRoomOutput`.
- `resyncJoinedRooms` has been made a little more clear, though I am concerned about how often it does run because it does seem expensive.


* ProtectedRooms is not supposed to track joined rooms.

The reason is because it is supposed to represent a specific
set of rooms to protect, not do horrible logic
for working out what rooms mjolnir is supposed to protect.
2022-09-29 14:49:09 +01:00
Jess Porter f108935d07 support compound consequences, switch WordList to consequences (#351) 2022-09-26 16:57:21 +01:00
Gnuxie 4d5447cb50 Rework the banning and unbanning of entities in PolicyLists. (#345)
* Rework the banning and unbanning of entities in PolicyLists.

1. We keep track of the event that created a list rule so that we
can remove the rule by having a way to determine the original state key for the rule.
This is because the state key of rules can be anything and should not be
relied on by Mjolnir to unban things (which it was doing).

2. The old scheme for producing a state key was causing for some entities to escape bans
https://github.com/matrix-org/mjolnir/issues/322.

We could have used a hash or something similar, but we know that
the reason for the `rule:${entity}` scheme existed was for ease of debugging
and finding rules in devtools. So instead we have followed a scheme simalar to
bridges where the first character of an mxid is replaced with an underscore.
Everything else just gets put into the state key. Since domains can't have '@'
and room ids, aliases can't either.

3. We have stopped the need for Mjolnir to wait for the next response from sync after banning,
unbanning an entity so that we can apply ACL's sooner.

* Use PolicyList's `banEntity` method to create imported rules.
2022-08-19 13:09:08 +01:00
Gnuxie 8bafa16495 Missing awaits on floating promises in tests. (#359)
If you add `"no-floating-promises": true` it's very easy
to find where this is done accidentally.
Not sure we can keep that on all the time yet though..
2022-08-18 13:09:03 +01:00