-
Notifications
You must be signed in to change notification settings - Fork 159
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2820 from IBM/issue-2605
Reject FHIR resources with control chars #2605
- Loading branch information
Showing
13 changed files
with
792 additions
and
26 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
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
88 changes: 88 additions & 0 deletions
88
fhir-model/src/test/java/com/ibm/fhir/model/util/test/unicode/InjectCharacterChannel.java
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,88 @@ | ||
/* | ||
* (C) Copyright IBM Corp. 2021 | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package com.ibm.fhir.model.util.test.unicode; | ||
|
||
import java.io.IOException; | ||
import java.nio.ByteBuffer; | ||
import java.nio.channels.ReadableByteChannel; | ||
|
||
import com.ibm.fhir.model.util.test.unicode.strategy.CharacterControlStrategy; | ||
|
||
/** | ||
* InjectCharacterChannel modifies the Channel based on the CharacterControlStategy. | ||
* | ||
* @implNote The injection (today) occurs on the first key. It's designed to be extensible, such that | ||
* other hooks on the stream are used for injection. | ||
*/ | ||
public class InjectCharacterChannel implements ReadableByteChannel { | ||
|
||
private ReadableByteChannel channel; | ||
private CharacterControlStrategy strategy; | ||
private boolean first = true; | ||
|
||
public InjectCharacterChannel(ReadableByteChannel channel, CharacterControlStrategy strategy) { | ||
this.channel = channel; | ||
this.strategy = strategy; | ||
} | ||
|
||
@Override | ||
public void close() throws IOException { | ||
channel.close(); | ||
} | ||
|
||
@Override | ||
public boolean isOpen() { | ||
return channel.isOpen(); | ||
} | ||
|
||
@Override | ||
public int read(ByteBuffer dst) throws IOException { | ||
/* | ||
* Based on the strategy, the ReadableByteChannel is updated. | ||
*/ | ||
ByteBuffer temp = ByteBuffer.allocate(4096); | ||
int len = channel.read(temp); | ||
|
||
if (strategy.isApplicable(len, temp) && len > 0) { | ||
byte[] pre = strategy.pre(); | ||
|
||
// Translate the body to a smaller byte array. | ||
if (first) { | ||
len++; | ||
} | ||
byte[] body = new byte[len]; | ||
|
||
int pad = 0; | ||
for (int index = 0; index < len; index++) { | ||
byte t = temp.get(index - pad); | ||
int x = t; | ||
if (x == 34 && first) { | ||
body[index] = t; | ||
index++; | ||
body[index] = strategy.pre()[0]; | ||
pad = 1; | ||
first = false; | ||
} else { | ||
body[index] = t; | ||
} | ||
} | ||
byte[] post = strategy.post(); | ||
|
||
// Don't reallocate the dst buffer above. | ||
dst.put(body); | ||
//len += post.length; | ||
} else if (len > 0) { | ||
byte[] body = new byte[len]; | ||
for (int index = 0; index < len; index++) { | ||
byte t = temp.get(index); | ||
body[index] = t; | ||
} | ||
dst = dst.put(body); | ||
} | ||
return len; | ||
} | ||
} |
Oops, something went wrong.