tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

test_protocol_handler_registration.js (2428B)


      1 /* Any copyright is dedicated to the Public Domain.
      2 * http://creativecommons.org/publicdomain/zero/1.0/ */
      3 
      4 "use strict";
      5 
      6 /**
      7 * Test that the moz-cached-ohttp protocol handler is properly registered.
      8 */
      9 add_task(async function test_protocol_handler_registration() {
     10  // Test that the protocol handler is registered
     11  let protocolHandler;
     12  try {
     13    protocolHandler = Services.io
     14      .getProtocolHandler("moz-cached-ohttp")
     15      .QueryInterface(Ci.nsIProtocolHandler);
     16  } catch (e) {
     17    Assert.ok(false, "moz-cached-ohttp protocol handler should be registered");
     18    return;
     19  }
     20 
     21  Assert.ok(protocolHandler, "Protocol handler should exist");
     22  Assert.equal(protocolHandler.scheme, "moz-cached-ohttp", "Correct scheme");
     23 });
     24 
     25 /**
     26 * Test that the protocol handler rejects invalid ports.
     27 */
     28 add_task(async function test_port_handling() {
     29  const protocolHandler = Services.io
     30    .getProtocolHandler("moz-cached-ohttp")
     31    .QueryInterface(Ci.nsIProtocolHandler);
     32 
     33  Assert.equal(
     34    protocolHandler.allowPort(80, "moz-cached-ohttp"),
     35    false,
     36    "Should not allow any ports"
     37  );
     38 
     39  Assert.equal(
     40    protocolHandler.allowPort(443, "moz-cached-ohttp"),
     41    false,
     42    "Should not allow any ports"
     43  );
     44 });
     45 
     46 /**
     47 * Test that creating a channel with invalid context throws an error.
     48 */
     49 add_task(async function test_invalid_context_rejection() {
     50  const protocolHandler = Services.io
     51    .getProtocolHandler("moz-cached-ohttp")
     52    .QueryInterface(Ci.nsIProtocolHandler);
     53 
     54  const testURI = Services.io.newURI(
     55    createTestOHTTPResourceURI("https://example.com/image.jpg")
     56  );
     57 
     58  // Create a regular web content principal (not privileged about)
     59  const webURI = Services.io.newURI("https://example.com");
     60  const webPrincipal = Services.scriptSecurityManager.createContentPrincipal(
     61    webURI,
     62    {}
     63  );
     64 
     65  // Create loadInfo using a different URI scheme to avoid calling our protocol handler
     66  const httpURI = Services.io.newURI("https://example.com/test");
     67  const loadInfo = NetUtil.newChannel({
     68    uri: httpURI,
     69    loadingPrincipal: webPrincipal,
     70    securityFlags: Ci.nsILoadInfo.SEC_ALLOW_CROSS_ORIGIN_INHERITS_SEC_CONTEXT,
     71    contentPolicyType: Ci.nsIContentPolicy.TYPE_IMAGE,
     72  }).loadInfo;
     73 
     74  Assert.throws(
     75    () => protocolHandler.newChannel(testURI, loadInfo),
     76    /moz-cached-ohttp protocol only accessible from privileged about content/,
     77    "Should reject non-privileged contexts"
     78  );
     79 });