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

validate_registry_response function #132

Open
IIEIIEJI opened this issue May 22, 2024 · 1 comment
Open

validate_registry_response function #132

IIEIIEJI opened this issue May 22, 2024 · 1 comment
Labels
bug Something isn't working

Comments

@IIEIIEJI
Copy link

IIEIIEJI commented May 22, 2024

Hello!.
Maybe, in the validate_registry_response function, there is a non-compliance with the https://github.com/opencontainers/distribution-spec/blob/main/spec.md#errors-2 standard.
Firstly the returned format may not be json ( ...A 4XX response code from the registry MAY return a body in any format....). Secondly some registries return their own code, for example Harbor v2.9.0 returns this error: r#"{"errors":[{"code": "NOT_FOUND", "message": "artifact image:0.0.1 not found"}]}]}"#.
It may be more correct to use the following variant of the validate_registry_response function:

fn validate_registry_response(status: reqwest::StatusCode, body: &[u8], url: &str) -> Result<()> {
    debug!(STATUS=?status);
    match status {
        reqwest::StatusCode::OK => Ok(()),
        reqwest::StatusCode::UNAUTHORIZED => Err(OciDistributionError::UnauthorizedError {
            url: url.to_string(),
        }),
        s if s.is_success() => Err(OciDistributionError::SpecViolationError(format!(
            "Expected HTTP Status {}, got {} instead",
            reqwest::StatusCode::OK,
            status,
        ))),
        s if s.is_client_error() => {
            let text = std::str::from_utf8(body)?;
            
            // According to the OCI spec, we should see an error in the message body.
            match serde_json::from_str::<OciEnvelope>(text) {
                Ok(envelope) => Err(OciDistributionError::RegistryError {
                    envelope,
                    url: url.to_string(),
                }),
                Err(err) => {
                    debug!(err=?err,"Not valid json format of RegistryError (https://github.com/opencontainers/distribution-spec/blob/master/spec.md#errors-2)");
                    Err(OciDistributionError::ServerError {
                        code: s.as_u16(),
                        url: url.to_string(),
                        message: text.to_string(),
                    })
                }
            }
        }
        s => {
            let text = std::str::from_utf8(body)?;

            Err(OciDistributionError::ServerError {
                code: s.as_u16(),
                url: url.to_string(),
                message: text.to_string(),
            })
        }
    }
}
@thomastaylor312
Copy link
Contributor

Looks like you are correct here! Code also looks pretty good with the only suggestion being to check the response headers to see if content-type is set before parsing JSON. Completely open to a PR if you'd like to submit!

@thomastaylor312 thomastaylor312 added the bug Something isn't working label May 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants