没有不值得去解决的问题,也没有不值得去学习的技术!

Implementing Secure Validation in Laravel 6, Modules, and Lighthouse With @rules and Exists

Test whether the validation rule is effective; it is confirmed to be effective

1. The structure when a request responds successfully. See Figure 1.

The structure when a request responds successfully
Figure 1
GraphQL


mutation {
  onlineStoreThemePreviewCodeGenerate(themeId: "vogue") {
    themePreviewCode
  }
}


JSON

{
  "data": {
    "onlineStoreThemePreviewCodeGenerate": {
      "themePreviewCode": "eyJpdiI6IjZlZ3RpZzlyZmp6S3BzQWJcL0N1NVR3PT0iLCJ2YWx1ZSI6IkNGdVwvdGJMZFI2MWJPRXFMbTNhdmVOUVVCVDhzb1ZnSzFNQzd2Y1RoSElGMmw4VkxOWFppbnlNbmtjaFNnbG9FXC9Oa1hYSndRU1hlcmpFMktneFNkQmVoMjhENnoxb3dQY0lxNHZnemJrNXlLTlpNKzJmbEU4RTFXNnFza2dyVG4iLCJtYWMiOiI0NzcwZjllYjIxZDliOGFkMTU2OTdiZmVmYWViN2I2OTI5NWE0ZDFjOTBmOGU1MGMyZjI3MzBjNTQxMWE3ODQ2In0="
    }
  }
}

2. However, at this stage, no security validation is performed on the request parameters. Reference: https://lighthouse-php.com/master/security/validation.html#single-arguments . Lighthouse allows you to use Laravel’s validation in queries and mutations.

3. The mutation for this GraphQL API has only one request parameter, themeId, so I only need to validate whether this field exists in the table.

4. The simplest way to utilize built-in validation rules is to use the @rules directive. Use the Exists rule.

GraphQL

extend type Mutation {
    "生成主题预览代码"
    onlineStoreThemePreviewCodeGenerate(themeId: ID! @rules(apply: ["exists:theme_asset,theme_id"])): OnlineStoreThemePreviewCodeGeneratePayload @field(resolver: "Modules\\ThemeStore\\Resolver\\ThemePreview\\GenerateThemePreviewCodeResolver")
}

5. Test whether the validation rule is effective; it is confirmed to be effective. See Figure 2.

Test whether the validation rule is effective; it is confirmed to be effective
Figure 2
GraphQL


mutation {
  onlineStoreThemePreviewCodeGenerate(themeId: "vogue1") {
    themePreviewCode
  }
}


JSON

{
  "errors": [
    {
      "message": "Validation failed for the field [onlineStoreThemePreviewCodeGenerate].",
      "extensions": {
        "validation": {
          "themeId": [
            "The selected theme id is invalid."
          ]
        },
        "category": "validation"
      },
      "locations": [
        {
          "line": 2,
          "column": 3
        }
      ],
      "path": [
        "onlineStoreThemePreviewCodeGenerate"
      ],
      "trace": ...
    }
  ],
  "data": {
    "onlineStoreThemePreviewCodeGenerate": null
  }
}

6. View the SQL statement in the request within Laravel Telescope.

SQL

select
  count(*) as aggregate
from
  `theme_asset`
where
  `theme_id` = 'vogue1'

7. However, there is still a problem at this stage because the table name prefix happens to be ”. If it is set to ‘object_’, there might be an issue. I made another request and found that it threw an error, which is as expected. This indicates that the validation rule automatically reads the table prefix. No adjustment is needed. See Figure 3.

However, there is still a problem at this stage because the table name prefix happens to be ''. If it is set to 'object_', there might be an issue. I made another request and found that it threw an error, which is as expected. This indicates that the validation rule automatically reads the table prefix. No adjustment is needed
Figure 3
Plaintext

        'mysql' => [
            ...
            'prefix' => 'object_',
        ],

JSON

{
  "errors": [
    {
      "debugMessage": "SQLSTATE[42S02]: Base table or view not found: 1146 Table 'object_store.object_theme_asset' doesn't exist (SQL: select count(*) as aggregate from `object_theme_asset` where `theme_id` = vogue1)",
      "message": "Internal server error",
      "extensions": {
        "category": "internal"
      },
      "locations": [
        {
          "line": 2,
          "column": 3
        }
      ],
      "path": [
        "onlineStoreThemePreviewCodeGenerate"
      ],
      "trace": [
        ...
      ]
    }
  ],
  "data": {
    "onlineStoreThemePreviewCodeGenerate": null
  }
}

关于作者 · 持续学习与技术实践

我是一名拥有 15 年以上开发经验的技术从业者,自 2013 年起持续运营个人技术博客,记录工作与个人项目中遇到的真实问题,以及解决问题过程中的探索、实践和思考。

除了技术写作,我也在持续开发和维护自己的独立项目,探索新技术和新工具在实际场景中的应用。我始终相信,没有不值得去解决的问题,也没有不值得去学习的技术。

如果这篇文章对你有所帮助,欢迎浏览博客中的其他文章,也欢迎交流技术经验与想法。

关于我  ·  GitHub  ·  邮件联系