Skip to content
This repository has been archived by the owner on Sep 13, 2023. It is now read-only.

Calls to /api are no longer forwarded to the replica #46

Closed
paulyoung opened this issue Aug 17, 2022 · 12 comments
Closed

Calls to /api are no longer forwarded to the replica #46

paulyoung opened this issue Aug 17, 2022 · 12 comments

Comments

@paulyoung
Copy link
Contributor

I upgraded icx-proxy from e3866f0 to 7624053 and now the initial call to /api/v2/status returns the HTML page of my asset canister, and a CBOR decoder tries to decode that and throws an error.

I downgraded back to e3866f0 API calls were forwarded again.

Is there some new flag I need to pass to get the old behavior? I couldn't see anything obvious in the help text.

@nomeata
Copy link
Contributor

nomeata commented Aug 18, 2022

This is intentional - at least for secure domains such uncertified “holes” make your users attackable by a malicious replica.

You should not use the canister domain for IC requests, but some generic domain (is it simply ic0.app? I don't remember)

@paulyoung
Copy link
Contributor Author

This is all abstracted away from me by agent-js; I’m using createActor as shown in the README: https://github.com/dfinity/agent-js/blob/main/packages/auth-client/README.md#in-the-browser

By the way, I’m only using icx-proxy locally; I have a wrapper script that reads the relevant canister’s ID and then starts icx-proxy with the --dns-alias flag, since it’s inconvenient for a certain client I’m targeting to have to update the canister ID if/when it changes.

@nomeata
Copy link
Contributor

nomeata commented Aug 18, 2022

Ah, so maybe (just guessing) the agent has logic for picking the right gateway domain in production, but not locally. Hmmm.

@paulyoung
Copy link
Contributor Author

paulyoung commented Aug 18, 2022

I think calls are made relative to the current domain, so if I do --dns-alias myproject.localhost:<canister id of myproject_assets> then browse to http://myproject.localhost in a Chromium browser (I use Brave) it serves up the front end and calls http://myproject.localhost/api/v2/status

@nomeata
Copy link
Contributor

nomeata commented Aug 18, 2022

Right, that is the old behaviour that used to work, until the proxing of /api was removed. For production, the agent takes care of it since dfinity/agent-js#516 I think… not sure how to help with your use case, though.

@paulyoung
Copy link
Contributor Author

I’ll try upgrading agent-js

@nomeata
Copy link
Contributor

nomeata commented Aug 18, 2022

That change won’t help you in the local use-case, I don’t know if any others changes there have. But worth trying, of course.

@paulyoung
Copy link
Contributor Author

paulyoung commented Aug 23, 2022

Based on this post by @Daniel-Bloom-dfinity I think I need to set the host option of the HTTP agent to something like isLocal ? "127.0.0.1:8000" : "ic0.app" (the local replica runs on port 8000)

@ninegua also shared something in this post about using ic0.app

Perhaps seeing <canister id>.ic0.app all the time has confused people. I think that's what confused me.

@paulyoung
Copy link
Contributor Author

Setting the host as described above appears to be working as intended. Thanks for the help!

@paulyoung
Copy link
Contributor Author

In addition to the above I had set the host to 127.0.0.1:8080 when running my local dev server for iterating on the front end (I'm using esbuild on port 8080 but other people may be using something like webpack dev server on a different port)

I think not setting the host or setting it to undefined has the same effect but I have an abstract over this and didn't want to may it optional.

I still needed to forward /api using some proxy middleware due to CORS.

@paulyoung
Copy link
Contributor Author

paulyoung commented Aug 23, 2022

I think the simple solution to the above is to use host: window.location.host everywhere.

@paulyoung
Copy link
Contributor Author

Ah, no, that's the default and that includes the subdomain.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants