-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
forbid nexus metadata fields in user payloads (#4013)
* forbid nexus metadata fields in user payloads * add config to turn off metadata field rejection * add second underscore check * add integration test * change approach * add back testing for the decoding lenience switch * add docs * add release notes * fix problems with config loading * fix test with underscore in payload * docs update
- Loading branch information
1 parent
a1095ce
commit 668f625
Showing
17 changed files
with
192 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletions
6
delta/app/src/test/resources/resources/errors/underscore-fields.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"@context" : "https://bluebrain.github.io/nexus/contexts/error.json", | ||
"@type" : "MalformedRequestContentRejection", | ||
"reason" : "The request content was malformed.", | ||
"details" : "DecodingFailure at : Field(s) starting with _ found in payload: _createdAt" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
delta/sdk/src/main/scala/ch/epfl/bluebrain/nexus/delta/sdk/resources/NexusSource.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package ch.epfl.bluebrain.nexus.delta.sdk.resources | ||
|
||
import io.circe.Decoder.Result | ||
import io.circe.{Decoder, DecodingFailure, HCursor, Json} | ||
import pureconfig.error.{CannotConvert, ConfigReaderFailures, ConvertFailure} | ||
import pureconfig.{ConfigCursor, ConfigReader} | ||
|
||
final case class NexusSource(value: Json) extends AnyVal | ||
|
||
object NexusSource { | ||
|
||
sealed trait DecodingOption | ||
|
||
object DecodingOption { | ||
final case object Strict extends DecodingOption | ||
|
||
final case object Lenient extends DecodingOption | ||
|
||
implicit val decodingOptionConfigReader: ConfigReader[DecodingOption] = { | ||
new ConfigReader[DecodingOption] { | ||
private val stringReader = implicitly[ConfigReader[String]] | ||
override def from(cur: ConfigCursor): ConfigReader.Result[DecodingOption] = { | ||
stringReader.from(cur).flatMap { | ||
case "strict" => Right(Strict) | ||
case "lenient" => Right(Lenient) | ||
case other => | ||
Left( | ||
ConfigReaderFailures( | ||
ConvertFailure( | ||
CannotConvert( | ||
other, | ||
"DecodingOption", | ||
s"values can only be 'strict' or 'lenient'" | ||
), | ||
cur | ||
) | ||
) | ||
) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
implicit def nexusSourceDecoder(implicit decodingOption: DecodingOption): Decoder[NexusSource] = { | ||
|
||
new Decoder[NexusSource] { | ||
private val decoder = implicitly[Decoder[Json]] | ||
|
||
println(decodingOption) | ||
|
||
override def apply(c: HCursor): Result[NexusSource] = { | ||
decoder(c).flatMap { json => | ||
val underscoreFields = json.asObject.toList.flatMap(_.keys).filter(_.startsWith("_")) | ||
if (underscoreFields.nonEmpty) { | ||
Left( | ||
DecodingFailure( | ||
s"Field(s) starting with _ found in payload: ${underscoreFields.mkString(", ")}", | ||
c.history | ||
) | ||
) | ||
} else { | ||
Right(NexusSource(json)) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
delta/sdk/src/test/resources/resources/resource-with-underscore-fields.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{ | ||
"@context": { | ||
"@vocab": "https://bluebrain.github.io/nexus/vocabulary/" | ||
}, | ||
"@id": "{{id}}", | ||
"@type": "Custom", | ||
"name": "Alex", | ||
"number": 24, | ||
"bool": false, | ||
"_createdAt": "1970-01-01T00:00:00Z" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.