Skip to content

Reject pixels without an access token before any request is made - #17

Merged
loevgaard merged 1 commit into
2.xfrom
reject-pixels-without-access-token
Sep 21, 2026
Merged

loevgaard merged 1 commit into
2.xfrom
reject-pixels-without-access-token

Conversation

@loevgaard

Copy link
Copy Markdown
Member

Summary

Posting to Meta without an access token can never succeed, yet the client did it anyway.

What happened before

  • http_build_query() drops null values, so for a token-less pixel the POST carried only data=…, with no access_token field at all.
  • Meta answers with a 400 whose message does not mention the token. Tried with a dummy pixel id and no credentials:
    Unsupported post request. Object with ID '123' does not exist, cannot be loaded due to missing
    permissions, or does not support this operation. (GraphMethodException, code 100, subcode 33)
    
    Someone debugging a queue handler would go looking for a wrong pixel id.
  • With several pixels, the ones listed before the token-less pixel had already received the event when the exception was thrown, and a queue retry would send to them again.

2.0 makes this more likely: PreparedEvent::withoutAccessTokens() turns token-less pixels into a normal state, and withAccessTokens() deliberately leaves pixels alone that are missing from the list. Forgetting the call, or a pixel id, led straight to the above.

What happens now

sendPreparedEvent(), and with it sendEvent(), checks all pixels first and throws ClientException::missingAccessToken() naming the pixels, with a hint at withAccessTokens(). No request is made, so the event is never delivered to only some of its pixels. An empty string counts as missing, because Pixel::$accessToken is public and only the constructor normalizes '' to null.

Token-less pixels are skipped nowhere and nothing is logged-and-dropped: a token-less pixel is legitimate for browser-only use with FbqGenerator, so the client cannot tell "not meant for the server" from "forgot to restore the token", and silently losing conversions is the worst failure mode for a tracking library. An exception lets a queue retry or park the message.

Compatibility

No new BC break (Roave: none). The call already ended in a ClientException before, only later and with Meta's text. The one difference is documented in UPGRADE-2.0.md: with several pixels, those listed before the token-less one no longer receive the event.

Two existing tests sent events with a token-less pixel and asserted a body without access_token; they now use a token.

Docs

README (queue section and error handling), CLAUDE.md (client paragraph), UPGRADE-2.0.md (new section), and the docblocks on ClientInterface and Pixel::$accessToken.

Test plan

  • ClientTest::it_throws_when_a_pixel_has_no_access_token — message names the pixel, no request made
  • ClientTest::it_sends_nothing_when_any_of_the_pixels_has_no_access_token — four pixels, two restored via withAccessTokens(): the message lists exactly the other two, and not even the first, valid pixel receives the event
  • ClientTest::it_treats_an_empty_access_token_as_missing
  • ClientExceptionTest::it_is_created_from_pixels_without_an_access_token — exact message
  • 113 tests, 100 % line coverage; PHPStan (level max), ECS, dependency analyser green; Infection MSI 95 % / covered 95 %, no escaped mutants in the touched files
  • Roave BC check locally against 2.x: no breaks

For a pixel without an access token the client sent the request anyway:
http_build_query() drops the null value, so the POST carried no
access_token at all, and Meta answered with an error that does not
mention the token ("Unsupported post request. Object with ID ... does
not exist, cannot be loaded due to missing permissions ..."). With
several pixels, the ones listed before the token-less pixel had already
received the event when the exception was thrown.

PreparedEvent::withoutAccessTokens() makes token-less pixels a normal
state, and withAccessTokens() leaves pixels alone that are missing from
the list, so forgetting the call or a pixel id leads straight to this.

sendPreparedEvent(), and with it sendEvent(), now checks all pixels
first and throws a ClientException naming the pixels without an access
token. No request is made, so the event is never delivered to only some
of its pixels. An empty string counts as missing, since the property is
public and only the constructor normalizes it to null.
@loevgaard
loevgaard merged commit 155568b into 2.x Sep 21, 2026
28 checks passed
@loevgaard
loevgaard deleted the reject-pixels-without-access-token branch September 21, 2026 08:38
loevgaard added a commit that referenced this pull request Sep 21, 2026
…ent (#19)

Since #17 a single pixel without an access token made the client throw
before sending anything, so that one pixel kept every other pixel from
receiving the event. A pixel without an access token is a legitimate
state, a pixel that is only used in the browser for instance.

The client now sends to every pixel that has an access token, skips the
ones that do not, and logs an error naming them. It throws an
InvalidArgumentException only when none of the pixels has an access
token, which is what happens when PreparedEvent::withAccessTokens() is
forgotten after withoutAccessTokens().
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant