Project

General

Profile

Actions

Feature #9045

closed

URIに含まれるエンコードされたID/パスワードがデコードされない

Added by vmi (Motonori IWAMURO) over 10 years ago. Updated almost 2 years ago.

Status:
Closed
Target version:
-
[ruby-dev:47768]

Description

URI(プロキシ設定も含む)にID/パスワードを含めるとき、IDやパスワードに「@」や「:」等がある場合はURIエンコードする必要がありますが、uriライブラリがこれをデコードしてくれません。
そのため、認証プロキシでIDに「@」が含まれていると認証に失敗するなどの問題が生じます。

ソースを追っかけて uri/generic.rb の問題だろうというところまでは調べたのですが、user/passwordを取り扱う部分がたくさんあるので、何処でエンコード/デコードすればいいのかよくわからない状況です。

とりあえず手元ではdef user と def password に URI.decode_www_form_component を突っ込んでますが、たぶんこれでは片手落ちと思われます。

Updated by vmi (Motonori IWAMURO) over 10 years ago

追記。
#8979 のそもそもの原因はこれだと思うのですが、あっちは rubygems の方直してるんですね……。
(こちらもgemが通らなくて調べてたのですが)
利用側でデコードするのと、提供側でデコードするのと、どちらが適切なんでしょう?

Updated by sorah (Sorah Fukumori) over 10 years ago

  • Status changed from Open to Assigned
  • Assignee set to akira (akira yamada)

他のメソッドもデコードしてくれる訳じゃないですしuser/passだけ特別扱いというのはどうも。

URI.parse('http://example.com/%2F').path
=> "/%2F"

Updated by fumiyas (Fumiyasu SATOH) over 10 years ago

At Wed, 23 Oct 2013 19:19:45 +0900,
sorah (Shota Fukumori) wrote:

他のメソッドもデコードしてくれる訳じゃないですしuser/passだけ特別扱いというのはどうも。

URI.parse('http://example.com/%2F').path
=> "/%2F"

URI::Component というクラスを作っている者です。

http://rubygems.org/gems/uri-component

現状、ドキュメントなしでテスト・実績も不十分ですが、
こんな感じ↓に使えます。ご参考まで。

require "uri/component"

URI::Component.mixin

u=URI.parse("http://alice:p%40s%25sword@example.com/dir%2Fname/subdir?foo=bar%40example.com")
p u.userinfo_component.user

=> "alice"

p u.userinfo_component.password

=> "p@s%sword"

p u.path_component.nodes

=> ["dir/name", "subdir"]

p u.query_component['foo']

=> [""]

p u.query_component.params

=> {"foo"=>[""]}


Bug #9045: URIに含まれるエンコードされたID/パスワードがデコードされない
https://bugs.ruby-lang.org/issues/9045#change-42564

--
-- Name: SATOH Fumiyasu @ OSS Technology Corp. (fumiyas @ osstech co jp)
-- Business Home: http://www.OSSTech.co.jp/
-- GitHub Home: https://GitHub.com/fumiyas/
-- PGP Fingerprint: BBE1 A1C9 525A 292E 6729 CDEC ADC2 9DCA 5E1C CBCA

Updated by vmi (Motonori IWAMURO) over 10 years ago

sorah
他のメソッドもデコードしてくれる訳じゃないですしuser/passだけ特別扱いというのはどうも。

とは言え、サーバ側が良きにはからってくれるpath等と違ってuser/passwordは完全一致でないと許されないので、逆に特別扱いすべき、とも考えられます。

落とし所は以下のどれかでしょうか。

(1) 制約事項をドキュメントに明記し、現行のままいじらない。(下手にいじると #8979 みたいなケースと衝突しそうだし)
(2) URIエンコード/デコードするためのスイッチをインスタンス変数orメソッド引数に組み込む。(新規で使う場合は無駄に手間がかかる)
(3) setter/getterやらuserinfoとのやりとりやらURIのエンコード/デコードやらをきっちり追いかけて、正確な値を設定/取得できるよう考慮したパッチを作成する。(パッチの前後で仕様が変わるので、複数の処理系をサポートしているプログラムだと対応が面倒かも)

fumiyas

新規で使う場合は良いですね。ただ、今回の件はそもそもgemが動かないことが切っ掛けだったので……。

Actions #5

Updated by hsbt (Hiroshi SHIBATA) about 4 years ago

  • Tags set to uriencoding, lib
  • Backport deleted (1.9.3: UNKNOWN, 2.0.0: UNKNOWN)

Updated by jeremyevans0 (Jeremy Evans) about 3 years ago

  • Tracker changed from Bug to Feature
  • ruby -v deleted (1.9.3p448)

This is a request relating to #user and #password for URIs returning actual (encoded) values and not decoded values. The fact that #user and #password return the actual and not decoded values is not a bug, and changing the behavior of the methods would break backwards compatibility. It seems simplest to address this need by adding #decoded_user and #decoded_password methods. I've submitted a PR which does that: https://github.com/ruby/uri/pull/17

Actions #7

Updated by jeremyevans (Jeremy Evans) almost 2 years ago

  • Status changed from Assigned to Closed

Applied in changeset git|fbebfe1697938a684f460cd28af36cf1f056513c.


[ruby/uri] Add URI::Generic#decoded_#{user,password}

URI::Generic#{user,password} return the encoded values, which are
not that useful if you want to do authentication with them.
Automatic decoding by default would break backwards compatibility.
Optional automatic decoding via a keyword to URI.parse would
require threading the option through at least 3 other methods, and
would make semantics confusing (user= takes encoded or unencoded
password?) or require more work. Thus, adding this as a separate
method seemed the simplest approach.

Unfortunately, URI lacks a method for correct decoding. Unlike in
www form components, + in earlier parts of the URI such as the
userinfo section is treated verbatim and not as an encoded space.
Add URI.#{en,de}code_uri_component methods, which are almost the
same as URI.#{en,de}code_www_form_component, but without the
special SP => + handling.

Implements [Feature #9045]

https://github.com/ruby/uri/commit/16cfc4e92f

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0Like0