misskey-dev / misskey

🌎 A completely free and open interplanetary microblogging platform 🚀
https://misskey-hub.net/
GNU Affero General Public License v3.0
9.99k stars 1.36k forks source link

APIに対して虚無JSONを送るとレスポンスが帰ってこない #2006

Closed AyaMorisawa closed 2 years ago

AyaMorisawa commented 6 years ago

https://misskey.xyz/notes/5b5ab8e040c98369543c4422

--- Want to back this issue? **[Post a bounty on it!](https://www.bountysource.com/issues/61400889-api-json?utm_campaign=plugin&utm_content=tracker%2F51027576&utm_medium=issues&utm_source=github)** We accept bounties via [Bountysource](https://www.bountysource.com/?utm_campaign=plugin&utm_content=tracker%2F51027576&utm_medium=issues&utm_source=github).
acid-chicken commented 5 years ago

おそらく原因はundefinedチェックをしていないため。

acid-chicken commented 5 years ago

Memo: ここら辺をいい感じに

https://github.com/syuilo/misskey/blob/89ac15b4de53770df661ef494b2c56220ff69b1a/src/server/api/call.ts#L51-L73

mei23 commented 5 years ago

(もう?) 起きない? ちゃんと返ってくる

$ curl -v -w '\n' -H 'Content-Type: application/json' -d '' http://host/api/notes/show
> POST /api/notes/show HTTP/1.1
> Host: host
> User-Agent: curl/7.55.1
> Accept: */*
> Content-Type: application/json
> Content-Length: 0
>
< HTTP/1.1 400 Bad Request
< Vary: Accept-Encoding, Origin
< Cache-Control: private, max-age=0, must-revalidate
< Content-Type: application/json; charset=utf-8
< Content-Length: 84
< Date: Tue, 25 Dec 2018 11:51:33 GMT
< Connection: keep-alive
<
{"error":{"code":"INVALID_PARAM","param":"noteId","reason":"must-be-not-undefined"}}
acid-chicken commented 5 years ago

いいえ、虚無とは{}のことで、現在もおきます。この場合、 https://github.com/syuilo/misskey/blob/89ac15b4de53770df661ef494b2c56220ff69b1a/src/server/api/call.ts#L54appにそのまま{}が渡されるため、各所でエラーが発生するようです。

mei23 commented 5 years ago

いや起きない

$ curl -v -w '\n' -H 'Content-Type: application/json' -d '{}' http://host/api/notes/show
> POST /api/notes/show HTTP/1.1
> Host: host
> User-Agent: curl/7.55.1
> Accept: */*
> Content-Type: application/json
> Content-Length: 2
>
< HTTP/1.1 400 Bad Request
< Vary: Accept-Encoding, Origin
< Cache-Control: private, max-age=0, must-revalidate
< Content-Type: application/json; charset=utf-8
< Content-Length: 84
< Date: Tue, 25 Dec 2018 11:55:15 GMT
< Connection: keep-alive
<
{"error":{"code":"INVALID_PARAM","param":"noteId","reason":"must-be-not-undefined"}}
acid-chicken commented 5 years ago

自環境だと起きる……

  <-- POST /api/users/show
TypeError: Cannot read property 'toLowerCase' of undefined
    at Promise (/Users/acid-chicken/misskey/built/server/api/endpoints/users/show.js:64:48)
    at new Promise (<anonymous>)
    at exports.default.define_1.default (/Users/acid-chicken/misskey/built/server/api/endpoints/users/show.js:38:62)
    at Object.exec (/Users/acid-chicken/misskey/built/server/api/define.js:17:16)
    at Promise (/Users/acid-chicken/misskey/built/server/api/call.js:43:24)
    at new Promise (<anonymous>)
    at Object.exports.default (/Users/acid-chicken/misskey/built/server/api/call.js:6:56)
    at exports.default (/Users/acid-chicken/misskey/built/server/api/api-handler.js:34:35)
    at process.internalTickCallback (internal/process/next_tick.js:77:7)
% curl -v -w '\n' -H 'Content-Type: application/json' -d '{}' http://localhost:3000/api/users/show
*   Trying ::1...
* TCP_NODELAY set
* Connected to localhost (::1) port 3000 (#0)
> POST /api/users/show HTTP/1.1
> Host: localhost:3000
> User-Agent: curl/7.54.0
> Accept: */*
> Content-Type: application/json
> Content-Length: 2
>
* upload completely sent off: 2 out of 2 bytes
mei23 commented 5 years ago

あーparamのValidate依存

mei23 commented 5 years ago

この場合パラメータのバリデーションはちゃんと通過しているみたい たぶんendpoint固有の問題だと思う

バリデーションではuserIdusernameはどちらも未定義であることを許容しているけど 使用しているときにはuserIdusernameの両方が未定義であることを想定してないので usernameでエラーが出る https://github.com/syuilo/misskey/blob/fa03c172f211f84dd9747fcf312ec41a92d1102b/src/server/api/endpoints/users/show.ts#L66-L68

syuilo commented 5 years ago

なるほど。 では両方undefinedならばエラーにする処理入れれば良さそうですね

acid-chicken commented 5 years ago

なるほど。 では両方undefinedならばエラーにする処理入れれば良さそうですね

可能であればバリデーションの強化か、オーバーロードへの対応などをした方が良さそう。

syuilo commented 5 years ago

バリデーションの合否を他のバリデーションに依存させるのは若干実装が複雑になりそうな感じはしますね

acid-chicken commented 5 years ago

ではオーバーロードにしませんか。見た目が非常に良くなると思います。

tamaina commented 3 years ago

めっちゃ古いIssueを見つけたのですが、さすがに直ってますよね…?

EbiseLutica commented 3 years ago

まだ残ってる image

mei23 commented 3 years ago

API console は JSON以外のものを入れるとクライアント側でエラーになるのでそれは別のエラー

SyntaxError: JSON5: invalid end of input at 1:1

rinsuki commented 3 years ago

ちゃんと users/show はエラー返す

$ curl -w '\n' -H 'Content-Type: application/json' -d '{}' https://misskey.io/api/users/show
{"error":{"message":"Internal error occurred. Please contact us if the error persists.","code":"INTERNAL_ERROR","id":"5d37dbcb-891e-41ca-a3d6-e690c97775ac","kind":"server","info":{"e":{"message":"Cannot read property 'toLowerCase' of undefined","code":"TypeError","stack":"TypeError: Cannot read property 'toLowerCase' of undefined\n    at /misskey/built/server/api/endpoints/users/show.js:91:48\n    at Object.exec (/misskey/built/server/api/define.js:22:16)\n    at Object.exports.default (/misskey/built/server/api/call.js:64:21)\n    at /misskey/built/server/api/api-handler.js:26:23\n    at runMicrotasks (<anonymous>)\n    at processTicksAndRejections (internal/process/task_queues.js:93:5)"}}}}

他のエンドポイントがどうかはわからん

mei23 commented 3 years ago

APIでなにか起きると何も返ってこないのは https://github.com/syuilo/misskey/commit/2756f553c68082342a784ef716c62da6cea6f3ca でなおってるはず

どれかのパラメーターが必要だけどパラメーターエラーにしてくれないエンドポイントは結構ある users/show {} => INTERNAL_ERROR が返るけど INVALID_PARAM を返して欲しい pages/show {} => NO_SUCH_PAGE が返るけど INVALID_PARAM を返して欲しい :