Skip to content

Sync with Ruby 2.6.6 webrick #53

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

Merged
merged 4 commits into from
Aug 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
sudo: false
language: ruby
rvm:
- 2.3.6
- 2.4.3
- 2.5.0
- ruby-head
- 2.3.8
- 2.4.9
- 2.5.8
- 2.6.6
before_install: gem install bundler
script: rake
6 changes: 6 additions & 0 deletions lib/webrick/.document
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Add files to this as they become documented

*.rb

httpauth
httpservlet
10 changes: 7 additions & 3 deletions lib/webrick/cgi.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
#
# $Id$

require "webrick/httprequest"
require "webrick/httpresponse"
require "webrick/config"
require_relative "httprequest"
require_relative "httpresponse"
require_relative "config"
require "stringio"

module WEBrick
Expand Down Expand Up @@ -265,6 +265,10 @@ def <<(data)
@out_port << data
end

def write(data)
@out_port.write(data)
end

def cert
return nil unless defined?(OpenSSL)
if pem = @env["SSL_SERVER_CERT"]
Expand Down
10 changes: 5 additions & 5 deletions lib/webrick/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@
#
# $IPR: config.rb,v 1.52 2003/07/22 19:20:42 gotoyuzo Exp $

require 'webrick/version'
require 'webrick/httpversion'
require 'webrick/httputils'
require 'webrick/utils'
require 'webrick/log'
require_relative 'version'
require_relative 'httpversion'
require_relative 'httputils'
require_relative 'utils'
require_relative 'log'

module WEBrick
module Config
Expand Down
2 changes: 1 addition & 1 deletion lib/webrick/cookie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
# $IPR: cookie.rb,v 1.16 2002/09/21 12:23:35 gotoyuzo Exp $

require 'time'
require 'webrick/httputils'
require_relative 'httputils'

module WEBrick

Expand Down
10 changes: 5 additions & 5 deletions lib/webrick/httpauth.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@
#
# $IPR: httpauth.rb,v 1.14 2003/07/22 19:20:42 gotoyuzo Exp $

require 'webrick/httpauth/basicauth'
require 'webrick/httpauth/digestauth'
require 'webrick/httpauth/htpasswd'
require 'webrick/httpauth/htdigest'
require 'webrick/httpauth/htgroup'
require_relative 'httpauth/basicauth'
require_relative 'httpauth/digestauth'
require_relative 'httpauth/htpasswd'
require_relative 'httpauth/htdigest'
require_relative 'httpauth/htgroup'

module WEBrick

Expand Down
18 changes: 13 additions & 5 deletions lib/webrick/httpauth/basicauth.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
#
# $IPR: basicauth.rb,v 1.5 2003/02/20 07:15:47 gotoyuzo Exp $

require 'webrick/config'
require 'webrick/httpstatus'
require 'webrick/httpauth/authenticator'
require_relative '../config'
require_relative '../httpstatus'
require_relative 'authenticator'

module WEBrick
module HTTPAuth
Expand All @@ -24,7 +24,7 @@ module HTTPAuth
#
# config = { :Realm => 'BasicAuth example realm' }
#
# htpasswd = WEBrick::HTTPAuth::Htpasswd.new 'my_password_file'
# htpasswd = WEBrick::HTTPAuth::Htpasswd.new 'my_password_file', password_hash: :bcrypt
# htpasswd.set_passwd config[:Realm], 'username', 'password'
# htpasswd.flush
#
Expand Down Expand Up @@ -81,7 +81,15 @@ def authenticate(req, res)
error("%s: the user is not allowed.", userid)
challenge(req, res)
end
if password.crypt(encpass) != encpass

case encpass
when /\A\$2[aby]\$/
password_matches = BCrypt::Password.new(encpass.sub(/\A\$2[aby]\$/, '$2a$')) == password
else
password_matches = password.crypt(encpass) == encpass
end

unless password_matches
error("%s: password unmatch.", userid)
challenge(req, res)
end
Expand Down
33 changes: 10 additions & 23 deletions lib/webrick/httpauth/digestauth.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
#
# $IPR: digestauth.rb,v 1.5 2003/02/20 07:15:47 gotoyuzo Exp $

require 'webrick/config'
require 'webrick/httpstatus'
require 'webrick/httpauth/authenticator'
require_relative '../config'
require_relative '../httpstatus'
require_relative 'authenticator'
require 'digest/md5'
require 'digest/sha1'

Expand Down Expand Up @@ -235,9 +235,11 @@ def _authenticate(req, res)
ha2 = hexdigest(req.request_method, auth_req['uri'])
ha2_res = hexdigest("", auth_req['uri'])
elsif auth_req['qop'] == "auth-int"
ha2 = hexdigest(req.request_method, auth_req['uri'],
hexdigest(req.body))
ha2_res = hexdigest("", auth_req['uri'], hexdigest(res.body))
body_digest = @h.new
req.body { |chunk| body_digest.update(chunk) }
body_digest = body_digest.hexdigest
ha2 = hexdigest(req.request_method, auth_req['uri'], body_digest)
ha2_res = hexdigest("", auth_req['uri'], body_digest)
end

