Comments (2)
Sensitive()
means that the field is not serializable (i.e. json
struct tags aren't added and/or are set to -
), so this is working as expected, I think. Since Echos Bind()
likely uses JSON struct tags behind the scenes, and JSON doesn't support one-way struct tags for marshalling vs unmarshalling, there is no way to have both.
I don't believe you should be unmarshalling directly to an ent type -- as that bypasses various checks. You should have structs specifically for your HTTP methods that can then be used with Set() and similar. This will also ensure that if you add a field that you don't want the user to set, Bind()
can't be used maliciously to override those fields. Without this, I see it as a future security risk.
from ent.
Sensitive()
means that the field is not serializable (i.e.json
struct tags aren't added and/or are set to-
), so this is working as expected, I think. Since EchosBind()
likely uses JSON struct tags behind the scenes, and JSON doesn't support one-way struct tags for marshalling vs unmarshalling, there is no way to have both.I don't believe you should be unmarshalling directly to an ent type -- as that bypasses various checks. You should have structs specifically for your HTTP methods that can then be used with Set() and similar. This will also ensure that if you add a field that you don't want the user to set,
Bind()
can't be used maliciously to override those fields. Without this, I see it as a future security risk.
Thank you for your explanation, it’s very clear. I understand that Sensitive() means the field is not serializable, which is why Echo’s Bind() function can’t use it directly. I also understand that JSON doesn’t support one-way struct tags for marshalling vs unmarshalling, so there’s no way to have both.
I agree with your point that I shouldn’t be unmarshalling directly to an ent type, as this bypasses various checks. I should create structs specifically for my HTTP methods that can then be used with Set() and similar. This will ensure that if I add a field that I don’t want the user to set, Bind() can’t be used maliciously to override those fields, thereby avoiding a future security risk.
package main
import (
"github.com/labstack/echo/v4"
"net/http"
)
type User struct {
Username string `json:"-"`
Password string `json:"password"`
}
type UserDTO struct {
Username string `json:"username"`
Password string `json:"password"`
}
func main() {
e := echo.New()
e.POST("/users", func(c echo.Context) error {
u := new(UserDTO)
if err := c.Bind(u); err != nil {
return err
}
user := User{
Username: u.Username,
Password: u.Password,
}
// saveUser(user)
return c.JSON(http.StatusCreated, u)
})
e.Start(":8080")
}
This code creates a UserDTO structure to receive json data transmitted from the front end. Then, we safely transmit this data to our User entity. In this way, we can effectively bind data while protecting sensitive information. Thanks again for your help!
from ent.
Related Issues (20)
- does entio support postgreesql RUM feature? HOT 1
- Expected value of the generated WithFixedNodeType?
- How to generate a schema file from an existing MySQL database HOT 1
- schema.WriteDriver neglects dialect
- How to use "sql.In" with uuids? HOT 1
- Opt-in mode for entgql.WithSchemaGenerator
- Use ent to import sql dump for postgres
- Partial indexes are always re-created when generating a migration file HOT 2
- table COMMENT Annotations will be automatically migrated and overwritten HOT 1
- entc/gen: set "TUser" foreign-keys: edge "t_wallets_user_id" has a field "user_id" but it is not holding a foreign key
- Atlas migrations don't work with sqlite fts5 virtual tables HOT 6
- Type safe IDs do not work
- Creating a row with a JSON column fails in Planetscale
- CreateBulk fails when entity has only ID field HOT 1
- Annotations in third-party mixins
- Is there a way to model generated virtual columns?
- NULL in `registered_at` field in getting started docs
- Optional JSON field can't be set to SQL NULL, only JSON null HOT 4
- Generated NamedDiff function does not consider entsql.Schema annotation
- How to easily extend entities with custom business logic? HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from ent.