Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
7 changes: 7 additions & 0 deletions packages/extension/src/background/actionHandlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
ReviewTransactionResult,
TransactionResult,
} from "../shared/actionQueue/types"
import { isDappMessageHasherAllowed } from "../shared/messageHasher"
import { MessageType } from "../shared/messages"
import { addNetwork, getNetworks } from "../shared/network"
import { preAuthorize } from "../shared/preAuthorizations"
Expand Down Expand Up @@ -130,6 +131,12 @@ export const handleActionApproval = async (

case "ALPH_SIGN_MESSAGE": {
try {
if (!isDappMessageHasherAllowed(action.payload.messageHasher)) {
throw Error(
"Unsupported message hasher. Only the 'alephium' message hasher is accepted.",
)
}

const account = await wallet.getAccount({
address: action.payload.signerAddress,
networkId: action.payload.networkId,
Expand Down
19 changes: 19 additions & 0 deletions packages/extension/src/background/actionMessaging.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { isDappMessageHasherAllowed } from "../shared/messageHasher"
import { ActionMessage } from "../shared/messages/ActionMessage"
import { handleActionApproval, handleActionRejection } from "./actionHandlers"
import { sendMessageToUi } from "./activeTabs"
Expand Down Expand Up @@ -57,6 +58,24 @@ export const handleActionMessage: HandleMessage<ActionMessage> = async ({
}

case "ALPH_SIGN_MESSAGE": {
if (!isDappMessageHasherAllowed(msg.data.messageHasher)) {
const rejectedActionHash = "rejected-unsupported-message-hasher"
await respond({
type: "ALPH_SIGN_MESSAGE_RES",
data: {
actionHash: rejectedActionHash,
},
})
return await respond({
type: "ALPH_SIGN_MESSAGE_FAILURE",
data: {
actionHash: rejectedActionHash,
error:
"Unsupported message hasher. Only the 'alephium' message hasher is accepted.",
},
})
}

const { meta } = await actionQueue.push({
type: "ALPH_SIGN_MESSAGE",
payload: msg.data,
Expand Down
5 changes: 4 additions & 1 deletion packages/extension/src/inpage/messageActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ export function waitForMessage<
predicate: (x: T) => boolean = () => true,
): Promise<T extends { data: infer S } ? S : undefined> {
return new Promise((resolve, reject) => {
const pid = setTimeout(() => reject(new Error("Timeout")), timeout)
const pid = setTimeout(() => {
window.removeEventListener("message", handler)
reject(new Error("Timeout"))
}, timeout)
const handler = (event: MessageEvent<WindowMessageType>) => {
if (event.data.type === type && predicate(event.data as any)) {
clearTimeout(pid)
Expand Down
14 changes: 14 additions & 0 deletions packages/extension/src/shared/__test__/messageHasher.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { isDappMessageHasherAllowed, SAFE_DAPP_MESSAGE_HASHER } from "../messageHasher"

describe("isDappMessageHasherAllowed (dApp signMessage policy)", () => {
it("accepts only the domain-separated 'alephium' hasher", () => {
expect(SAFE_DAPP_MESSAGE_HASHER).toBe("alephium")
expect(isDappMessageHasherAllowed("alephium")).toBe(true)
})

it("rejects every other hasher a dApp could request", () => {
expect(isDappMessageHasherAllowed("identity")).toBe(false)
expect(isDappMessageHasherAllowed("sha256")).toBe(false)
expect(isDappMessageHasherAllowed("blake2b")).toBe(false)
})
})
6 changes: 6 additions & 0 deletions packages/extension/src/shared/messageHasher.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { MessageHasher } from "@alephium/web3"

export const SAFE_DAPP_MESSAGE_HASHER = "alephium"

@h0ngcha0 h0ngcha0 Jun 23, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would disable Nostr injection, which uses the sha256 hasher.

If it is ok to remove Nostr support, then it shouldn't be a problem.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sha256 should be okay to be kept since it's not used for tx building

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pushed


export const isDappMessageHasherAllowed = (messageHasher: MessageHasher): boolean =>
messageHasher === SAFE_DAPP_MESSAGE_HASHER
Loading