amphtml icon indicating copy to clipboard operation
amphtml copied to clipboard

A4A has apparent race condition in crypto validation

Open tdrl opened this issue 9 years ago • 2 comments

A4A is exhibiting apparently non-determinist behavior in creative crypto signature validation. The symptom is that correctly signed, valid creatives are sometimes being rendered in friendly iframe (as intended) and sometimes in cross-domain iframe (broken behavior). We suspect a race condition in the crypto verification promise... Graph.

How do we reproduce the issue?

The following is how we reproduced. It may also happen in production -- not yet verified.

  1. Start gulp to serve local dev code.
  2. Load a local AMP page that contains the following ad code:
<amp-ad width=300 height=250
type="adsense"
data-ad-client="ca-pub-2005682797531342"
data-ad-slot="7046626912">

Use &exp=a4a:-1&force_a4a_ctypes=1 in the local page URL params. E.g., http://localhost:8000/examples/a4a.amp.max.html?exp=a4a:-1&force_a4a_ctypes=1. 3. Examine ad request response (Chrome debug -> Network -> search for ads?). If it has an X-ampadsignature header then it's a valid creative. If not, retry until you get one. 4. Examine the DOM. If the ad creative is rendered via friendly iframe (srcdoc=), then reload until it ends up in a cross-domain iframe (src=). In our experience, this is happening ~30% of the time.

It appears that the verification chain is being executed multiple times. That's expected when it fails the first time, because it can re-fetch the keys in that case. But it shouldn't ever fail in the case of a valid creative.

What browsers are affected?

Observed so far on Chrome 55.

Which AMP version is affected?

1482332119512, but that's a local dev version. We suspect this is active in prod, but are still verifying.

tdrl avatar Dec 21 '16 20:12 tdrl

Assigning initially to @taymonbeal , but it may fall more into @glevitzky 's area. Taymon may reassign.

tdrl avatar Dec 21 '16 20:12 tdrl

Issue due to #6504 which modified verifyCreativeSignature_ to return null in the event a key provider failed to validate. The semantics of src/utils/promise#some is that it will return the result of the first promise that resolves. Change to return null caused invalid providers to return such that other providers did not have a chance to resolve. The issue only appears in our development environment as it is currently the only situation where we have more than one provider (google + google-dev). Fix is to revert to rejecting when provider does not verify (see PR #6837). Closing issue as cause has been determined and is being addressed.

On a side note, negative of some is that other promises continue to execute despite one already having resolved. This results in unnecessary CPU overhead (which perhaps will become moot once the key format is modified to explicitly indicating which public key to use such that we're only every verifying one). To address, I have added a local variable (verificationFound) which is set true when a promise successfully validates. Other promises check the state of the field and if true, will immediately reject.

keithwrightbos avatar Dec 29 '16 16:12 keithwrightbos