Skip to content

Workaround Worker same origin restriction#7587

Merged
dreamofabear merged 4 commits intoampproject:masterfrom
dreamofabear:worker-as-blob
Feb 21, 2017
Merged

Workaround Worker same origin restriction#7587
dreamofabear merged 4 commits intoampproject:masterfrom
dreamofabear:worker-as-blob

Conversation

@dreamofabear
Copy link
Copy Markdown

Fixes #7566.

/to @cramforce

app.get(['/dist/ww.js', '/dist/ww.max.js'], function(req, res) {
fs.readFileAsync(process.cwd() + req.path).then(file => {
res.setHeader('Content-Type', 'application/javascript');
res.setHeader('AMP-Access-Control-Allow-Source-Origin', getUrlPrefix(req));
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You want the normal CORS header Access-Control-Allow-Source-Origin and static value of * is OK here.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. Also I think we want ampCors: false in the XHR since this resource isn't source-origin-specific.

*/
app.get(['/dist/ww.js', '/dist/ww.max.js'], function(req, res) {
fs.readFileAsync(process.cwd() + req.path).then(file => {
res.setHeader('Content-Type', 'application/javascript');
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'text/javascript'

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Really?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Its what the AMP cache sends :)

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

this.fetchPromise_ = this.xhr_.fetchText(url).then(text => {
// Fetch web worker binary and create from Blob as workaround since
// Worker constructor won't accept CORS URLs.
const blob = new win.Blob([text], {type: 'text/javascript'});
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You now probably want to factor out a isWorkerSupported function to determine whether to go the worker route.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like Blob and URL.createObjectURL (Blob URLs) both have good support.

// Worker constructor won't accept CORS URLs.
const blob = new win.Blob([text], {type: 'text/javascript'});
const blobUrl = win.URL.createObjectURL(blob);
this.worker_ = new win.Worker(blobUrl);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just checked and I'm pretty sure this does not violate our CSP, but we probably should add an explicit worker-src CSP directive to lock that down.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good call -- actually it does violate our CSP after this PR due to Blob-ification.

worker-src is CSP3 which has poor support. Going to add blob: to fallback default-src instead: cl/147751445

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please file an issue for Cloudflare to update their CSP. CC @dknecht.

And update and try out https://github.com/ampproject/amphtml/blob/master/examples/csp.amp.html before submitting cl/147751445

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • @dknecht confirmed Cloudflare change is on staging
  • Verified functionality on csp.amp.html

@dreamofabear dreamofabear force-pushed the worker-as-blob branch 2 times, most recently from f68947d to 8124c90 Compare February 17, 2017 19:31
@dreamofabear
Copy link
Copy Markdown
Author

@cramforce Friendly ping.

@dreamofabear dreamofabear merged commit d8455db into ampproject:master Feb 21, 2017
@dreamofabear dreamofabear deleted the worker-as-blob branch February 21, 2017 17:36
mrjoro pushed a commit to mrjoro/amphtml that referenced this pull request Apr 28, 2017
* initial commit for worker blob

* fix type error

* malte's comments

* fix unit tests
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.

3 participants