← All Exercises

Validate Browser Extension Message Routing

intermediatejavascript~25min

Background

Extension privilege boundaries are enforced by message routing. A content script that can message the background with any type becomes an escalation path.

Problem

Check each message against the allowed sender-to-receiver routes and report routed messages plus rejected ones with a reason.

Input

JSON object: { routes: { [sender]: string[] }, messages: [{ from, to, type }] }

Output

JSON object: { routed: string[], rejected: [{ id, reason }] }

Sample tests

Sample 1

Input: {"routes":{"content":["background"],"background":["content","storage"]},"messages":[{"id":"m1","from":"content","to":"background","type":"sync"},{"id":"m2","from":"content","to":"storage","type":"read"}]}
Expected: {"routed":["m1"],"rejected":[{"id":"m2","reason":"route-not-allowed"}]}

Sample 2

Input: {"routes":{"popup":["background"]},"messages":[{"id":"m3","from":"popup","to":"background","type":"query"},{"id":"m4","from":"unknown","to":"background","type":"query"}]}
Expected: {"routed":["m3"],"rejected":[{"id":"m4","reason":"unknown-sender"}]}

Your solution

Learning objective

Enforce extension privilege boundaries at the message layer.