riboseinc / uri_format_validator

Validate URL for Rails
MIT License
3 stars 2 forks source link

Parsing Git URIs which are written in scp-like notation #96

Open skalee opened 6 years ago

skalee commented 6 years ago

From Git Book:

The SSH Protocol

A common transport protocol for Git when self-hosting is over SSH. This is because SSH access to servers is already set up in most places – and > if it isn’t, it’s easy to do. SSH is also an authenticated network protocol; and because it’s ubiquitous, it’s generally easy to set up and use.

To clone a Git repository over SSH, you can specify ssh:// URL like this:

$ git clone ssh://user@server/project.git

Or you can use the shorter scp-like syntax for the SSH protocol:

$ git clone user@server:project.git

You can also not specify a user, and Git assumes the user you’re currently logged in as.

That shorter scp-like is not a valid URI, even when prefixed with ssh://, because in URI a colon is expected to be followed with a port number. Both URI implementations in Ruby (stdlib's and Addressable's) are raising exception on parse attempt. However, this syntax is very popular, and perhaps dominant. For instance, GitHub features it in the "Clone or download" pop-up.

ronaldtse commented 6 years ago

@skalee is this a case you think we should support?

Since user@server:project.git is obviously not a URI, but a URI with the scheme stripped away and the port replaced by a path, I think a good way to handle this using a special case.

validate :git_url, :uri_format -> { scheme: :git_short }

What do you think?

skalee commented 6 years ago

@ronaldtse I think that URI normalization is useful in general, not only in our URI format validator. One use case is uniqueness validation which should be performed with standard Rails validators on normalized URIs. This validation relies on performing a database query, hence normalized URI is required to be stored in a separate column.

Therefore, I think that URI normalization should be a separate feature, but I am not sure of details yet. Perhaps this feature should be moved to a separate gem? Perhaps uri_format_validator gem should be renamed to uri_field, and have both features? Neither I am sure how attribute normalization should be declared in user's code. Perhaps in a manner resembling attr_encrypted or attr_masker gem?