web-push-libs / pywebpush

Python Webpush Data encryption library
Mozilla Public License 2.0
303 stars 52 forks source link

For edge, I only get 400 Bad Request. I tested it in webpush library for node. It works properly. #161

Closed quroom closed 5 months ago

quroom commented 5 months ago
With pywebpush latest.
try:
    webpush(
        subscription_info={
            "endpoint": "https://wns2-pn1p.notify.windows.com/w/?token=BQYAAACFux1wyiPSDqVcMvnBRnb7Vg6NMdGRcmgJV6TDobZLzVvNFTogkgie6lSlOPAdGmtC10lpmcchVdIL8%2bll6FtsYOP9kA4M1CUZKB668YdgmIcg4FnHh4IWFOj5WHSGLLhhNuJw40%2f7TzpnDEJ2mH78dCBWwXYPTWoTRoYEVcqw6QjDYsLHojWHceVcWOHqokVLqDKBOhBGevirxY0ZthCJz91O9tTsE3gpCE%2bdrf9qhK%2bPJitGF0J6EM4h6xeSzERm6Qg3vZDTKmoNXM3RymJR5dUIZGWLKfbEWDJTncCjd8bibd7Lqg%2b0oqjdxYKQF8Q%3d",
            "keys": {
                "p256dh": "BNv-kpItGLybLtcl-RbVZKK5YrCwgJNMjLo9drzS6jCVm-YXMW21mQ5DK1pPrWPQG2cTKJH0QTXFc5IdfcD_Br8",
                "auth": "4PPU40n9wOfZqOKEDjD8KA"
            }},
        data="Mary had a little lamb, with a nice mint jelly",
        vapid_private_key="TqoRbiCxPrbbkU8HMacQ111-Yx3sNvy84Q_5ooJf5rY",
        vapid_claims={
                "sub": "mailto:YourNameHere@example.org",
            }
    )
except WebPushException as ex:
    print("I'm sorry, Dave, but I can't do that: {}", repr(ex))
    # Mozilla returns additional information in the body of the response.
    if ex.response and ex.response.json():
        extra = ex.response.json()
        print("Remote service replied with a {}:{}, {}",
              extra.code,
              extra.errno,
              extra.message
              )

I get only this response I'm sorry, Dave, but I can't do that: {} WebPushException('Push failed: 400 Bad Request')

With node webpush module.
  web-push send-notification  \
  --endpoint=https://wns2-pn1p.notify.windows.com/w/?token=BQYAAACFux1wyiPSDqVcMvnBRnb7Vg6NMdGRcmgJV6TDobZLzVvNFTogkgie6lSlOPAdGmtC10lpmcchVdIL8%2bll6FtsYOP9kA4M1CUZKB668YdgmIcg4FnHh4IWFOj5WHSGLLhhNuJw40%2f7TzpnDEJ2mH78dCBWwXYPTWoTRoYEVcqw6QjDYsLHojWHceVcWOHqokVLqDKBOhBGevirxY0ZthCJz91O9tTsE3gpCE%2bdrf9qhK%2bPJitGF0J6EM4h6xeSzERm6Qg3vZDTKmoNXM3RymJR5dUIZGWLKfbEWDJTncCjd8bibd7Lqg%2b0oqjdxYKQF8Q%3d  \
  --key=BNv-kpItGLybLtcl-RbVZKK5YrCwgJNMjLo9drzS6jCVm-YXMW21mQ5DK1pPrWPQG2cTKJH0QTXFc5IdfcD_Br8 \
  --auth=4PPU40n9wOfZqOKEDjD8KA \
  --vapid-subject=mailto:example@qq.com \
  --vapid-pubkey=BCNpfCmfaTVb_dsgucwqJXleQpk4b0xjHJFZnodrdofHcdh4UoEcWuRCVdvpoMKaIifxvlVPi2fpIWpuauZFXC0 \
  --vapid-pvtkey=TqoRbiCxPrbbkU8HMacQ111-Yx3sNvy84Q_5ooJf5rY \
  --payload=Hello

It works well same parameter in webpush node module. I don't know why it doesn't work well. Could you help me?

quroom commented 5 months ago

I am not sure what the problem was exactly. But now it works. I only changed urlBase64ToUint8Array function const base64 = (base64String + padding).replace(/-/g, "+").replace(/_/g, "/"); to

const base64 = (base64String + padding)
    // eslint-disable-next-line no-useless-escape
    .replace(/\-/g, "+")
    .replace(/_/g, "/");

I don't think it is related. As long as I figure out it, I will leave some comment.

quroom commented 5 months ago

It's because of ttl I didn't write.

jrconlin commented 5 months ago

Yep, the TTL is required (according to the RFC) I'll also caution about using standard base64 encoding. Python is very lax about what it accepts, but other languages (like Rust) are exceptionally pedantic about things.

send() should set ttl to 0 if you don't supply one. If it's not doing that, it might be a legit bug.

quroom commented 4 months ago

Yep, the TTL is required (according to the RFC) I'll also caution about using standard base64 encoding. Python is very lax about what it accepts, but other languages (like Rust) are exceptionally pedantic about things.

send() should set ttl to 0 if you don't supply one. If it's not doing that, it might be a legit bug.

Then I guess it should be set with default value or showing error for like "need to set TTL" Because without setting it, almost every browser works , but edge doesn't work and response 400 error not including much information.