Skip to content

validate sitenames, validate config during init, fixes #86 - #339

Merged
tannerjfco merged 3 commits into
ddev:masterfrom
tannerjfco:safe-site-names
Jun 23, 2017
Merged

tannerjfco merged 3 commits into
ddev:masterfrom
tannerjfco:safe-site-names

Conversation

@tannerjfco

@tannerjfco tannerjfco commented Jun 16, 2017 •

Copy link
Copy Markdown
Contributor

The Problem:

#86 OP we currently accept site names allowing any/all characters. This doesn't work out so well for domain names, and attempting to start a site whose sitename contains invalid characters for a domain will result in the site failing to start.

The Fix:

This PR introduces validation of the site name value. Site names will now be validated as hostnames per RFC 1123. If the working directory name does not validate, it will not be suggested to the user. If the user supplies an invalid site name during "ddev config", they will be re-prompted for the site name.

This also introduces a Validate() method for config that runs during App.Init(), which will validate hostname, docroot, and apptype. If any of these values are invalid, an error will be returned to the user instructing them to resolve the invalid configuration.

The Test:

The case in OP should no longer be reproducible:

  • mkdir invalid-drud-site?
  • cd invalid-drud-site?
  • ddev config, there should be no suggested site name
  • Provide a sitename with invalid characters. You should be prompted to re-enter the sitename.
  • ddev start. it should not fail

You can also test config validation on start by manually setting values in config.yaml and running "ddev start:

  • Entering a sitename with invalid characters and running "ddev start" should result in an error saying the sitename is invalid.
  • Entering a non-existent docroot path should result in an error saying the docroot path does not exist.
  • Entering an invalid apptype should result in an error saying the apptype is invalid.

Automation Overview:

Introduced a test for the new config Validate() method.

Related Issue Link(s):

#86

Release/Deployment notes:

@tannerjfco tannerjfco self-assigned this Jun 16, 2017
rfay
rfay previously requested changes Jun 16, 2017

@rfay rfay left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Haven't tested it manually yet, should work out OK, pretty sure the regex is wrong. Regex should get a comment anyway as most people stumble on them.

Comment thread pkg/ddevapp/config.go Outdated
var AllowedAppTypes = []string{"drupal7", "drupal8", "wordpress"}

// Regexp pattern for alphanumeric + dashes
var re = regexp.MustCompile(`[^a-zA-Z0-9_-]+`)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't you want '^[a-zA-Z0-9_-]+$' ?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I misunderstood the intent - I was giving a validation string, not a replacement string.

@rfay rfay left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry I didn't take the time to understand this properly, sure you can get Brad to pick it up on Monday. (Trying to cancel my "Request Changes" :)

@rfay
rfay dismissed their stale review June 17, 2017 15:33

I jumped the gun on this and didn't actually understand what was being done.

Comment thread pkg/ddevapp/config.go Outdated
var AllowedAppTypes = []string{"drupal7", "drupal8", "wordpress"}

// Regexp pattern to match any non-alphanumeric characters (except dashes) so they can be replaced.
var re = regexp.MustCompile(`[^a-zA-Z0-9_-]+`)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any reason to not just do a full hostname regex here, since that's our end goal?

It's a lot more wordy, but a full hostname REGEX based on RFC 1123 would be: ^(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9\-]*[A-Za-z0-9])$ according to https://stackoverflow.com/a/106223

Comment thread pkg/ddevapp/config.go Outdated
// Define an application name.
fmt.Print(namePrompt + ": ")
c.Name = util.GetInput(c.Name)
c.Name = re.ReplaceAllString(c.Name, "")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@beeradb beeradb left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Requesting changes due to minor feedback issues. I do wonder if this should just be a validation operation instead of trying to strip characters, though.

It may make sense to add a config.Validate() method, which could check the active config for things like this, as well as other potential problems like a missing docroot or unknown application types at start time. Just a thought.

@rickmanelius

Copy link
Copy Markdown
Contributor

I see merit in Brad's comment about validation instead of substitution. Perhaps there is a better middle ground and we can use this as an opportunity to help guide an end-user. If someone tries to use ☃ as a valid UTF-8 character, we simply warn the user "We enforce site names to be alphanumeric with dashes. We detected your input to include non-supported characters. Would you like us to automatically convert them?" Or we can be a little more heavy handed "We automatically converted 'my-☃-site' to 'my--site'". That way we're not roadblocking the user but making them aware of what happened.

@tannerjfco

Copy link
Copy Markdown
Contributor Author

Given that config can be manually written I think it does make sense to perform validation on start. I could also see how stripping the sitename could be unexpected to the user, and realistically it's a simple correction to make, and we want them to make it so that the written configuration is valid. I'll move forward on the validation approach as suggested.

@tannerjfco tannerjfco changed the title normalize sitenames on config write and read, fixes #86 validate sitenames, validate config during init, fixes #86 Jun 20, 2017
Comment thread pkg/ddevapp/config.go
// validate docroot
fullPath := filepath.Join(c.AppRoot, c.Docroot)
if _, err := os.Stat(fullPath); os.IsNotExist(err) {
return fmt.Errorf("no directory could be found at %s. Please enter a valid docroot in your configuration", fullPath)

@beeradb beeradb Jun 20, 2017 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Absolutely love this. Thanks so much for adding it.

This might fall outside of this PR, but should we also use the app type detection function and issue a warning if a known app type isn't detected/doesn't match configured app type?

Happy to push that one down the road if you would prefer, it's just a check that comes to mind as potentially useful as I look at these others.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm a bit wary of doing this, as it means our detection would need to be more robust than it currently is. The big problem case I think of off-hand is that WordPress allows for a custom core directory, and a site using this structure would fail to detect as a WordPress site.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My thinking was that it would just be a warning about a potential problem rather than actually prevent the site from starting, but I can accept your answer either way :)

@rickmanelius

Copy link
Copy Markdown
Contributor

@tannerjfco I know you're moving onto the pre/post hook feature #183, but this would probably be a good WIP item to close out.

@beeradb beeradb left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code looks good and works as expected.

I do think we should fix tests in master before hitting the merge button, though.

@rickmanelius

Copy link
Copy Markdown
Contributor

So tempted to hit the big green merge button! :)

@tannerjfco
tannerjfco merged commit 67cb529 into ddev:master Jun 23, 2017
rfay pushed a commit to rfay/ddev that referenced this pull request Jun 27, 2017
)

* normalize sitenames on config write and read

* improve comment on regexp

* switch to a validation approach vs replacing invalid values, add config validation to Init
@rickmanelius rickmanelius mentioned this pull request Mar 5, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants