Skip to content

What do we do about Safari Extensions? #54

Closed

Description

@Wowfunhappy

I want to have AI build/update some Safari Extensions!

Unfortunately, all legacy Safari Extensions (.safariextz format) need to be signed by an official Safari Developer certificate issued by Apple. Apple no longer issues these certificates.

We can create and install a self-signed Safari Developer certificate like this:

#!/bin/bash
set -e
export RANDFILE=/dev/null

ID=$(LC_ALL=C tr -dc A-Z0-9 </dev/urandom | head -c 10)
read -rp "Email: " EMAIL
NAME="Safari Developer: ($ID) $EMAIL"

cat >safari-cert.conf <<EOF
[req]
prompt=no
distinguished_name=name
x509_extensions=extensions
[name]
CN=$NAME
[extensions]
basicConstraints=critical,CA:false
keyUsage=critical,digitalSignature
extendedKeyUsage=1.2.840.113635.100.4.8
1.2.840.113635.100.6.1.5=critical,DER:05:00
EOF

openssl req -new -x509 -nodes -newkey rsa:2048 -sha256 -days 3650 -config safari-cert.conf -keyout safari.key -out safari.crt
openssl pkcs12 -export -inkey safari.key -in safari.crt -name "$NAME" -passout pass:safari -out safari.p12

KEYCHAIN="$HOME/Library/Keychains/login.keychain"
security import safari.p12 -k "$KEYCHAIN" -P safari -T /Applications/Safari.app
security add-trusted-cert -r trustRoot -k "$KEYCHAIN" safari.crt
rm safari-cert.conf safari.crt safari.key safari.p12
echo "Installed $NAME"

This will initially appear to satisfy the Extension Builder (Develop → Show Extension Builder within Safari), but you'll get an error when you try to actually install an extension, or the .safariextz the builder generates.

To get passed that, we need to patch Safari. Run this while Safari is open. The app will be patched in memory and the change will only persist until Safari is restarted:

#!/bin/bash

set -e

PID=${1:-$(pgrep -x Safari)}
SYMBOL="_ZNK6Safari11Certificate31hasInvalidAppleCertificateChainEdPKS0_"

[ -n "$PID" ] || { echo "Safari is not running." >&2; exit 1; }

ADDRESS=$(
    lldb -p "$PID" \
        -o "disassemble -n $SYMBOL -c 1" \
        -o detach -o quit 2>&1 |
    sed -n "s/^[[:space:]]*\(0x[0-9a-f]*\):.*/\1/p" |
    head -n 1
)

[ -n "$ADDRESS" ] || { echo "Could not find Safari verifier." >&2; exit 1; }

lldb -p "$PID" \
    -o "memory write $ADDRESS 0x31 0xc0 0xc3" \
    -o "memory read --format x --size 1 --count 3 $ADDRESS" \
    -o detach -o quit

echo "Safari PID $PID patched in memory at $ADDRESS."

