@@ -107,6 +107,14 @@ var nativePortManifests = map[string]nativePortManifest{
107107 },
108108}
109109
110+ var nativePortManifestsByRuntimeProfile = func () map [string ]nativePortManifest {
111+ out := make (map [string ]nativePortManifest , len (nativePortManifests ))
112+ for _ , manifest := range nativePortManifests {
113+ out [nativePortProfileKey (manifest .RuntimeProfile )] = manifest
114+ }
115+ return out
116+ }()
117+
110118func nativePortMetadataFromForm (formValue func (string ) string , runtimeProfile string ) (nativePortMetadata , bool ) {
111119 if formValue == nil {
112120 return nativePortMetadata {}, false
@@ -125,24 +133,34 @@ func nativePortMetadataFromForm(formValue func(string) string, runtimeProfile st
125133 return meta , meta .PortID != "" || meta .RuntimeProfile != ""
126134}
127135
136+ func completeNativePortMetadata (meta nativePortMetadata ) nativePortMetadata {
137+ if meta .PortID == "" && meta .RuntimeProfile == "" {
138+ return meta
139+ }
140+ manifest , ok := nativePortManifestForMetadata (meta )
141+ if ! ok {
142+ return meta
143+ }
144+ if meta .PortID == "" {
145+ meta .PortID = manifest .ID
146+ }
147+ if meta .PortName == "" {
148+ meta .PortName = manifest .Name
149+ }
150+ if meta .OriginSystemSlug == "" {
151+ meta .OriginSystemSlug = manifest .OriginSystemSlug
152+ }
153+ if meta .RuntimeProfile == "" {
154+ meta .RuntimeProfile = manifest .RuntimeProfile
155+ }
156+ return meta
157+ }
158+
128159func applyNativePortInput (input saveCreateInput , meta nativePortMetadata ) saveCreateInput {
129160 if meta .PortID == "" && meta .RuntimeProfile == "" {
130161 return input
131162 }
132- if manifest , ok := nativePortManifestForMetadata (meta ); ok {
133- if meta .PortID == "" {
134- meta .PortID = manifest .ID
135- }
136- if meta .PortName == "" {
137- meta .PortName = manifest .Name
138- }
139- if meta .OriginSystemSlug == "" {
140- meta .OriginSystemSlug = manifest .OriginSystemSlug
141- }
142- if meta .RuntimeProfile == "" {
143- meta .RuntimeProfile = manifest .RuntimeProfile
144- }
145- }
163+ meta = completeNativePortMetadata (meta )
146164 input .PortID = meta .PortID
147165 input .PortName = meta .PortName
148166 input .OriginSystemSlug = meta .OriginSystemSlug
@@ -167,20 +185,7 @@ func attachNativePortMetadata(input saveCreateInput, meta nativePortMetadata) sa
167185 if meta .PortID == "" && meta .RuntimeProfile == "" {
168186 return input
169187 }
170- if manifest , ok := nativePortManifestForMetadata (meta ); ok {
171- if meta .PortID == "" {
172- meta .PortID = manifest .ID
173- }
174- if meta .PortName == "" {
175- meta .PortName = manifest .Name
176- }
177- if meta .OriginSystemSlug == "" {
178- meta .OriginSystemSlug = manifest .OriginSystemSlug
179- }
180- if meta .RuntimeProfile == "" {
181- meta .RuntimeProfile = manifest .RuntimeProfile
182- }
183- }
188+ meta = completeNativePortMetadata (meta )
184189 input .PortID = firstNonEmpty (input .PortID , meta .PortID )
185190 input .PortName = firstNonEmpty (input .PortName , meta .PortName )
186191 input .OriginSystemSlug = firstNonEmpty (input .OriginSystemSlug , meta .OriginSystemSlug )
@@ -269,34 +274,13 @@ func titleizeNativePortSlot(value string) string {
269274}
270275
271276func validateNativePortSave (input saveCreateInput , detection saveSystemDetectionResult ) consoleValidationResult {
272- if ! detection .Evidence .HelperTrusted && ! metadataHasTrustedSystemEvidence (input .Metadata ) {
273- return consoleValidationResult {
274- Rejected : true ,
275- RejectReason : "native port saves must come from a trusted helper manifest" ,
276- }
277- }
278277 meta := nativePortMetadataFromInput (input )
279278 manifest , ok := nativePortManifestForMetadata (meta )
280279 if ! ok {
281280 return consoleValidationResult {Rejected : true , RejectReason : "unknown native port manifest" }
282281 }
283- if canonicalOptionalSegment (meta .PortSaveKind ) != "progress" {
284- return consoleValidationResult {Rejected : true , RejectReason : "only native port progress saves are supported" }
285- }
286- if len (input .Payload ) == 0 {
287- return consoleValidationResult {Rejected : true , RejectReason : "native port save payload is empty" }
288- }
289- if len (input .Payload ) > 16 * 1024 * 1024 {
290- return consoleValidationResult {Rejected : true , RejectReason : "native port save payload is too large" }
291- }
292- if ! nativePortPathAllowed (manifest , meta .RelativePath ) {
293- return consoleValidationResult {
294- Rejected : true ,
295- RejectReason : fmt .Sprintf ("native port path %q is not allowed for %s" , meta .RelativePath , manifest .ID ),
296- }
297- }
298- if meta .RootRelativePath != "" && ! safePortRelativePath (meta .RootRelativePath ) {
299- return consoleValidationResult {Rejected : true , RejectReason : "native port root-relative path is unsafe" }
282+ if result , ok := validateNativePortEnvelope (input , detection , meta , manifest ); ! ok {
283+ return result
300284 }
301285 return consoleValidationResult {
302286 Inspection : & saveInspection {
@@ -335,30 +319,9 @@ func originNativePortManifestForInput(input saveCreateInput, systemSlug string)
335319}
336320
337321func validateOriginNativePortSave (input saveCreateInput , detection saveSystemDetectionResult , manifest nativePortManifest ) consoleValidationResult {
338- if ! detection .Evidence .HelperTrusted && ! metadataHasTrustedSystemEvidence (input .Metadata ) {
339- return consoleValidationResult {
340- Rejected : true ,
341- RejectReason : "native port saves must come from a trusted helper manifest" ,
342- }
343- }
344322 meta := nativePortMetadataFromInput (input )
345- if canonicalOptionalSegment (meta .PortSaveKind ) != "progress" {
346- return consoleValidationResult {Rejected : true , RejectReason : "only native port progress saves are supported" }
347- }
348- if len (input .Payload ) == 0 {
349- return consoleValidationResult {Rejected : true , RejectReason : "native port save payload is empty" }
350- }
351- if len (input .Payload ) > 16 * 1024 * 1024 {
352- return consoleValidationResult {Rejected : true , RejectReason : "native port save payload is too large" }
353- }
354- if ! nativePortPathAllowed (manifest , meta .RelativePath ) {
355- return consoleValidationResult {
356- Rejected : true ,
357- RejectReason : fmt .Sprintf ("native port path %q is not allowed for %s" , meta .RelativePath , manifest .ID ),
358- }
359- }
360- if meta .RootRelativePath != "" && ! safePortRelativePath (meta .RootRelativePath ) {
361- return consoleValidationResult {Rejected : true , RejectReason : "native port root-relative path is unsafe" }
323+ if result , ok := validateNativePortEnvelope (input , detection , meta , manifest ); ! ok {
324+ return result
362325 }
363326
364327 title := strings .TrimSpace (manifest .OriginGameTitle )
@@ -390,6 +353,34 @@ func validateOriginNativePortSave(input saveCreateInput, detection saveSystemDet
390353 }
391354}
392355
356+ func validateNativePortEnvelope (input saveCreateInput , detection saveSystemDetectionResult , meta nativePortMetadata , manifest nativePortManifest ) (consoleValidationResult , bool ) {
357+ if ! detection .Evidence .HelperTrusted && ! metadataHasTrustedSystemEvidence (input .Metadata ) {
358+ return consoleValidationResult {
359+ Rejected : true ,
360+ RejectReason : "native port saves must come from a trusted helper manifest" ,
361+ }, false
362+ }
363+ if canonicalOptionalSegment (meta .PortSaveKind ) != "progress" {
364+ return consoleValidationResult {Rejected : true , RejectReason : "only native port progress saves are supported" }, false
365+ }
366+ if len (input .Payload ) == 0 {
367+ return consoleValidationResult {Rejected : true , RejectReason : "native port save payload is empty" }, false
368+ }
369+ if len (input .Payload ) > 16 * 1024 * 1024 {
370+ return consoleValidationResult {Rejected : true , RejectReason : "native port save payload is too large" }, false
371+ }
372+ if ! nativePortPathAllowed (manifest , meta .RelativePath ) {
373+ return consoleValidationResult {
374+ Rejected : true ,
375+ RejectReason : fmt .Sprintf ("native port path %q is not allowed for %s" , meta .RelativePath , manifest .ID ),
376+ }, false
377+ }
378+ if meta .RootRelativePath != "" && ! safePortRelativePath (meta .RootRelativePath ) {
379+ return consoleValidationResult {Rejected : true , RejectReason : "native port root-relative path is unsafe" }, false
380+ }
381+ return consoleValidationResult {}, true
382+ }
383+
393384func nativePortMetadataFromInput (input saveCreateInput ) nativePortMetadata {
394385 return nativePortMetadata {
395386 PortID : strings .TrimSpace (input .PortID ),
@@ -409,13 +400,7 @@ func nativePortManifestForMetadata(meta nativePortMetadata) (nativePortManifest,
409400 manifest , ok := nativePortManifests [meta .PortID ]
410401 return manifest , ok
411402 }
412- profile := strings .TrimSpace (meta .RuntimeProfile )
413- for _ , manifest := range nativePortManifests {
414- if profile != "" && strings .EqualFold (manifest .RuntimeProfile , profile ) {
415- return manifest , true
416- }
417- }
418- return nativePortManifest {}, false
403+ return nativePortManifestForRuntimeProfile (meta .RuntimeProfile )
419404}
420405
421406func nativePortPathAllowed (manifest nativePortManifest , relativePath string ) bool {
0 commit comments