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

Empty origin header doesn't work #23

Closed
adimarco opened this issue Jul 21, 2017 · 6 comments
Closed

Empty origin header doesn't work #23

adimarco opened this issue Jul 21, 2017 · 6 comments
Labels

Comments

@adimarco
Copy link

After pulling my hair out a bit trying to get rocket_cors working with my existing rocket code, I tried checking out the git repo and running cargo run --example fairing and got the same results.

No headers are ever added. curl output for the fairing example nets me:

$ curl -v localhost:8000/
* Hostname was NOT found in DNS cache
*   Trying 127.0.0.1...
* Connected to localhost (127.0.0.1) port 8000 (#0)
> GET / HTTP/1.1
> User-Agent: curl/7.35.0
> Host: localhost:8000
> Accept: */*
> 
< HTTP/1.1 200 OK
< Content-Type: text/plain; charset=utf-8
* Server Rocket is not blacklisted
< Server: Rocket
< Content-Length: 10
< Date: Fri, 21 Jul 2017 15:00:32 GMT
< 
* Connection #0 to host localhost left intact
Hello CORS

I'm running the latest nightly rust available from rustup:

$ rustup update nightly
info: syncing channel updates for 'nightly-x86_64-unknown-linux-gnu'

  nightly-x86_64-unknown-linux-gnu unchanged - rustc 1.20.0-nightly (ae98ebfcb 2017-07-20)

$ rustc --version
rustc 1.20.0-nightly (ae98ebfcb 2017-07-20)

Console output from the running example is:

 $ cargo run --example fairing
    Finished dev [unoptimized + debuginfo] target(s) in 0.0 secs
     Running `target/debug/examples/fairing`
🔧  Configured for development.
    => address: localhost
    => port: 8000
    => log: normal
    => workers: 16
    => secret key: generated
    => limits: forms = 32KiB
    => tls: disabled
🛰  Mounting '/':
    => GET /
🛰  Mounting '/cors':
    => GET /cors/<status>
📡  Fairings:
    => 0 launch: 
    => 1 request: CORS
    => 1 response: CORS
🚀  Rocket has launched from http://localhost:8000

Am I missing something? This is exactly the same thing that happens when I try to integrate it with my existing rocket app (nothing at all).

@adimarco
Copy link
Author

This seems to be due to the lack of an "Origin" header in my curl request. When I set an Origin header it works.

According to Mozilla, the "Origin" header can be blank - https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS#Origin though it appears that's not currently supported as rocket_cors is written. (I tried the curl with an -H "Origin: " to send it the empty string)

@adimarco adimarco changed the title Fairing example seems broken, doesn't seem to add any headers regardless Empty origin header doesn't work Jul 21, 2017
@lawliet89
Copy link
Owner

lawliet89 commented Jul 23, 2017 via email

@lawliet89
Copy link
Owner

lawliet89 commented Jul 24, 2017

You can test with the following example:

#![feature(plugin)]
#![plugin(rocket_codegen)]
extern crate rocket;
extern crate rocket_cors;

use rocket::http::Method;
use rocket_cors::{AllowedOrigins, AllowedHeaders};

#[get("/")]
fn get<'a>() -> &'a str {
    "Hello CORS"
}

#[put("/")]
fn put<'a>() -> &'a str {
    "Hello CORS"
}

#[post("/")]
fn post<'a>() -> &'a str {
    "Hello CORS"
}

#[delete("/")]
fn delete<'a>() -> &'a str {
    "Hello CORS"
}

fn main() {
    let (allowed_origins, failed_origins) = AllowedOrigins::some(&["http://www.test-cors.org"]);
    assert!(failed_origins.is_empty());

    // You can also deserialize this
    let options = rocket_cors::Cors {
        allowed_origins: allowed_origins,
        allowed_methods: vec![Method::Get, Method::Put, Method::Post, Method::Delete]
            .into_iter()
            .map(From::from)
            .collect(),
        allowed_headers: AllowedHeaders::some(&["Authorization", "Accept"]),
        allow_credentials: true,
        ..Default::default()
    };

    rocket::ignite()
        .mount("/", routes![get, put, post, delete])
        .attach(options)
        .launch();
}

From the test-cors.org website. For example, to test PUT:

     Running `target/debug/examples/test-cors`
🔧  Configured for development.
    => address: localhost
    => port: 8000
    => log: normal
    => workers: 8
    => secret key: generated
    => limits: forms = 32KiB
    => tls: disabled
🛰  Mounting '/':
    => GET /
    => PUT /
    => POST /
    => DELETE /
🛰  Mounting '/cors':
    => GET /cors/<status>
📡  Fairings:
    => 0 launch: 
    => 1 request: CORS
    => 1 response: CORS
🚀  Rocket has launched from http://localhost:8000
OPTIONS /:
    => Error: No matching routes for OPTIONS /.
    => Warning: Responding with 404 Not Found catcher.
    => CORS Fairing: Turned missing route OPTIONS / into an OPTIONS pre-flight request
    => Response succeeded.
PUT /:
    => Matched: PUT /
    => Outcome: Success
    => Response succeeded.

@lawliet89
Copy link
Owner

@adimarco: I am assuming that you have no further issue with this. Please reopen or raise a new issue if something new arises.

@Arignir
Copy link

Arignir commented Jan 23, 2019

I got brained by this issue the first time I tried rocket_cors, and adimarco and I probably aren't the only one. Took me quite a lot of googling to find out the missing Origin was the key.

Handling no/default Origin would be great to make rocket_cors easier to understand, and help them understand they aren't doing anything wrong when they try the crate for the first time.

@lawliet89
Copy link
Owner

Do you mean you would like the crate to inject the CORS response headers even when the browser does not include any CORS related request headers?

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

No branches or pull requests

3 participants