TheThingsNetwork / lorawan-stack

The Things Stack, an Open Source LoRaWAN Network Server
https://www.thethingsindustries.com/stack/
Apache License 2.0
980 stars 309 forks source link

Network Server panic on end device creation #4731

Closed bafonins closed 3 years ago

bafonins commented 3 years ago

Summary

NS returns error 500 when creating a new end device and adding "ids" field mask. This is what I see in the logs:

runtime error: invalid memory address or nil pointer dereference

Steps to Reproduce

  1. Register application with app-id
  2. Create end device in NS via this curl command:
    curl --location --request PUT 'http://localhost:8080/api/v3/ns/applications/{app-id}/devices/{device-id}' \
    --header 'Authorization: Bearer ...' \
    --header 'Content-Type: application/json' \
    --data-raw '{
    "end_device": {
        "ids": {
            "device_id": {device-id},
            "dev_eui": "1111111111111111",
            "join_eui": "2222222222222222"
          },
          "supports_join": true
    },
    "field_mask": {"paths": ["ids", "ids.device_id", "ids.join_eui", "ids.dev_eui", "supports_join"]}
    }'
  3. See NS returning 500 on such request
  4. Try removing the "ids" field mask from the request:
    curl --location --request PUT 'http://localhost:8080/api/v3/ns/applications/{app-id}/devices/{device-id}' \
    --header 'Authorization: Bearer ...' \
    --header 'Content-Type: application/json' \
    --data-raw '{
    "end_device": {
        "ids": {
            "device_id": {device-id},
            "dev_eui": "1111111111111111",
            "join_eui": "2222222222222222"
          },
          "supports_join": true
    },
    "field_mask": {"paths": ["ids.device_id", "ids.join_eui", "ids.dev_eui", "supports_join"]}
    }'

What do you see now?

NS returns error 500

What do you want to see instead?

No panic

Can you do this yourself and submit a Pull Request?

no

bafonins commented 3 years ago

@adriansmares please assign milestone

bafonins commented 3 years ago

Found another one. NS panic when no lorawan_version and lorawan_phy_version provided in creation request.

Without lorawan_version and lorawan_phy_version:

curl --location --request PUT 'http://localhost:8080/api/v3/ns/applications/{app-id}/devices/{device-id}' \
--header 'Authorization: Bearer ... \
--header 'Content-Type: application/json' \
--data-raw '{
    "end_device": {
        "multicast": true,
        "ids": {
            "device_id": {device-id}
        },
        "session": {
            "keys": {
                "f_nwk_s_int_key": {
                    "key": "C87C8BEFC72E7A3011218B027CCB5057"
                }
            },
            "dev_addr": "01A8B2AE"
        },
        "supports_class_c": true,
        "frequency_plan_id": "EU_863_870"
    },
    "field_mask": {
        "paths": [
            "multicast",
            "ids.device_id",
            "session.keys.f_nwk_s_int_key.key",
            "session.dev_addr",
            "supports_class_c",
            "frequency_plan_id"
        ]
    }
}'

Response:

{
    "code": 13,
    "message": "error:pkg/rpcserver:rpc_recovered (Internal Server Error)",
    "details": [
        {
            "@type": "type.googleapis.com/ttn.lorawan.v3.ErrorDetails",
            "namespace": "pkg/rpcserver",
            "name": "rpc_recovered",
            "message_format": "Internal Server Error",
            "correlation_id": "9da93977a6de4b26827a9ce6942497d0",
            "code": 13
        }
    ]
}

With lorawan_version and lorawan_phy_version inplace:

curl --location --request PUT 'http://localhost:8080/api/v3/ns/applications/{app-id}/devices/{device-id}' \
--header 'Authorization: Bearer ... \
--header 'Content-Type: application/json' \
--data-raw '{
    "end_device": {
        "multicast": true,
        "ids": {
            "device_id": {device-id}
        },
        "session": {
            "keys": {
                "f_nwk_s_int_key": {
                    "key": "C87C8BEFC72E7A3011218B027CCB5057"
                }
            },
            "dev_addr": "01A8B2AE"
        },
        "supports_class_c": true,
        "lorawan_version": "MAC_V1_0",
        "frequency_plan_id": "EU_863_870",
        "lorawan_phy_version": "PHY_V1_0"

    },
    "field_mask": {
        "paths": [
            "multicast",
            "ids.device_id",
            "session.keys.f_nwk_s_int_key.key",
            "session.dev_addr",
            "supports_class_c",
            "lorawan_version",
            "frequency_plan_id",
            "lorawan_phy_version"
        ]
    }
}'

NS return 200 as expected