if auth_req['qop'] == "auth" || auth_req['qop'] == "auth-int"
Expand Down Expand Up @@ -288,23 +290,8 @@ def _authenticate(req, res)

def split_param_value(string)
ret = {}
while string.bytesize != 0
case string
when /^\s*([\w\-\.\*\%\!]+)=\s*\"((\\.|[^\"])*)\"\s*,?/
key = $1
matched = $2
string = $'
ret[key] = matched.gsub(/\\(.)/, "\\1")
when /^\s*([\w\-\.\*\%\!]+)=\s*([^,\"]*),?/
key = $1
matched = $2
string = $'
ret[key] = matched.clone
when /^s*^,/
string = $'
else
break
end
string.scan(/\G\s*([\w\-.*%!]+)=\s*(?:\"((?>\\.|[^\"])*)\"|([^,\"]*))\s*,?/) do
ret[$1] = $3 || $2.gsub(/\\(.)/, "\\1")
end
ret
end
Expand Down
4 changes: 2 additions & 2 deletions lib/webrick/httpauth/htdigest.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
#
# $IPR: htdigest.rb,v 1.4 2003/07/22 19:20:45 gotoyuzo Exp $

require 'webrick/httpauth/userdb'
require 'webrick/httpauth/digestauth'
require_relative 'userdb'
require_relative 'digestauth'
require 'tempfile'

module WEBrick
Expand Down
11 changes: 7 additions & 4 deletions lib/webrick/httpauth/htgroup.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,18 @@ def reload

def flush(output=nil)
output ||= @path
tmp = Tempfile.new("htgroup", File::dirname(output))
tmp = Tempfile.create("htgroup", File::dirname(output))
begin
@group.keys.sort.each{|group|
tmp.puts(format("%s: %s", group, self.members(group).join(" ")))
}
ensure
tmp.close
File::rename(tmp.path, output)
rescue
tmp.close(true)
if $!
File.unlink(tmp.path)
else
return File.rename(tmp.path, output)
end
end
end

Expand Down
41 changes: 37 additions & 4 deletions lib/webrick/httpauth/htpasswd.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
#
# $IPR: htpasswd.rb,v 1.4 2003/07/22 19:20:45 gotoyuzo Exp $

require 'webrick/httpauth/userdb'
require 'webrick/httpauth/basicauth'
require_relative 'userdb'
require_relative 'basicauth'
require 'tempfile'

module WEBrick
Expand All @@ -35,11 +35,29 @@ class Htpasswd
##
# Open a password database at +path+

def initialize(path)
def initialize(path, password_hash: nil)
@path = path
@mtime = Time.at(0)
@passwd = Hash.new
@auth_type = BasicAuth
@password_hash = password_hash

case @password_hash
when nil
# begin
# require "string/crypt"
# rescue LoadError
# warn("Unable to load string/crypt, proceeding with deprecated use of String#crypt, consider using password_hash: :bcrypt")
# end
@password_hash = :crypt
when :crypt
# require "string/crypt"
when :bcrypt
require "bcrypt"
else
raise ArgumentError, "only :crypt and :bcrypt are supported for password_hash keyword argument"
end

File.open(@path,"a").close unless File.exist?(@path)
reload
end
Expand All @@ -56,6 +74,14 @@ def reload
line.chomp!
case line
when %r!\A[^:]+:[a-zA-Z0-9./]{13}\z!
if @password_hash == :bcrypt
raise StandardError, ".htpasswd file contains crypt password, only bcrypt passwords supported"
end
user, pass = line.split(":")
when %r!\A[^:]+:\$2[aby]\$\d{2}\$.{53}\z!
if @password_hash == :crypt
raise StandardError, ".htpasswd file contains bcrypt password, only crypt passwords supported"
end
user, pass = line.split(":")
when /:\$/, /:{SHA}/
raise NotImplementedError,
Expand Down Expand Up @@ -102,7 +128,14 @@ def get_passwd(realm, user, reload_db)
# Sets a password in the database for +user+ in +realm+ to +pass+.

def set_passwd(realm, user, pass)
@passwd[user] = make_passwd(realm, user, pass)
if @password_hash == :bcrypt
# Cost of 5 to match Apache default, and because the
# bcrypt default of 10 will introduce significant delays
# for every request.
@passwd[user] = BCrypt::Password.create(pass, :cost=>5)
else
@passwd[user] = make_passwd(realm, user, pass)
end
end

##
Expand Down
Loading