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

Prevent preview/dev for unauthenticated workers sites #1138

Closed
EverlastingBugstopper opened this issue Mar 12, 2020 · 4 comments · Fixed by #1447
Closed

Prevent preview/dev for unauthenticated workers sites #1138

EverlastingBugstopper opened this issue Mar 12, 2020 · 4 comments · Fixed by #1447
Labels
bug Something isn't working
Milestone

Comments

@EverlastingBugstopper
Copy link
Contributor

EverlastingBugstopper commented Mar 12, 2020

We currently prevent wrangler preview if there are namespaces - let's extend that to if there are sites

@ashleymichal ashleymichal added this to the sites clean up milestone Apr 28, 2020
@bradyjoslin
Copy link
Contributor

What do you think about this change as a way to handle?

Current:

https://github.com/cloudflare/wrangler/blob/9ce660f1d2a75e0e696a378449524a3a516845c2/src/preview/upload.rs#L177

Proposed:

...

fn unauthenticated_upload(target: &Target) -> Result<Preview, failure::Error> {
    let create_address = "https://cloudflareworkers.com/script";
    log::info!("address: {}", create_address);

    // KV namespaces and Sites are not supported by the preview service unless you authenticate
    // so we omit them and provide the user with a little guidance. We don't error out, though,
    // because there are valid workarounds for this for testing purposes.
    let script_upload_form = if target.kv_namespaces.is_some() {
        message::warn(
            "KV Namespaces are not supported in preview without setting API credentials and account_id",
        );
        let mut target = target.clone();
        target.kv_namespaces = None;
        upload::form::build(&target, None)?
    }
    // --- Check for and handle presence of a Site
    else if target.site.is_some() {
        message::warn(
            "Sites are not supported in preview without setting API credentials and account_id",
        );
        let mut target = target.clone();
        target.site = None;
        upload::form::build(&target, None)?
    }
    // ---
    else {
        upload::form::build(&target, None)?
    };
    let client = http::client();
    let res = client
        .post(create_address)
        .multipart(script_upload_form)
        .send()?
        .error_for_status()?;

    let text = &res.text()?;
    log::info!("Response from preview: {:#?}", text);

    let preview: Preview =
        serde_json::from_str(text).expect("could not create a script on cloudflareworkers.com");

    Ok(preview)
}

...

@EverlastingBugstopper
Copy link
Contributor Author

seems good to me @bradyjoslin 😄

@jspspike
Copy link
Contributor

Hey @bradyjoslin are you planning on making a PR with that change? I can do it if you would like

@bradyjoslin
Copy link
Contributor

Hi @jspspike - please feel free to run with the PR. Thanks for checking!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants