online-judge-tools / api-client

API client to develop tools for competitive programming
MIT License
64 stars 18 forks source link

Receiving `403 Forbidden` when submitting to Codeforces #102

Closed morioprog closed 4 years ago

morioprog commented 4 years ago

Description

When I try to submit to Codeforces, it receives 403 Forbidden error message.

Error log

$ oj s https://codeforces.com/contest/1400/problem/E main.cpp
[INFO] online-judge-tools 10.1.2 (+ online-judge-api-client 10.3.2)
[INFO] read history from: /Users/morio/Library/Caches/online-judge-tools/download-history.jsonl
[INFO] found urls in history:
https://codeforces.com/contest/1400/problem/E
[INFO] problem recognized: CodeforcesProblem.from_url('https://codeforces.com/contest/1400/problem/E'): https://codeforces.com/contest/1400/problem/E
[INFO] code (9087 byte):

** MY SOLUTION **

[INFO] load cookie from: /Users/morio/Library/Application Support/online-judge-tools/cookie.jar
[NETWORK] GET: https://codeforces.com/contest/1400/problem/E
[NETWORK] 200 OK
[INFO] both GCC and Clang are available for C++ compiler
[INFO] use: GCC
[INFO] chosen language: 61 (GNU G++17 9.2.0 (64 bit, msys 2))
[INFO] sleep(3.00)
Are you sure? [y/N] y
[NETWORK] GET: https://codeforces.com/contest/1400/problem/E
[NETWORK] 200 OK
[NETWORK] POST: https://codeforces.com/contest/1400/problem/E
[NETWORK] 403 Forbidden
[ERROR] 403 Client Error:  for url: https://codeforces.com/contest/1400/problem/E
Traceback (most recent call last):
  File "/Users/morio/.pyenv/versions/3.8.2/lib/python3.8/site-packages/onlinejudge_command/main.py", line 242, in main
    run_program(parsed, parser=parser)
  File "/Users/morio/.pyenv/versions/3.8.2/lib/python3.8/site-packages/onlinejudge_command/main.py", line 212, in run_program
    submit(args)
  File "/Users/morio/.pyenv/versions/3.8.2/lib/python3.8/site-packages/onlinejudge_command/subcommand/submit.py", line 131, in submit
    submission = problem.submit_code(code, language_id=LanguageId(args.language), session=sess)
  File "/Users/morio/.pyenv/versions/3.8.2/lib/python3.8/site-packages/onlinejudge/service/codeforces.py", line 378, in submit_code
    resp = form.request(session=session)
  File "/Users/morio/.pyenv/versions/3.8.2/lib/python3.8/site-packages/onlinejudge/_implementation/utils.py", line 107, in request
    return request(method, url, session=session, raise_for_status=raise_for_status, data=self.payload, files=self.files, **kwargs)
  File "/Users/morio/.pyenv/versions/3.8.2/lib/python3.8/site-packages/onlinejudge/_implementation/utils.py", line 172, in request
    resp.raise_for_status()
  File "/Users/morio/.pyenv/versions/3.8.2/lib/python3.8/site-packages/requests/models.py", line 941, in raise_for_status
    raise HTTPError(http_error_msg, response=self)
requests.exceptions.HTTPError: 403 Client Error:  for url: https://codeforces.com/contest/1400/problem/E

Environment

macOS Catalina 10.15.6 (19G2021)

$ python3 --version
Python 3.8.2

$ oj --version
[INFO] GET: https://pypi.org/pypi/online-judge-tools/json
[INFO] 200 OK
[INFO] GET: https://pypi.org/pypi/online-judge-api-client/json
[INFO] 200 OK
online-judge-tools 10.1.2 (+ online-judge-api-client 10.3.2)

Other notes

kmyk commented 4 years ago

@morioprog just to confirm: are you logged in?

morioprog commented 4 years ago

@kmyk I checked, and I'm logged in. I also logged in again by deleting cookie.jar, but it still fails.

$ oj l https://codeforces.com
[INFO] online-judge-tools 10.1.2 (+ online-judge-api-client 10.3.2)
[INFO] service recognized: CodeforcesService.from_url('https://codeforces.com/'): https://codeforces.com
[INFO] load cookie from: /Users/morio/Library/Application Support/online-judge-tools/cookie.jar
[NETWORK] GET: https://codeforces.com/enter
[NETWORK] 302 Found
[SUCCESS] You have already signed in.
[INFO] save cookie to: /Users/morio/Library/Application Support/online-judge-tools/cookie.jar
kmyk commented 4 years ago

@morioprog thank you for checking!

kmyk commented 4 years ago

It's a problem about User-Agent. I'll stop sending them.

They accepts the usual UA like Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.130 Safari/537.36 or just no UA, but denies others. It seems implemented based on an allowlist. I think that they should use denylist instead and accepting the no UA is just their bug.