Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 43 additions & 1 deletion packages/mxfx/src/branded/user-id.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { describe, expect, it } from '@effect/vitest'
import { Effect, Exit } from 'effect'
import { Effect, Exit, Schema } from 'effect'

import { UserId } from './user-id.ts'

Expand All @@ -24,4 +24,46 @@ describe('branded', () => {
expect(Exit.isFailure(yield* Effect.exit(UserId.make(veryLongUserId)))).toBeTruthy()
}),
)

it.effect('should decode historical UserIds received from a server', () =>
Effect.gen(function* () {
const decode = Schema.decodeUnknownEffect(UserId.schema)

expect(yield* decode('@:matrix.org')).toBe('@:matrix.org')
expect(yield* decode('@CapitalizedUserId:matrix.org')).toBe('@CapitalizedUserId:matrix.org')
expect(yield* decode('@legacy~user:matrix.org')).toBe('@legacy~user:matrix.org')
expect(yield* decode('@\u00e9:matrix.org')).toBe('@\u00e9:matrix.org')
expect(yield* decode('@\ud83d\ude00:matrix.org')).toBe('@\ud83d\ude00:matrix.org')
}),
)

it.effect('should reject invalid Unicode in historical UserIds', () =>
Effect.gen(function* () {
const decode = Schema.decodeUnknownEffect(UserId.schema)

expect(Exit.isFailure(yield* Effect.exit(decode('@\ud800:matrix.org')))).toBeTruthy()
expect(Exit.isFailure(yield* Effect.exit(decode('@\udfff:matrix.org')))).toBeTruthy()
expect(Exit.isFailure(yield* Effect.exit(decode('@legacy\u0000user:matrix.org')))).toBeTruthy()
}),
)

it.effect('should enforce the 255-byte UTF-8 limit for historical UserIds', () =>
Effect.gen(function* () {
const decode = Schema.decodeUnknownEffect(UserId.schema)
const exactly255Bytes = `@${'\u00e9'.repeat(121)}a:matrix.org`
const moreThan255Bytes = `@${'\u00e9'.repeat(122)}:matrix.org`

expect(new TextEncoder().encode(exactly255Bytes).byteLength).toBe(255)
expect(yield* decode(exactly255Bytes)).toBe(exactly255Bytes)
expect(new TextEncoder().encode(moreThan255Bytes).byteLength).toBe(256)
expect(Exit.isFailure(yield* Effect.exit(decode(moreThan255Bytes)))).toBeTruthy()
}),
)

it.effect('should not create new UserIds using the historical grammar', () =>
Effect.gen(function* () {
expect(Exit.isFailure(yield* Effect.exit(UserId.make('@CapitalizedUserId:matrix.org')))).toBeTruthy()
expect(Exit.isFailure(yield* Effect.exit(UserId.make('@legacy~user:matrix.org')))).toBeTruthy()
}),
)
})
29 changes: 26 additions & 3 deletions packages/mxfx/src/branded/user-id.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,34 @@ import { opaqueId } from './opaque-id.ts'
import { ServerName } from './server-name.ts'

const localpart = opaqueId.check(Schema.isPattern(/^[a-z0-9._=\-/+]+$/))
const historicalLocalpart = Schema.String.check(
Schema.isPattern(/^[^:\uD800-\uDFFF]*$/u),
Schema.makeFilter((input: string) => !input.includes('\u0000'), { expected: 'a localpart without a NUL character' }),
)
const utf8Encoder = new TextEncoder()
const isValidUserIdByteLength = Schema.makeFilter((input: string) => utf8Encoder.encode(input).byteLength <= 255, {
expected: 'a user ID at most 255 bytes long when encoded as UTF-8',
})

/**
* User IDs received from Matrix servers may use the historical, opaque localpart
* grammar. Homeservers must continue accepting these IDs even though they may not
* create new IDs using that grammar.
*/
const schema = Schema.TemplateLiteral(['@', historicalLocalpart, ':', ServerName.schema]).pipe(
Schema.check(isValidUserIdByteLength),
Schema.brand('mxfx/UserId'),
)

const schema = Schema.TemplateLiteral(['@', localpart, ':', ServerName.schema]).pipe(
Schema.check(Schema.isMaxLength(255)),
/** The modern grammar used when constructing a new user ID in mxfx. */
const creationSchema = Schema.TemplateLiteral(['@', localpart, ':', ServerName.schema]).pipe(
Schema.check(isValidUserIdByteLength),
Schema.brand('mxfx/UserId'),
)

export type UserId = typeof schema.Type
export const UserId = { schema, make: Schema.decodeUnknownEffect(schema) }
export const UserId = {
schema,
creationSchema,
make: Schema.decodeUnknownEffect(creationSchema),
}