(FYI, both scripts were written by the new GPT 5.6 Sol, with some manual cleanup. I think it did well? My sense is that it's great at coding, but it's not knowledgable like Claude, it needs to be told things explicitly. It doesn't seem to mind reverse engineering work though.)

Once an extension has been installed, it will continue working without the patch. But I don't like this as a long term solution.

What is the least bad thing to do here? Certainly, it should be easy to apply the memory patch to the on-disk Safari framework. I'm annoyed because I have gone out of my way to avoid modifying Safari at all—I just think it's so cool that we can update the framework and use the vanilla app.

But, clearly something has to be done here.

Activity

  1. Wowfunhappy commented on Jul 11, 2026

    @Wowfunhappy
    OwnerAuthor

    @krackers If you have thoughts on how to architect this, I'd enjoy hearing them.

    Although, I do think the best, practical fix is probably to suck it up and patch the framework on disk. (Or to create and use some heretofore unmade auto-patcher that doesn't break code signatures, I guess.)

    Although although, it would be cool if we could exploit the fact that once an extension is installed, it stays installed. From that perspective, the thing with lldb actually works fine, I guess? But long term I'd like users to not need the developer tools installed.

    Although although although, if we're talking long term, I'm probably going to try again at recompiling Security.framework (with AI assistance this time, unlike six years ago), at which point I could probably just patch the extension check there.

  2. Wowfunhappy commented on Jul 11, 2026

    @Wowfunhappy
    OwnerAuthor

    Oh, GPT also thinks the patch could just be done within WebKit... although it's apparently not allowed to tell me how to do it.

    • WebKit cannot change extension verification through an API because the verifier lives in Safari.framework, but your replacement WebKit can act as the in-process loader for the same memory patch.

    Add a small x86_64 source file to WebKit with a constructor that:

    1. Checks getprogname() is exactly Safari.
    2. Resolves the verifier using dlsym(RTLD_DEFAULT, mangledSymbol).
    3. Temporarily makes the code page writable.
    4. Writes 31 c0 c3.
    5. Restores read/execute protection.

    Conceptually:

    __attribute__((constructor))
    static void patchSafariExtensionVerifier()
    {
        if (strcmp(getprogname(), "Safari"))
            return;
    
        void* function = dlsym(
            RTLD_DEFAULT,
    

    ⓘ This chat was flagged for possible cybersecurity risk

    I'm confused because I would have thought they'd be in different address spaces? But I guess because Safari loads the framework, the framework has access to Safari's address space, or something like that?

  3. Wowfunhappy commented on Jul 11, 2026

    @Wowfunhappy
    OwnerAuthor

    Apparently arguing with the classifier works? It gave me this sample code:

    #include <dlfcn.h>
    #include <mach/mach.h>
    #include <mach/mach_vm.h>
    #include <stdint.h>
    #include <stdlib.h>
    #include <string.h>
    #include <unistd.h>
    
    __attribute__((constructor))
    static void patchSafariExtensionChainCheck()
    {
        if (strcmp(getprogname(), "Safari"))
            return;
    
        const char* symbol =
            "_ZNK6Safari11Certificate31hasInvalidAppleCertificateChainEdPKS0_";
        uint8_t* function = static_cast<uint8_t*>(dlsym(RTLD_DEFAULT, symbol));
        if (!function)
            return;
    
        const uint8_t original[] = { 0x55, 0x48, 0x89 };
        const uint8_t replacement[] = { 0x31, 0xc0, 0xc3 };
        if (!memcmp(function, replacement, sizeof(replacement)))
            return;
        if (memcmp(function, original, sizeof(original)))
            return;
    
        mach_vm_size_t pageSize = static_cast<mach_vm_size_t>(getpagesize());
        mach_vm_address_t page =
            reinterpret_cast<mach_vm_address_t>(function) & ~(pageSize - 1);
    
        kern_return_t result = mach_vm_protect(
            mach_task_self(), page, pageSize, false,
            VM_PROT_READ | VM_PROT_WRITE | VM_PROT_COPY);
        if (result != KERN_SUCCESS)
            return;
    
        memcpy(function, replacement, sizeof(replacement));
        mach_vm_protect(
            mach_task_self(), page, pageSize, false,
            VM_PROT_READ | VM_PROT_EXECUTE);
    }
    
  4. krackers commented on Jul 11, 2026

    @krackers

    Safari Developer certificate issued by Apple.

    I had no idea these existed! Thought a self-signed one would be enough...

    My sense is that it's great at coding, but it's not knowledgable like Claude

    That would fit with rumors that Claude is a much larger pretrain (rumors that it might not even be sparse MoE but idk if that's credible).

    Certainly, it should be easy to apply the memory patch to the on-disk Safari framework

    Since the patch is to the main safari process, why can't you just have a SIMBL plugin? You aren't limited to doing obj-c swizzling, once you're injected into the process you can do anything you want, e.g. write arbitrary memory.

  5. krackers commented on Jul 11, 2026

    @krackers

    Yup that code looks right. Don't add this to webkit, that's insane. Just have it as a simbl plugin.

  6. Wowfunhappy commented on Jul 11, 2026

    @Wowfunhappy
    OwnerAuthor

    Why don't you like having this in WebKit?

    I might be overindexing on this, but I'm concerned about the Mavericks Forever setup path, which has people install a web browser well before SIMBL, because a browser is the most essential thing everyone needs. So the "Allow self-signed extensions" SIMBL plugin would be much further down the page, and not everyone would install it.

    Which, like, is fine—I want users to be in control. But in this case, it is literally impossible for anyone to sign an extension properly, and it has been impossible for 6+ years (I don't remember exactly when the Safari Developer program was shut down).

    With a SIMBL plugin, any time that I or anyone else distributes an extension, it's going to have a "needs modern WebKit and SIMBL and the self-signed extensions SIMBL plugin" asterisks attached. I'd like to be able to distribute extensions and have them work for anyone with modern WebKit installed.

  7. krackers commented on Jul 11, 2026

    @krackers

    In-memory patching of a process from a loaded framework just feels really gross.

    I mean it's not what you would expect. If I saw that the in-memory signature of the code segment differed from the on-disk one, especially in a browser, I'd immediately be supsicious and wonder if i'd been exploited.

    If you keep it as a SIMBL plugin, then it's more obvious what's going on.

  8. krackers commented on Jul 11, 2026

    @krackers

    also can you ask chatgpt to double check that there's really no bypass (e.g. no hidden default or anything) which allows you to bypass the signature check? I've always wondered that Apple must have had a way to bypass it during development, similar to how chrome allows a developer mode

  9. Wowfunhappy commented on Jul 11, 2026

    @Wowfunhappy
    OwnerAuthor

    also can you ask chatgpt to double check that there's really no bypass (e.g. no hidden default or anything) which allows you to bypass the signature check?

    Oh, believe me I did! It says nothing exists and that this byte patch is the cleanest way. (I admittedly didn't try with Claude; I'm really trying to conserve my usage while Fable works through larger and thornier WebKit problems.)

  10. krackers commented on Jul 11, 2026

    @krackers

    If you have time to burn, I double checked and similar to firefox you can temporarily install unsigned extensions until quit by using developer menu > extension builder> plus button at bottom.

    I wonder how this mechanism, if it's purely in-memory I guess there's not much that can be done.

  11. Wowfunhappy commented on Jul 11, 2026

    @Wowfunhappy
    OwnerAuthor

    similar to firefox you can temporarily install unsigned extensions until quit by using developer menu > extension builder> plus button at bottom.

    Huh? I can add them to the list in Extension Builder, but not install them. Merely having them in the list doesn't do anything, Safari can't use them. What am I missing?

  12. krackers commented on Jul 11, 2026

    @krackers

    Nvmind you're right, I had tested on a newer non-mavericks safari and then assumed same was true on 10.9 since I saw the same menu. Serves me right for not actually checking.

  13. pinned this issue on Jul 13, 2026
  14. added 2 commits that reference this issue on Jul 17, 2026
  15. 11 remaining items

  16. krackers commented on Aug 12, 2026

    @krackers

    Claude also immediately agreed with you when pressed, for whatever that's worth.

    This was with opus 5? Based on how many fundamental conceptual mistakes it has made, it seems the anecdotal reports that it's too RL fried or something seems right.

    In any case, you can verify it by connecting lldb and dumping the relevant memory page. Lookup the image base address image list, calculate the symbol address, then di -f and inspect the actual memory (or image dump around that, if you're so inclined).

    That said I don't know if code signing somehow plays a role here, maybe there's some security feature where if the binary is signed then it trusts that pages won't be modified in memory so it does this optimization. Or it deliberately ejects the page with invalid signature and reloads from disk. (after all it is a browser, and this seems like a good feature to protect against such memory overwrite shenanigans)

    using Opus 4.8 because I'm pretty sure the newer models would refuse to work on this.

    Maybe you should switch to kimi k3 or deepseek for this work? It doesn't really require that frontier a model.

  17. Wowfunhappy commented on Aug 20, 2026

    @Wowfunhappy
    OwnerAuthor

    This message hasn't reappeared after a week of moving the patch outside of SIMBL (it's currently injected via Security.framework), so I'm now pretty sure SIMBL is messing this up somehow, even though that doesn't really make sense.

    So my options are:

    • Port MIP to Mavericks as a replacement for SIMBL, and move the patch there.
      • Pro: I'd like to do this anyway, eventually
      • Con: I'd really like to not work on this until after I'm finished with WebKit.
      • Con: I don't actually know if this will fix the problem.
      • Con: I need to also make some kind of SIMBL plugin compatibility layer, or port all current SIMBL plugins of significance.
    • Edit Safari.framework on disk instead of trying to patch it in memory.
      • Pro: Patch is guaranteed to always be present.
      • Con: Will break the code signature
        • But who the heck actually uses these?
      • Con: Have to remember to clean it up if WebKit is uninstalled.
    • Have WebKit automatically install the memory patch.
      • Pro: Everyone who uses the WebKit backport will definitely have the patch. Fewer pieces to think about.
      • Con: A framework patching its process is gross and might confuse people who monitor their system closely.
      • Con: Putting the patch in WebKit might make LLMs refuse to work on WebKit. This is so incredibly stupid, but needs to be considered.

    I think I'm going to just patch the Safari framework on disk.

  18. Wowfunhappy commented on Aug 20, 2026

    @Wowfunhappy
    OwnerAuthor

    FYI, to just patch Safari.framework:

    Allow self-signed extensions:
    perl -0777 -pi -e 's/\x55\x48\x89(\xe5\xe8\x15\xf8\xff\xff)/\x31\xc0\xc3$1/' /System/Library/PrivateFrameworks/Safari.framework/Versions/A/Safari

    Uninstall patch
    perl -0777 -pi -e 's/\x31\xc0\xc3(\xe5\xe8\x15\xf8\xff\xff)/\x55\x48\x89$1/' /System/Library/PrivateFrameworks/Safari.framework/Versions/A/Safari

    I don't know why, but seeing this in the form of a perl one-liner makes me feel better about it.

  19. krackers commented on Aug 21, 2026

    @krackers

    so I'm now pretty sure SIMBL is messing this up somehow, even though that doesn't really make sense.

    Darn how puzzling. Is it possible that it's not just Safari.app itself which uses the framework but some other binary which also loads it? When you're injecting your patch in Security.framework are you gating by the process name? What happens if you gate it like that to only Safari?

    Fwiw I still really suspect it's just the SIMBL raciness manifesting. Isn't the fact that you observed

    if I patch [via lldb] and then restart Safari, I cannot install new extensions after the restart, it's just that previously installed extensions keep working.

    evidence of that? Say SIMBL racily fails to inject into Safari. Then you won't notice it until it randomly decides to recheck signature, which could be days down the line.


    Patching safari.framework on disk seems fine for now. I like the perl 1-liner. I'd also just drop a README.txt though so someone who discovers the signature mismatch years down the line remembers why the thing has been tampered with.

  20. Wowfunhappy commented on Aug 21, 2026

    @Wowfunhappy
    OwnerAuthor

    Say SIMBL racily fails to inject into Safari. Then you won't notice it until it randomly decides to recheck signature, which could be days down the line.

    But then none of my other SIMBL plugins should be working either, right? Notably, this means I would see a full screen button in the upper right corner of the window, which I would notice quickly (speaking from experience).

    Mind, at this point I also think this is caused by some sort of SIMBL raciness, but I can't come up with a theory for why that would happen.

    (I didn't miss your other questions but I'm too tired to look now.)

  21. krackers commented on Aug 21, 2026

    @krackers

    Notably, this means I would see a full screen button in the upper right corner of the window, which I would notice quickly (speaking from experience).

    No? Some plugins can be loaded at any time and they work fine. As an example, swizzling some method of NSWindow can be done arbitrarily late and the side-effects will always show up on the next paint (more or less I think, so long as you can trigger a full redraw). By comparison when you do memory rewriting within a SIMBL plugin, you're relying on the fact that you must rewrite the memory before the function you're rewriting is first run.

    Or if that wasn't clear:

    • If Safari runs signature validation at t1, and your bundle which rewrites the function gets loaded at t2 > t1, then it's too late.

    • But if Safari draws the NSWindow at t1, your bundle which removes full screen at t2 loads at t2 > t1, then the NSWindow is drawn again at t3, you still see the buttom removed. Technically there is a brief instant between [t1, t3] where the button does exist but so long as this is < 16ms it's never practically drawn.

  22. Wowfunhappy commented on Aug 21, 2026

    @Wowfunhappy
    OwnerAuthor

    But it doesn't really make sense for Safari to be checking the certificate so early when it doesn't complain until much later. And I know that at least in the extension-install case, the patch can be installed arbitrarily late via lldb.

    But whatever, as I said you must be right somehow since it's not working.

  23. krackers commented on Aug 21, 2026

    @krackers

    so early when it doesn't complain until much later.

    Who knows maybe it's some grace period or something. I think the easiest way to test this theory would be to code-inject via DYLD_INSERT_LIBRARIES into safari, and do memory rewriting from there? That would guarantee you get early but also still maintain in-memory rewriting.

  24. Wowfunhappy commented on Aug 22, 2026

    @Wowfunhappy
    OwnerAuthor

    I'd also just drop a README.txt

    It will be an option in the installer.

    Image

    Closing this since shape of solution has been decided and I'm happy with it.

  25. unpinned this issue on Aug 25, 2026
  26. Wowfunhappy commented on Aug 25, 2026

    @Wowfunhappy
    OwnerAuthor

    ...probably should have double checked that the perl one-liner actually worked before declaring victory. Oops. Safari won't open unless the Safari.framework binary is codesigned. I forgot about that.

    So I can do:

    sudo perl -0777 -pi -e 's/\x55\x48\x89(\xe5\xe8\x15\xf8\xff\xff)/\x31\xc0\xc3$1/' /System/Library/PrivateFrameworks/Safari.framework/Versions/A/Safari`
    sudo codesign -f -s - /System/Library/PrivateFrameworks/Safari.framework/Versions/A/Safari
    

    This works fine, it's just (A) no longer a clean Perl one-liner (B) requires the developer Command Line Tools and (C) can't just be cleanly reversed with a second perl command.

    To resolve B, the installer will need to ship jtool2. Then I think the syntax is just ./jtool2 --sign --inplace /System/Library/PrivateFrameworks/Safari.framework/Versions/A/Safari

    To resolve C, the installer will need to actually keep a backup of the original somewhere. Presumably it'll just be beside the patched binary.

    None of this is hard, it just slightly complicates something that I'd really wanted to keep simple. Oh well.

    ...I guess the alternative to both would be to pre-sign the patched framework, then have the installer apply a tiny bspatch on both install and uninstall. Paradoxically, this feels at once more elegant but also overcomplicated, and potentially looks more sketchy.

  27. Wowfunhappy commented on Aug 30, 2026

    @Wowfunhappy
    OwnerAuthor

    Okay, this can still be done as perl one-liners, you just need two of them.

    Install:

    # Remove LC_CODE_SIGNATURE.
    sudo perl -0777 -pi -e 's/\A(\xcf\xfa\xed\xfe\x07\x00\x00\x01\x03\x00\x00\x00\x06\x00\x00\x00)\x32\x00\x00\x00\x78\x18\x00\x00/$1\x31\x00\x00\x00\x68\x18\x00\x00/' /System/Library/PrivateFrameworks/Safari.framework/Versions/A/Safari
    
    # Install Patch
    sudo perl -0777 -pi -e 's/\x55\x48\x89(\xe5\xe8\x15\xf8\xff\xff)/\x31\xc0\xc3$1/' /System/Library/PrivateFrameworks/Safari.framework/Versions/A/Safari
    

    Note that the code signature is still there, we just removed the load command. That means to uninstall, we can do:

    # Uninstall patch
    sudo perl -0777 -pi -e 's/\x31\xc0\xc3(\xe5\xe8\x15\xf8\xff\xff)/\x55\x48\x89$1/' /System/Library/PrivateFrameworks/Safari.framework/Versions/A/Safari
    
    # Add back LC_CODE_SIGNATURE
    sudo perl -0777 -pi -e 's/\A(\xcf\xfa\xed\xfe\x07\x00\x00\x01\x03\x00\x00\x00\x06\x00\x00\x00)\x31\x00\x00\x00\x68\x18\x00\x00/$1\x32\x00\x00\x00\x78\x18\x00\x00/' /System/Library/PrivateFrameworks/Safari.framework/Versions/A/Safari
    

    Ta-da, back to the normal, signed binary. No developer tools needed, no separate backups, we're just changing a handful of bytes. I'm happy.

    P.S. Here is a Perl script (written by GPT 5.6 Sol) which will generate Perl-one-liners to remove and restore the code signature of a macho binary.

    Details
    #!/usr/bin/perl
    use strict;
    use warnings;
    
    die "usage: $0 binary ...\n" unless @ARGV;
    
    sub hex_bytes {
        return join "", map { sprintf "\\x%02x", $_ } unpack "C*", $_[0];
    }
    
    sub shell_quote {
        return "'" . join("'\\''", split /'/, $_[0], -1) . "'";
    }
    
    for my $file (@ARGV) {
        open my $fh, "<", $file or die "$file: $!\n";
        binmode $fh;
        local $/;
        my $data = <$fh>;
        close $fh or die "$file: $!\n";
    
        my $magic = unpack "V", substr($data, 0, 4);
        die "$file: unsupported Mach-O format\n"
            unless $magic == 0xfeedface || $magic == 0xfeedfacf;
    
        my $header_size = $magic == 0xfeedfacf ? 32 : 28;
        my ($command_count, $commands_size) = unpack "V2", substr($data, 16, 8);
        my ($position, $commands_end) = ($header_size, $header_size + $commands_size);
        my ($signature_index, $signature_size);
    
        for (my $i = 0; $i < $command_count; $i++) {
            my ($command, $size) = unpack "V2", substr($data, $position, 8);
            die "$file: malformed Mach-O\n"
                if $size < 8 || $position + $size > $commands_end;
            ($signature_index, $signature_size) = ($i, $size) if $command == 0x1d;
            $position += $size;
        }
    
        die "$file: final LC_CODE_SIGNATURE not found\n"
            unless defined($signature_index)
                && $signature_index == $command_count - 1
                && $signature_size == 16;
    
        my $prefix = hex_bytes(substr($data, 0, 16));
        my $signed = hex_bytes(pack "V2", $command_count, $commands_size);
        my $unsigned = hex_bytes(pack "V2", $command_count - 1, $commands_size - 16);
        my $quoted_file = shell_quote($file);
    
        print "# $file: remove\n";
        print "/usr/bin/perl -0777 -pi -e 's/\\A($prefix)$signed/\$1$unsigned/' $quoted_file\n";
        print "# $file: restore\n";
        print "/usr/bin/perl -0777 -pi -e 's/\\A($prefix)$unsigned/\$1$signed/' $quoted_file\n";
    }
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions