Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error: only absolute urls are supported #1213

Closed
ccorcos opened this issue Feb 20, 2017 · 9 comments
Closed

Error: only absolute urls are supported #1213

ccorcos opened this issue Feb 20, 2017 · 9 comments

Comments

@ccorcos
Copy link

ccorcos commented Feb 20, 2017

How can I specify what url to use based on the environment being development or production?

I'm basically using the same server as a proxy

app.prepare()
.then(() => {
  const server = express()
  server.use(morgan(dev ? 'dev' : 'common'))
  server.use(bodyParser.json({limit: '50mb'}))

  server.get('/posts', (req, res) => {
    db.list('Posts', {
      pageSize: 10,
      fields: ['Name', 'Date Published'],
      cursor: req.query.cursor,
    })
    .then(result => res.json(result))
  })

So I'm trying to make a fetch to the root domain, but I cant get it to work.

export default class extends React.PureComponent {
  static async getInitialProps() {
    console.log(process.env)
    const response = await fetch('/posts')
    const data = await response.json()
    return { data }
  }
  render() {
    console.log(this.props)
    return (
      <div>
        hello {this.props.data}
      </div>
    )
  }
}
@eezing
Copy link

eezing commented Feb 20, 2017

Yea, when fetching server side, it appears we need to define the full URL. I did this:

static getInitialProps ({ req }) {
  const baseUrl = req ? `${req.protocol}://${req.get('Host')}` : '';
  const response = await fetch(baseUrl + '/posts');
}

@rauchg
Copy link
Member

rauchg commented Feb 20, 2017

Error: only absolute urls are supported

This is a good thing! How else would fetch know where to go on the server?

@arunoda
Copy link
Contributor

arunoda commented Feb 20, 2017

I think this is something we can't change. You need to define the full URL.
Defining something like API_URL and setting it via some env variable is a pretty good thing to do.

@arunoda arunoda closed this as completed Feb 20, 2017
@ccorcos
Copy link
Author

ccorcos commented Feb 20, 2017

@eezing has a nice solution that worked.

@timneutkens
Copy link
Member

@eezing @ccorcos I wonder if this is vulnerable to an attack. SInce the host header is provided by the client, not the server.
http://www.skeletonscribe.net/2013/05/practical-http-host-header-attacks.html

@ccorcos
Copy link
Author

ccorcos commented Feb 20, 2017

Interesting.

@eezing
Copy link

eezing commented Feb 21, 2017

@timneutkens @ccorcos Thanks for bringing this up. Yes, the Host header is vulnerable to spoofing, but it may not be a cause for concern depending on your host provider.

In my case, my cloud hosting provider's proxy routes requests based on Host request header, which in theory makes it impossible for a client to be on my site and have the Host header be something different.

The behavior can be confirmed using Postman or curl (allows you to define Host header).

Does this make sense or am I missing something? Thoughts? In any case I should have dropped a disclaimer with my original post.

@timneutkens
Copy link
Member

@eezing yeah it would be quite impossible to do that on now also, just wanted to drop it in, since if you go hosting it yourself it would be vulnerable 😉

@ccorcos
Copy link
Author

ccorcos commented Feb 21, 2017

yeah, definitely something to consider!

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

5 participants